pub struct ScopePool { /* private fields */ }Expand description
A pool of pre-allocated scopes for high-throughput scenarios.
Creating a scope involves allocating a DashMap (~134ns). For web servers handling thousands of requests per second, this adds up. ScopePool pre-allocates scopes and reuses them, reducing per-request overhead to near-zero.
§Example
use dependency_injector::{Container, ScopePool};
#[derive(Clone)]
struct AppConfig { name: String }
#[derive(Clone)]
struct RequestId(String);
// Create root container with app-wide services
let root = Container::new();
root.singleton(AppConfig { name: "MyApp".into() });
// Create a pool of reusable scopes (pre-allocates 4 scopes)
let pool = ScopePool::new(&root, 4);
// In request handler: acquire a pooled scope
{
let scope = pool.acquire();
scope.singleton(RequestId("req-123".into()));
// Can access parent services
assert!(scope.contains::<AppConfig>());
assert!(scope.contains::<RequestId>());
// Scope automatically released when dropped
}
// Next request reuses the same scope allocation
{
let scope = pool.acquire();
// Previous RequestId is cleared, fresh scope
assert!(!scope.contains::<RequestId>());
}§Performance
- First acquisition: ~134ns (creates new scope if pool is empty)
- Subsequent acquisitions: ~20ns (reuses pooled scope)
- Release: ~10ns (clears and returns to pool)
Implementations§
Source§impl ScopePool
impl ScopePool
Sourcepub fn new(parent: &Container, capacity: usize) -> Self
pub fn new(parent: &Container, capacity: usize) -> Self
Create a new scope pool with pre-allocated capacity.
§Arguments
parent- The parent container that scopes will inherit fromcapacity- Number of scopes to pre-allocate
§Example
use dependency_injector::{Container, ScopePool};
let root = Container::new();
// Pre-allocate 8 scopes for concurrent request handling
let pool = ScopePool::new(&root, 8);Sourcepub fn acquire(&self) -> PooledScope<'_>
pub fn acquire(&self) -> PooledScope<'_>
Acquire a scope from the pool.
Returns a PooledScope that automatically returns to the pool when dropped.
If the pool is empty, creates a new scope.
§Example
use dependency_injector::{Container, ScopePool};
#[derive(Clone)]
struct RequestData { id: u64 }
let root = Container::new();
let pool = ScopePool::new(&root, 4);
let scope = pool.acquire();
scope.singleton(RequestData { id: 123 });
let data = scope.get::<RequestData>().unwrap();
assert_eq!(data.id, 123);Sourcepub fn available_count(&self) -> usize
pub fn available_count(&self) -> usize
Get the current number of available scopes in the pool.
Auto Trait Implementations§
impl !Freeze for ScopePool
impl !RefUnwindSafe for ScopePool
impl Send for ScopePool
impl Sync for ScopePool
impl Unpin for ScopePool
impl !UnwindSafe for ScopePool
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Injectable for T
impl<T> Injectable for T
Source§fn type_id_of() -> TypeIdwhere
Self: Sized,
fn type_id_of() -> TypeIdwhere
Self: Sized,
Returns the TypeId of this type (for internal use)
Source§fn type_name_of() -> &'static strwhere
Self: Sized,
fn type_name_of() -> &'static strwhere
Self: Sized,
Returns the type name for debugging