Skip to main content

Ctx

Struct Ctx 

Source
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<'_>

Source

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.

Source

pub fn send_static(&mut self, bytes: &'static [u8])

Queues static bytes to send (a fixed reply costs no copy).

Examples found in repository?
examples/loadgen.rs (line 55)
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
Hide additional examples
examples/raw_http.rs (line 52)
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    }
Source

pub fn send_buf(&mut self, buf: OutBuf)

Queues an OutBuf.

Source

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.

Source

pub fn recycle(&mut self, buf: Vec<u8>)

Returns a buffer to the loop’s pool.

Source

pub fn queued(&mut self) -> usize

Bytes queued and not yet written.

Source

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).

Source

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.

Source

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.

Source

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).

Source

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?
examples/raw_http.rs (line 58)
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    }
Source

pub fn abort(&mut self)

Closes at once, discarding queued output.

Source

pub fn is_closing(&mut self) -> bool

Whether Ctx::close or Ctx::abort was called.

Source

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.

Source

pub fn cancel_timer(&mut self, id: TimerId) -> bool

Cancels a timer. Returns whether it was still pending.

Source

pub fn handle(&mut self) -> ConnHandle

A handle for reaching this connection from other threads.

Source

pub fn offload<F>(&mut self, job: F) -> Result<(), Rejected>
where F: FnOnce() -> Message + Send + 'static,

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.

Source

pub fn offload_pool(&mut self) -> &OffloadPool

The offload pool, for callers that manage their own completion.

Source

pub fn now_ns(&mut self) -> u64

The loop’s clock in nanoseconds (read once per loop turn).

Source

pub fn peer_addr(&mut self) -> Result<SocketAddr>

The peer’s address.

Source

pub fn local_addr(&mut self) -> Result<SocketAddr>

The local address.

Source

pub fn socket(&mut self) -> Option<&Socket>

The socket, for options the runtime doesn’t wrap.

Source

pub fn loop_index(&mut self) -> usize

The index of the loop running this connection.

Source

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.

Trait Implementations§

Source§

impl Debug for Ctx<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for Ctx<'a>

§

impl<'a> !Send for Ctx<'a>

§

impl<'a> !Sync for Ctx<'a>

§

impl<'a> !UnwindSafe for Ctx<'a>

§

impl<'a> Forget for Ctx<'a>

§

impl<'a> Freeze for Ctx<'a>

§

impl<'a> Move for Ctx<'a>

§

impl<'a> Unpin for Ctx<'a>

§

impl<'a> UnsafeUnpin for Ctx<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.