Skip to main content

open_net/api/ws/
response.rs

1use super::{ConnectionId, IncomingMessage, Message, RequestId};
2use crate::error::{ErrorKind, NetError};
3use crate::Result;
4use std::time::SystemTime;
5
6/// Owned business response that can outlive its inbound message and registration.
7#[derive(Clone, Debug)]
8pub struct Response {
9    /// Identifier of the business request completed by this response.
10    request_id: RequestId,
11    /// Text or binary business body, excluding control frames.
12    message: Message,
13    /// Time at which the inbound message was received.
14    received_at: SystemTime,
15    /// Physical connection that received the response.
16    connection_id: ConnectionId,
17    retry_report: Option<crate::api::retry::RetryReport>,
18    #[cfg(feature = "quality-doctor")]
19    quality: Option<crate::api::quality::QualityReportRef>,
20}
21
22impl Response {
23    /// Returns the associated request identifier.
24    pub(crate) fn from_incoming(request_id: RequestId, incoming: &IncomingMessage) -> Result<Self> {
25        let message = incoming
26            .message()
27            .ok_or_else(|| NetError::from(ErrorKind::InvalidInput))?
28            .clone();
29        Ok(Self {
30            request_id,
31            message,
32            received_at: incoming.received_at(),
33            connection_id: incoming.connection_id(),
34            retry_report: None,
35            #[cfg(feature = "quality-doctor")]
36            quality: None,
37        })
38    }
39
40    /// Returns the request observation chosen by the path delivering this response.
41    ///
42    /// A fixed request attaches its registration's `WsRequest` reference. A
43    /// retry-capable logical operation selects its stable `WsRequestOperation`
44    /// reference when it produces the final response, replacing the individual
45    /// registration reference. Inspect the reference's kind to distinguish them.
46    /// This is separate from the `WsReceive` observation exposed on the source
47    /// [`IncomingMessage`]; it is not a socket-receive latency measurement.
48    ///
49    /// `None` means the selected request path has no observation reference, for
50    /// example because diagnostics were Off/dormant, filtered out, or unable to
51    /// allocate one. The accessor does not activate diagnostics. Resolve a
52    /// reference using the owning engine's `QualityDoctor::lookup`; the report
53    /// may still be receiving delivery events, may be incomplete, or may have
54    /// been evicted. Keeping the response does not pin its diagnostic history.
55    #[cfg(feature = "quality-doctor")]
56    pub fn quality_ref(&self) -> Option<crate::api::quality::QualityReportRef> {
57        self.quality
58    }
59    #[cfg(feature = "quality-doctor")]
60    pub(crate) fn with_quality_ref(mut self, reference: Option<crate::api::quality::QualityReportRef>) -> Self {
61        self.quality = reference;
62        self
63    }
64    /// Retry diagnostics for a completed logical request; absent on fixed single requests.
65    pub fn retry_report(&self) -> Option<&crate::api::retry::RetryReport> {
66        self.retry_report.as_ref()
67    }
68    pub(crate) fn with_retry_report(mut self, report: crate::api::retry::RetryReport) -> Self {
69        self.retry_report = Some(report);
70        self
71    }
72    /// Returns the associated request identifier.
73    pub fn request_id(&self) -> &RequestId {
74        &self.request_id
75    }
76
77    /// Borrows the response body.
78    pub fn message(&self) -> &Message {
79        &self.message
80    }
81
82    /// Returns the receive timestamp recorded by the SDK.
83    pub fn received_at(&self) -> SystemTime {
84        self.received_at
85    }
86
87    /// Returns the physical connection identity.
88    pub fn connection_id(&self) -> ConnectionId {
89        self.connection_id
90    }
91
92    /// Consumes the response and returns its body.
93    pub fn into_message(self) -> Message {
94        self.message
95    }
96}
97
98#[cfg(test)]
99#[path = "response_tests.rs"]
100mod tests;