Counter

Struct Counter 

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

Implementation of distributed counters using a JetStream stream.

Implementations§

Source§

impl Counter

Source

pub async fn from_stream(context: Context, stream: Stream) -> Result<Self>

Creates a counter from an existing stream.

Source

pub async fn add<S, V>(&self, subject: S, value: V) -> Result<BigInt>
where S: ToSubject, V: Into<BigInt> + Send,

Adds a value to the counter for the given subject.

Source

pub async fn get<S: ToSubject>(&self, subject: S) -> Result<Entry>

Gets the counter entry for a subject, including sources and last increment.

Returns complete information about a counter including its current value, source breakdown (if using stream sourcing), and the last increment value.

Source

pub async fn load<S: ToSubject>(&self, subject: S) -> Result<BigInt>

Loads just the counter value for a subject.

This is a convenience method that returns only the value, without sources or increment information.

Source

pub async fn increment<S>(&self, subject: S) -> Result<BigInt>
where S: ToSubject,

Increments the counter by 1 for the given subject. This is a convenience method for the common case of incrementing by 1.

Source

pub async fn decrement<S>(&self, subject: S) -> Result<BigInt>
where S: ToSubject,

Decrements the counter by 1 for the given subject. This is a convenience method for the common case of decrementing by 1.

Source

pub async fn get_multiple( &self, subjects: Vec<String>, ) -> Result<Pin<Box<dyn FutureStream<Item = Result<Entry>> + Send + '_>>>

Gets multiple counter entries matching the given subjects.

Efficiently fetches multiple counters in a batch operation. Returns a stream of entries.

Note: Subjects should be exact subjects, not patterns. Non-existent counters are silently skipped in the results.

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> 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