Struct Ctx

Source
pub struct Ctx {
Show 45 fields pub connection_id: usize, pub processing: i32, pub accepted: u64, pub location_processing: i32, pub location_accepted: u64, pub created_at: u64, pub tls_version: Option<String>, pub tls_cipher: Option<String>, pub tls_handshake_time: Option<u64>, pub status: Option<StatusCode>, pub connection_time: u64, pub connection_reused: bool, pub location: String, pub upstream_address: String, pub client_ip: Option<String>, pub remote_port: Option<u16>, pub remote_addr: Option<String>, pub server_port: Option<u16>, pub server_addr: Option<String>, pub guard: Option<Guard>, pub request_id: Option<String>, pub cache_namespace: Option<String>, pub cache_prefix: Option<String>, pub check_cache_control: bool, pub cache_lookup_time: Option<u64>, pub cache_lock_time: Option<u64>, pub cache_max_ttl: Option<Duration>, pub upstream: String, pub upstream_reused: bool, pub upstream_processing: Option<i32>, pub upstream_connect_time: Option<u64>, pub upstream_connected: Option<i32>, pub upstream_tcp_connect_time: Option<u64>, pub upstream_tls_handshake_time: Option<u64>, pub upstream_processing_time: Option<u64>, pub upstream_response_time: Option<u64>, pub upstream_connection_time: Option<u64>, pub payload_size: usize, pub compression_stat: Option<CompressionStat>, pub modify_response_body: Option<Box<dyn ModifyResponseBody>>, pub response_body: Option<BytesMut>, pub cache_reading: Option<u32>, pub cache_writing: Option<u32>, pub variables: Option<AHashMap<String, String>>, pub plugin_processing_times: Option<Vec<(String, u32)>>,
}
Expand description

Represents the state of a request/response cycle, tracking various metrics and properties including connection details, caching information, and upstream server interactions.

Fields§

§connection_id: usize

Unique identifier for the connection, it should be unique among all existing connections of the same type

§processing: i32

Number of requests currently processing

§accepted: u64

Total number of requests accepted

§location_processing: i32

Number of requests currently processing for the current location

§location_accepted: u64

Total number of requests accepted for the current location

§created_at: u64

Timestamp when this context was created (in milliseconds)

§tls_version: Option<String>

TLS version used by the client connection (e.g., “TLSv1.3”)

§tls_cipher: Option<String>

TLS cipher suite used by the client connection

§tls_handshake_time: Option<u64>

Time taken for TLS handshake with client (in milliseconds)

§status: Option<StatusCode>

HTTP status code of the response

§connection_time: u64

Total time the connection has been alive (in milliseconds) May be large for reused connections

§connection_reused: bool

Indicates if this connection is reused

§location: String

The location handling request

§upstream_address: String

Address of the upstream server

§client_ip: Option<String>

Client’s IP address

§remote_port: Option<u16>

Remote connection port

§remote_addr: Option<String>

Remote connection address

§server_port: Option<u16>

Server’s listening port

§server_addr: Option<String>

Server’s address

§guard: Option<Guard>

Rate limiting guard

§request_id: Option<String>

Unique identifier for the request

§cache_namespace: Option<String>

Namespace for cache entries

§cache_prefix: Option<String>

Prefix for cache keys

§check_cache_control: bool

Whether to check cache control headers

§cache_lookup_time: Option<u64>

Time spent looking up cache entries (in milliseconds)

§cache_lock_time: Option<u64>

Time spent acquiring cache locks (in milliseconds)

§cache_max_ttl: Option<Duration>

Maximum time-to-live for cache entries

§upstream: String

The upstream server

§upstream_reused: bool

Indicates if the upstream connection is reused

§upstream_processing: Option<i32>

Number of requests processing by upstream

§upstream_connect_time: Option<u64>

Time taken to establish/reuse upstream connection (in milliseconds)

