Struct throttle_client::Client

source ·
pub struct Client { /* private fields */ }
Expand description

Send http requests to a throttle server. Only concerned with sending correct HTTP requests the Throttle server understands. Not a higher level locking library.

Implementations§

source§

impl Client

source

pub fn new(url: impl IntoUrl) -> Result<Self, Error>

A new throttle Client instance.

source

pub async fn new_peer(&self, expires_in: Duration) -> Result<u64, Error>

Register a new peer with the server.

§Parameters
  • expires_in: Retention time of the peer on the server. A peer is used to acquire locks and keep the leases to them alive. A Peer owns the locks which it acquires and releasing it is going to release the owned locks as well.

Every call to new_peer should be matched by a call to release.

Creating a peer new_peer is separated from acquire in an extra Request mostly to make acquire idempotent. This prevents a call to acquire from acquiring more than one semaphore in case it is repeated due to a timeout.

source

pub async fn release(&self, peer_id: u64) -> Result<(), Error>

Deletes the peer on the throttle server.

This is important to unblock other clients which may be waiting for the semaphore remainder to increase.

source

pub async fn acquire( &self, peer_id: u64, semaphore: &str, count: u32, expires_in: Option<Duration>, block_for: Option<Duration> ) -> Result<bool, Error>

Acquire a lock from the server.

Every call to acquire should be matched by a call to release. Check out lock which as contextmanager does this for you.

§Parameters
  • semaphore: Name of the semaphore to be acquired.
  • count: The count of the lock. A larger count represents a larger ‘piece’ of the resource under procection.
  • block_for: The request returns as soon as the lock could be acquireod or after the duration has elapsed, even if the lock could not be acquired. If set to None, the request returns immediatly. Please note that this function is asynchronous and does not block. The blocking does refer to the actual request. As such block_for represents an upper bound after which the returned futures poll method is going to return Ready.
  • expires_in: The amount of time the remains valid. Can be prolonged by calling heartbeat. After the time has passed the lock is considered released on the server side.
§Return

true if the lock is active, false otherwise.

source

pub async fn is_acquired(&self, peer_id: u64) -> Result<bool, Error>

Ask the server wether the peer has acquired all its locks.

source

pub async fn remainder(&self, semaphore: &str) -> Result<i64, Error>

The curent semaphore count. I.e. the number of available leases

This is equal to the full semaphore count minus the current count. This number could become negative, if the semaphores have been overcommitted (due to previously reoccuring leases previously considered dead).

source

pub async fn restore( &self, peer_id: u64, acquired: &HashMap<String, u32>, expires_in: Duration ) -> Result<(), Error>

Sends a restore request to the server.

This request creates a peer with the specified peer id. The peer is also going to hold all the locks passed in acquired, even if this means exceeding the semaphore count.

source

pub async fn heartbeat( &self, peer_id: u64, expires_in: Duration ) -> Result<(), Error>

Send a PUT request to the server updating the expiration timestamp

source

pub async fn release_lock( &self, peer_id: u64, semaphore: &str ) -> Result<(), Error>

Release a lock to a semaphore for a specific peer

Trait Implementations§

source§

impl Clone for Client

source§

fn clone(&self) -> Client

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Client

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Client

§

impl !RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl !UnwindSafe for Client

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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

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

§

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