Skip to main content

ConnectionReusePolicy

Trait ConnectionReusePolicy 

pub trait ConnectionReusePolicy:
    Debug
    + Send
    + Sync
    + 'static {
    // Required methods
    fn classifier(&self) -> ReuseKey;
    fn connection_key(&self) -> Option<ReuseKey>;
    fn request_key(&self, input: &Extensions) -> Option<ReuseKey>;
}
Available on crate feature net only.
Expand description

Connection-owned requirements for reusing an established transport.

A connection serves a request whose key equals the connection’s key. Pools derive a request’s key once per classifier and index connections by key, so a checkout does not test each pooled connection. Methods must be deterministic, nonblocking and side-effect free; pools call them outside their locks.

A requirement beyond the pool’s connection id maps to a cheap exact key, such as ReuseKey::from_bits::<Tenant>(id) for a tenant, ReuseKey::from_arc of a shared authenticated identity, or ReuseKey::new of a pinned SPKI digest:

use rama_core::extensions::{Extension, Extensions};
use rama_net::client::pool::{ConnectionReuse, ConnectionReusePolicy, ReuseKey};

#[derive(Debug, Clone, Copy, Extension)]
struct Tenant(u64);

/// A connection authenticated for a tenant only serves that tenant.
#[derive(Debug)]
struct TenantReuse(Tenant);

impl ConnectionReusePolicy for TenantReuse {
    fn classifier(&self) -> ReuseKey {
        ReuseKey::of::<Self>()
    }

    fn connection_key(&self) -> Option<ReuseKey> {
        Some(ReuseKey::from_bits::<Tenant>(self.0.0.into()))
    }

    fn request_key(&self, input: &Extensions) -> Option<ReuseKey> {
        let tenant = input.get_ref::<Tenant>()?;
        Some(ReuseKey::from_bits::<Tenant>(tenant.0.into()))
    }
}

let reuse = ConnectionReuse::new(TenantReuse(Tenant(7)));
let request = Extensions::new();
request.insert(Tenant(7));
assert!(reuse.matches(&request));
request.insert(Tenant(8));
assert!(!reuse.matches(&request));

Required Methods§

fn classifier(&self) -> ReuseKey

Identity of Self::request_key: policies with equal classifiers must derive equal keys for every request.

fn connection_key(&self) -> Option<ReuseKey>

This connection’s key, captured when it was established, or None if the connection must not be retained after its establishing request.

fn request_key(&self, input: &Extensions) -> Option<ReuseKey>

The key a connection needs to serve input, or None if no connection of this classifier may serve it.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§