§upstream_connected: Option<i32>

Current number of active upstream connections

§upstream_tcp_connect_time: Option<u64>

Time taken for TCP connection to upstream (in milliseconds)

§upstream_tls_handshake_time: Option<u64>

Time taken for TLS handshake with upstream (in milliseconds)

§upstream_processing_time: Option<u64>

Time taken by upstream server to process request (in milliseconds)

§upstream_response_time: Option<u64>

Total time taken by upstream server (in milliseconds)

§upstream_connection_time: Option<u64>

Total time the upstream connection has been alive (in milliseconds) May be large for reused connections

§payload_size: usize

Size of the request payload in bytes

§compression_stat: Option<CompressionStat>

Statistics about response compression

§modify_response_body: Option<Box<dyn ModifyResponseBody>>

Handler for modifying response body

§response_body: Option<BytesMut>

Body buffer of modified response

§cache_reading: Option<u32>

Number of cache reading operations

§cache_writing: Option<u32>

Number of cache writing operations

§variables: Option<AHashMap<String, String>>

Custom variables map for request processing

§plugin_processing_times: Option<Vec<(String, u32)>>

Plugin processing times

Implementations§

Source§

impl Ctx

Source

pub fn new() -> Self

Creates a new Ctx instance with the current timestamp and default values.

Returns a new Ctx struct initialized with the current timestamp and all other fields set to their default values.

Source

pub fn add_variable(&mut self, key: &str, value: &str)

Adds a variable to the state’s variables map with the given key and value. The key will be automatically prefixed with ‘$’ before being stored.

§Arguments
  • key - The variable name (will be prefixed with ‘$’)
  • value - The value to store for this variable
Source

pub fn get_variable(&self, key: &str) -> Option<&str>

Returns the value of a variable by key.

§Arguments
  • key - The key of the variable to retrieve

Returns: Option<&str> representing the value of the variable, or None if the variable does not exist

Source

pub fn get_upstream_response_time(&self) -> Option<u64>

Returns the upstream response time if it’s less than one hour, otherwise None. This helps filter out potentially invalid or stale timing data.

Returns: Option representing milliseconds, or None if time exceeds 1 hour

Source

pub fn get_upstream_connect_time(&self) -> Option<u64>

Returns the upstream connect time if it’s less than one hour, otherwise None. This helps filter out potentially invalid or stale timing data.

Returns: Option representing milliseconds, or None if time exceeds 1 hour

Source

pub fn get_upstream_processing_time(&self) -> Option<u64>

Returns the upstream processing time if it’s less than one hour, otherwise None. This helps filter out potentially invalid or stale timing data.

Returns: Option representing milliseconds, or None if time exceeds 1 hour

Source

pub fn add_plugin_processing_time(&mut self, name: &str, time: u32)

Adds a plugin processing time to the context

§Arguments
  • name - The name of the plugin
  • time - The time taken by the plugin in milliseconds
Source

pub fn append_value(&self, buf: BytesMut, key: &str) -> BytesMut

Appends a formatted value to the provided buffer based on the given key. Handles various metrics including connection info, timing data, and TLS details.

§Arguments
  • buf - The BytesMut buffer to append the value to
  • key - The key identifying which state value to format and append

Returns: The modified BytesMut buffer

Source

pub fn generate_server_timing(&self) -> String

Generates a Server-Timing header value based on the context’s timing metrics.

The Server-Timing header allows servers to communicate performance metrics about the request-response cycle to the client. This implementation includes various timing metrics like connection time, processing time, and cache operations.

Returns a String containing the formatted Server-Timing header value.

Trait Implementations§

Source§

impl Default for Ctx

Source§

fn default() -> Ctx

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Ctx

§

impl !RefUnwindSafe for Ctx

§

impl Send for Ctx

§

impl Sync for Ctx

§

impl Unpin for Ctx

§

impl !UnwindSafe for Ctx

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,