Skip to main content

ConnectionTracker

Struct ConnectionTracker 

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

Tracks the per-server NNTP connection budget. Each server has a tokio::sync::Semaphore whose initial permit count is the configured connections limit. Workers acquire a permit before connecting and hold it for their entire lifetime; the permit’s Drop releases the slot synchronously back to the pool. This makes over-allocation a type-level impossibility — once limit permits are out, the next acquire awaits.

Limit changes at runtime are handled differently for grow vs shrink:

  • Grow (e.g. 5 → 10): the existing semaphore is given the additional permits via add_permits; existing slot holders are unaffected.
  • Shrink (e.g. 10 → 5): the entire ServerSlot is replaced with a fresh one. Old permit holders continue to reference the orphaned semaphore via their Arc; their drops release back to it (a no-op since nothing else points at it). Workers detect the replacement via ConnectionTracker::slot_is_current and exit on the next iteration.

Implementations§

Source§

impl ConnectionTracker

Source

pub fn new() -> Self

Source

pub fn set_limit(&self, server_id: &str, server_name: &str, limit: usize)

Set or update the per-server connection limit.

  • First call for a server_id: creates a fresh semaphore with limit permits.
  • Subsequent call with the same limit and server_name: no-op.
  • Grow (limit > current): adds permits in place.
  • Shrink or rename: replaces the slot. Old permit holders detach naturally via [slot_is_current].
Source

pub fn remove_server(&self, server_id: &str)

Forget a server entirely (e.g. on update_servers removing it). Existing permit holders are unaffected; they will detect that their slot is no longer current and exit.

Source

pub async fn acquire(&self, server_id: &str) -> Option<ConnectionSlot>

Acquire a connection slot for server_id. Awaits if the pool is at the limit. Returns None if the server isn’t registered or its limit is zero.

Source

pub fn slot_is_current(&self, slot: &ConnectionSlot) -> bool

Returns true if slot was acquired from the current semaphore for its server. False if the limit was changed (semaphore replaced) or the server was removed — the worker should exit at its next safe checkpoint.

Source

pub fn slot_status(&self, slot: &ConnectionSlot) -> SlotStatus

Like [slot_is_current] but distinguishes the reason a slot is no longer current — useful for diagnostics on the worker exit path.

Source

pub fn snapshot(&self) -> Vec<(String, usize, usize)>

(server_id, active, limit) triples for the live pool. active is derived from the semaphore’s available permits and is always <= limit by construction.

Source

pub fn total(&self) -> usize

Total currently-held permits across all servers in the live pool. Permits held against orphaned (replaced) semaphores are NOT counted — they’ll go away as the holding workers exit.

Trait Implementations§

Source§

impl Default for ConnectionTracker

Source§

fn default() -> Self

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

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

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. 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