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: usizeUnique identifier for the connection, it should be unique among all existing connections of the same type
processing: i32Number of requests currently processing
accepted: u64Total number of requests accepted
location_processing: i32Number of requests currently processing for the current location
location_accepted: u64Total number of requests accepted for the current location
created_at: u64Timestamp 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: u64Total time the connection has been alive (in milliseconds) May be large for reused connections
connection_reused: boolIndicates if this connection is reused
location: StringThe location handling request
upstream_address: StringAddress 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: boolWhether 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: StringThe upstream server
upstream_reused: boolIndicates 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: usizeSize 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
impl Ctx
Sourcepub fn new() -> Self
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.
Sourcepub fn add_variable(&mut self, key: &str, value: &str)
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
Sourcepub fn get_variable(&self, key: &str) -> Option<&str>
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
Sourcepub fn get_upstream_response_time(&self) -> Option<u64>
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
Sourcepub fn get_upstream_connect_time(&self) -> Option<u64>
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
Sourcepub fn get_upstream_processing_time(&self) -> Option<u64>
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
Sourcepub fn add_plugin_processing_time(&mut self, name: &str, time: u32)
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 plugintime- The time taken by the plugin in milliseconds
Sourcepub fn append_value(&self, buf: BytesMut, key: &str) -> BytesMut
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 tokey- The key identifying which state value to format and append
Returns: The modified BytesMut buffer
Sourcepub fn generate_server_timing(&self) -> String
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.