pub struct Ctx<'a> { /* private fields */ }Expand description
What a Connection callback can do: send, close, pause, set timers, reach other threads.
Implementations§
Source§impl Ctx<'_>
impl Ctx<'_>
Sourcepub fn send(&mut self, buf: Vec<u8>)
pub fn send(&mut self, buf: Vec<u8>)
Queues bytes to send. Output queued in one loop turn goes out together, in order, at the end of the turn. The buffer returns to the loop’s pool once written.
Sourcepub fn send_static(&mut self, bytes: &'static [u8])
pub fn send_static(&mut self, bytes: &'static [u8])
Queues static bytes to send (a fixed reply costs no copy).
Examples found in repository?
53 fn on_message(&mut self, cx: &mut Ctx<'_>, _msg: Message) {
54 for _ in 0..self.pipeline {
55 cx.send_static(REQUEST);
56 }
57 }
58
59 fn on_data(&mut self, cx: &mut Ctx<'_>, data: &[u8]) -> usize {
60 let mut pos = 0;
61 let mut n = 0u64;
62 while let Some(len) = response_len(&data[pos..]) {
63 pos += len;
64 n += 1;
65 cx.send_static(REQUEST);
66 }
67 DONE.with(|c| c.set(c.get() + n));
68 pos
69 }More examples
49 fn on_data(&mut self, cx: &mut Ctx<'_>, data: &[u8]) -> usize {
50 let (n, end) = heads(data);
51 for _ in 0..n {
52 cx.send_static(RESPONSE);
53 }
54 SERVED.with(|c| c.set(c.get() + n as u64));
55 // `Connection: close` (a connection per request, S5): close once the reply is written,
56 // as an HTTP server does.
57 if n > 0 && data[..end].windows(17).any(|w| w.eq_ignore_ascii_case(b"connection: close")) {
58 cx.close();
59 }
60 end
61 }Sourcepub fn buffer(&mut self, min: usize) -> Vec<u8> ⓘ
pub fn buffer(&mut self, min: usize) -> Vec<u8> ⓘ
An empty buffer from the loop’s pool with room for at least min bytes, to build output
in. Hand it to Ctx::send or back with Ctx::recycle.
Sourcepub fn pause_reading(&mut self)
pub fn pause_reading(&mut self)
Stops calling Connection::on_data and stops reading from the socket (backpressure,
for example while a request is processed elsewhere).
Sourcepub fn resume_reading(&mut self)
pub fn resume_reading(&mut self)
Undoes Ctx::pause_reading. Input that arrived meanwhile is offered to
Connection::on_data right after the current callback.
Sourcepub fn redeliver(&mut self)
pub fn redeliver(&mut self)
Offers input that is still unconsumed to Connection::on_data again right after the
current callback, for a connection that left input unconsumed without pausing (it was
busy) and can take it now.
Sourcepub fn notify_drain(&mut self)
pub fn notify_drain(&mut self)
Asks for Connection::on_drain once the queued output is at or below the low-water
mark (right after this callback if it already is).
Sourcepub fn close(&mut self)
pub fn close(&mut self)
Closes gracefully: queued output is written first, then the socket is closed. Nothing
more is delivered except Connection::on_close.
Examples found in repository?
49 fn on_data(&mut self, cx: &mut Ctx<'_>, data: &[u8]) -> usize {
50 let (n, end) = heads(data);
51 for _ in 0..n {
52 cx.send_static(RESPONSE);
53 }
54 SERVED.with(|c| c.set(c.get() + n as u64));
55 // `Connection: close` (a connection per request, S5): close once the reply is written,
56 // as an HTTP server does.
57 if n > 0 && data[..end].windows(17).any(|w| w.eq_ignore_ascii_case(b"connection: close")) {
58 cx.close();
59 }
60 end
61 }Sourcepub fn is_closing(&mut self) -> bool
pub fn is_closing(&mut self) -> bool
Whether Ctx::close or Ctx::abort was called.
Sourcepub fn set_timer(&mut self, after: Duration, token: u64) -> TimerId
pub fn set_timer(&mut self, after: Duration, token: u64) -> TimerId
Calls Connection::on_timer with token after after (rounded up to the loop’s
timer tick). The timers of a connection are cancelled when it is closed.
Sourcepub fn cancel_timer(&mut self, id: TimerId) -> bool
pub fn cancel_timer(&mut self, id: TimerId) -> bool
Cancels a timer. Returns whether it was still pending.
Sourcepub fn handle(&mut self) -> ConnHandle
pub fn handle(&mut self) -> ConnHandle
A handle for reaching this connection from other threads.
Sourcepub fn offload<F>(&mut self, job: F) -> Result<(), Rejected>
pub fn offload<F>(&mut self, job: F) -> Result<(), Rejected>
Runs job on the shared offload pool and delivers its result to
Connection::on_message. Fails at once when the pool is full or overloaded.
Sourcepub fn offload_pool(&mut self) -> &OffloadPool
pub fn offload_pool(&mut self) -> &OffloadPool
The offload pool, for callers that manage their own completion.
Sourcepub fn peer_addr(&mut self) -> Result<SocketAddr>
pub fn peer_addr(&mut self) -> Result<SocketAddr>
The peer’s address.
Sourcepub fn local_addr(&mut self) -> Result<SocketAddr>
pub fn local_addr(&mut self) -> Result<SocketAddr>
The local address.
Sourcepub fn loop_index(&mut self) -> usize
pub fn loop_index(&mut self) -> usize
The index of the loop running this connection.
Sourcepub fn connection_pressure(&mut self) -> bool
pub fn connection_pressure(&mut self) -> bool
Whether open connections passed three quarters of what the server can hold (its
max_connections, and on Linux its file descriptor limit): time to shed connections
that hold a slot without doing anything useful (K6).
Asked only when a slow request head reaches its deadline, so the descriptor limit is
read then (one getrlimit), not kept per loop.