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}
66
67/// Shared staggered multi-bucket heartbeat wheel driving idle WebSocket ping timeouts from one timer.
68pub(crate) struct WsHeartbeatWheel {
69 /// Staggered watch channels notified one bucket per step to prevent thundering-herd pings.
70 pub(crate) buckets: Box<[Sender<u64>]>,
71 /// Rotating phase cursor advanced once per step by the shared background ticker task.
72 pub(crate) phase: AtomicUsize,
73 /// Indicates whether the background ticker task is still running.
74 pub(crate) alive: AtomicBool,
75}
76
77/// Partial WebSocket input that must survive selection of an outbound message.
78#[derive(Clone, Data, Debug)]
79pub(super) struct WebSocketReadState {
80 /// Wire bytes not yet consumed by a complete frame.
81 #[get(pub(super), type(ref))]
82 #[get_mut(pub(super))]
83 #[set(pub(super))]
84 pub(super) buffer: Vec<u8>,
85 /// Start offset of unconsumed wire bytes within `buffer`.
86 #[get(pub(super), type(copy))]
87 #[get_mut(pub(super))]
88 #[set(pub(super))]
89 pub(super) cursor: usize,
90 /// Payload accumulated from a message spanning multiple frames.
91 #[get(pub(super))]
92 #[get_mut(pub(super))]
93 #[set(pub(super))]
94 pub(super) full_frame: Vec<u8>,
95 /// Data opcode of the currently active fragmented message, if any.
96 #[get(pub(super), type(ref))]
97 #[get_mut(pub(super))]
98 #[set(pub(super))]
99 pub(super) fragment_opcode: Option<WebSocketOpcode>,
100}
101
102/// A buffered reader over a transport with a reusable read buffer.
103///
104/// This reader batches reads into an internal buffer to reduce syscall count,
105/// and returns drained buffers to a thread-local pool while waiting for more
106/// input so idle keep-alive connections do not each retain a read buffer.
107#[derive(Data, New)]
108pub(crate) struct PooledReader<'a> {
109 /// The underlying transport being read.
110 #[get(pub(super))]
111 #[get_mut(pub(super))]
112 #[set(pub(super))]
113 pub(super) stream: &'a mut Box<dyn Transport>,
114 /// The reusable read buffer holding unconsumed bytes in `start..end`.
115 #[get(pub(super))]
116 #[get_mut(pub(super))]
117 #[set(pub(super))]
118 pub(super) buffer: Vec<u8>,
119 /// The required buffer length when the transport becomes readable.
120 #[get(pub(super), type(copy))]
121 #[get_mut(pub(super))]
122 #[set(pub(super))]
123 pub(super) capacity: usize,
124 /// The index of the first unconsumed byte in the buffer.
125 #[get(pub(super), type(copy))]
126 #[get_mut(pub(super))]
127 #[set(pub(super))]
128 #[new(skip)]
129 pub(super) start: usize,
130 /// The index one past the last valid byte in the buffer.
131 #[get(pub(super), type(copy))]
132 #[get_mut(pub(super))]
133 #[set(pub(super))]
134 #[new(skip)]
135 pub(super) end: usize,
136}