Skip to main content

http_type/stream/
struct.rs

1use super::*;
2
3#[derive(CustomDebug, Data, DisplayDebug)]
4pub struct Stream {
5    /// The bidirectional byte pipe the request/response cycle runs over.
6    ///
7    /// The concrete type is erased behind `Transport`, so a plain TCP socket, a
8    /// TLS session and a per-request duplex stream are interchangeable here.
9    /// Address metadata comes from the transport itself, so a transport without
10    /// a socket identity reports an error instead of a fabricated address.
11    pub(crate) stream: Box<dyn Transport>,
12    #[get_mut(pub(super))]
13    #[set(pub(super))]
14    pub(super) request_config: RequestConfig,
15    #[get(type(copy))]
16    #[get_mut(pub(super))]
17    pub(super) closed: bool,
18    /// Whether a WebSocket peer frame has arrived since the last idle ping probe (stored in struct padding).
19    #[get(skip)]
20    #[get_mut(skip)]
21    #[set(skip)]
22    pub(super) ws_client_response: bool,
23    /// Bytes read ahead of the current request, already consumed from the
24    /// transport but not yet parsed into a request.
25    ///
26    /// A single read can deliver bytes belonging to the next pipelined
27    /// request. Those bytes are carried here between requests so
28    /// HTTP pipelining is not silently dropped.
29    #[get_mut(pub(super))]
30    #[set(pub(super))]
31    pub(super) leftover: Vec<u8>,
32    /// Offset of the first unconsumed byte in `leftover`.
33    #[get(type(copy))]
34    #[get_mut(pub(super))]
35    #[set(pub(super))]
36    pub(super) leftover_start: usize,
37    /// Offset one past the last valid byte in `leftover`.
38    #[get(type(copy))]
39    #[get_mut(pub(super))]
40    #[set(pub(super))]
41    pub(super) leftover_end: usize,
42    /// The buffer the request line is read into.
43    ///
44    /// It lives with the connection rather than with the request, so a
45    /// keep-alive connection reads its second, third and later request lines
46    /// into the same allocation instead of a fresh one per request.
47    #[get_mut(pub(super))]
48    #[set(pub(super))]
49    pub(super) line_buffer: String,
50    /// The buffer the header lines are read into, kept for the same reason as
51    /// the request line buffer.
52    #[get_mut(pub(super))]
53    #[set(pub(super))]
54    pub(super) header_line_buffer: String,
55    /// Lazily allocated WebSocket parsing state retained only across partial/fragmented/pipelined frames.
56    #[get(pub(super), type(ref))]
57    #[get_mut(pub(super))]
58    #[set(pub(super))]
59    pub(super) websocket_state: Option<Box<WebSocketReadState>>,
60    /// Reusable read-timeout sleep timer or shared WebSocket heartbeat receiver.
61    #[get(skip)]
62    #[get_mut(skip)]
63    #[set(skip)]
64    pub(super) read_timer: StreamTimer,
65    /// Reusable outbound buffer for zero-allocation response serialization and pipelined write coalescing.
66    #[get(pub(super))]
67    #[get_mut(pub(super))]
68    #[set(pub(super))]
69    pub(super) write_buffer: Vec<u8>,
70    /// Start byte offset of the parsed request target inside `line_buffer` (`0` when unindexed).
71    #[get(pub(super), type(copy))]
72    #[get_mut(pub(super))]
73    #[set(pub(super))]
74    pub(super) line_target_start: usize,
75    /// End byte offset of the parsed request target inside `line_buffer` (`0` when unindexed).
76    #[get(pub(super), type(copy))]
77    #[get_mut(pub(super))]
78    #[set(pub(super))]
79    pub(super) line_target_end: usize,
80    /// Lazily cached peer socket address avoiding per-request `getpeername` syscalls.
81    #[get(pub(super))]
82    #[get_mut(skip)]
83    #[set(skip)]
84    pub(super) cached_peer_addr: OnceLock<SocketAddr>,
85    /// Lazily cached local socket address avoiding per-request `getsockname` syscalls.
86    #[get(pub(super))]
87    #[get_mut(skip)]
88    #[set(skip)]
89    pub(super) cached_local_addr: OnceLock<SocketAddr>,
90}
91
92/// Shared staggered multi-bucket heartbeat wheel driving idle WebSocket ping timeouts from one timer.
93pub(crate) struct WsHeartbeatWheel {
94    /// Staggered watch channels notified one bucket per step to prevent thundering-herd pings.
95    pub(crate) buckets: Box<[Sender<u64>]>,
96    /// Rotating phase cursor advanced once per step by the shared background ticker task.
97    pub(crate) phase: AtomicUsize,
98    /// Indicates whether the background ticker task is still running.
99    pub(crate) alive: AtomicBool,
100}
101
102/// Partial WebSocket input that must survive selection of an outbound message.
103#[derive(Clone, Data, Debug)]
104pub(super) struct WebSocketReadState {
105    /// Wire bytes not yet consumed by a complete frame.
106    #[get(pub(super), type(ref))]
107    #[get_mut(pub(super))]
108    #[set(pub(super))]
109    pub(super) buffer: Vec<u8>,
110    /// Start offset of unconsumed wire bytes within `buffer`.
111    #[get(pub(super), type(copy))]
112    #[get_mut(pub(super))]
113    #[set(pub(super))]
114    pub(super) cursor: usize,
115    /// Payload accumulated from a message spanning multiple frames.
116    #[get(pub(super))]
117    #[get_mut(pub(super))]
118    #[set(pub(super))]
119    pub(super) full_frame: Vec<u8>,
120    /// Data opcode of the currently active fragmented message, if any.
121    #[get(pub(super), type(ref))]
122    #[get_mut(pub(super))]
123    #[set(pub(super))]
124    pub(super) fragment_opcode: Option<WebSocketOpcode>,
125}
126
127/// A buffered reader over a transport with a reusable read buffer.
128///
129/// This reader batches reads into an internal buffer to reduce syscall count,
130/// and returns drained buffers to a thread-local pool while waiting for more
131/// input so idle keep-alive connections do not each retain a read buffer.
132#[derive(Data, New)]
133pub(crate) struct PooledReader<'a> {
134    /// The underlying transport being read.
135    #[get(pub(super))]
136    #[get_mut(pub(super))]
137    #[set(pub(super))]
138    pub(super) stream: &'a mut Box<dyn Transport>,
139    /// The reusable read buffer holding unconsumed bytes in `start..end`.
140    #[get(pub(super))]
141    #[get_mut(pub(super))]
142    #[set(pub(super))]
143    pub(super) buffer: Vec<u8>,
144    /// The required buffer length when the transport becomes readable.
145    #[get(pub(super), type(copy))]
146    #[get_mut(pub(super))]
147    #[set(pub(super))]
148    pub(super) capacity: usize,
149    /// The index of the first unconsumed byte in the buffer.
150    #[get(pub(super), type(copy))]
151    #[get_mut(pub(super))]
152    #[set(pub(super))]
153    #[new(skip)]
154    pub(super) start: usize,
155    /// The index one past the last valid byte in the buffer.
156    #[get(pub(super), type(copy))]
157    #[get_mut(pub(super))]
158    #[set(pub(super))]
159    #[new(skip)]
160    pub(super) end: usize,
161}