DefaultLease

Struct DefaultLease 

Source
pub struct DefaultLease { /* private fields */ }
Expand description

Default lease implementation with single-index lock-free architecture.

§Performance Characteristics

  • is_expired(): O(1), ~10-20ns, lock-free
  • register(): O(1), ~20ns, lock-free (single shard write lock)
  • unregister(): O(1), ~20ns, lock-free (single shard write lock)
  • cleanup(): O(N), time-limited, shard read locks (rare: 1/1000 applies)

§Memory Usage

  • Per-key overhead: ~50 bytes (single DashMap entry)
  • Expired keys are removed automatically during cleanup

Implementations§

Source§

impl DefaultLease

Source

pub fn new(config: LeaseConfig) -> DefaultLease

Creates a new default lease manager with the given configuration.

§Arguments
  • config - Lease cleanup strategy configuration
Source

pub fn get_expiration(&self, key: &[u8]) -> Option<SystemTime>

Get expiration time for a specific key.

§Performance

O(1) - DashMap lookup, lock-free

Source

pub fn from_snapshot(data: &[u8], config: LeaseConfig) -> DefaultLease

Restore from snapshot data (used during initialization).

Filters out already-expired keys during restoration.

§Arguments
  • data - Serialized snapshot data
  • config - Lease configuration to use
Source

pub fn config(&self) -> &LeaseConfig

Returns reference to the lease configuration.

Used by StateMachine implementations to check cleanup strategy.

Trait Implementations§

Source§

impl Debug for DefaultLease

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Lease for DefaultLease

Source§

fn register(&self, key: Bytes, ttl_secs: u64)

Register a key with TTL (Time-To-Live).

§TTL Semantics
  • Absolute expiration time: The expiration time is calculated as expire_at = SystemTime::now() + Duration::from_secs(ttl_secs) and stored internally.
  • Crash-safe: The absolute expiration time survives node restarts. After crash recovery, expired keys remain expired (no TTL reset).
  • Persistent: The expiration time is persisted to disk during snapshot generation and graceful shutdown.
§Example
T0:  register(key="foo", ttl=10) → expire_at = T0 + 10 = T10
T5:  CRASH
T12: RESTART
     → WAL replay: expire_at = T10 < T12 (already expired)
     → Key is NOT restored (correctly expired)
§Arguments
  • key - The key to register expiration for
  • ttl_secs - Time-to-live in seconds from now
§Performance

O(1) - Completely lock-free, only acquires single shard write lock

Source§

fn unregister(&self, key: &[u8])

Unregister a key’s TTL.

§Performance

O(1) - Completely lock-free, only acquires single shard write lock

Source§

fn is_expired(&self, key: &[u8]) -> bool

Check if a key has expired.

§Performance

O(1) - Lock-free DashMap lookup

Source§

fn get_expired_keys(&self, now: SystemTime) -> Vec<Bytes>

Get all expired keys (without time limit).

This method is rarely used directly. Most cleanup happens via on_apply().

§Performance

O(N) - Iterates all keys with shard read locks

Source§

fn on_apply(&self) -> Vec<Bytes>

Piggyback cleanup on apply operations (DEPRECATED - no longer used).

This method is kept for backward compatibility but is no longer called. Cleanup is now handled by:

  • Lazy strategy: cleanup in get() method
  • Background strategy: dedicated async task
§Performance
  • Fast path: O(1) - always returns empty vec
Source§

fn has_lease_keys(&self) -> bool

Check if any keys have been registered.

§Performance

O(1) - Single atomic load

Source§

fn may_have_expired_keys(&self, now: SystemTime) -> bool

Quick check if there might be expired keys.

This is a heuristic check - samples first 10 entries. May return false negatives but never false positives.

§Performance

O(1) - Checks first few entries only

Source§

fn len(&self) -> usize

Get total number of keys with active leases.

§Performance

O(1) - DashMap maintains internal count

Source§

fn to_snapshot(&self) -> Vec<u8>

Serialize current lease state to snapshot.

§Performance

O(N) - Iterates all keys with shard read locks

Source§

fn reload(&self, data: &[u8]) -> Result<(), Error>

Reload lease state from snapshot.

Filters out already-expired keys during restoration.

§Performance

O(N) - Rebuilds single index

Source§

fn is_empty(&self) -> bool

Check if no keys have active leases.

Auto Trait Implementations§

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

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
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