pub async fn acquire(
        &self,
        peer_id: PeerId,
        semaphore: &str,
        amount: u32,
        wait_for: Option<Duration>,
        expires_in: Duration,
    ) -> Result<bool, ThrottleError> {
        let max = *self
            .semaphores
            .get(semaphore)
            .ok_or(ThrottleError::UnknownSemaphore)?;
        // Return early if lease can never be acquired
        if max < amount as i64 {
            return Err(ThrottleError::ForeverPending {
                asked: amount as i64,
                max,
            });
        }
        let mut leases = self.leases.lock().unwrap();
        let valid_until = Instant::now() + expires_in;
        leases.update_valid_until(peer_id, valid_until)?;
        let acquired = leases.acquire(peer_id, semaphore, amount, Some(max))?;
        if acquired {
            // Resolve this immediatly, if we can
            debug!("Peer {} acquired lock to '{}'.", peer_id, semaphore);
            Ok(true)
        } else {
            debug!("Peer {} waiting for lock to '{}'", peer_id, semaphore);

            // We could not acquire the lock immediatly. Are we going to wait for it?
            if let Some(wait_for) = wait_for {
                // keep holding the lock to `leases` until everything is registered. So we don't miss
                // the call to `resolve_with`.

                let acquire_or_timeout =
                    time::timeout(wait_for, self.wakers.wait_for_resolving(peer_id));

                // Release lock on leases, while waiting for acquire_or_timeout! Otherwise, we might
                // deadlock.
                drop(leases);
                // The outer `Err` indicates a timeout.
                match acquire_or_timeout.await {
                    // Locks could be acquired
                    Ok(Ok(())) => {
                        debug!("Peer {} acquired lock to '{}'.", peer_id, semaphore);
                        Ok(true)
                    }
                    // Failure
                    Ok(Err(e)) => Err(e),
                    // Lock could not be acquired in time
                    Err(_) => Ok(false),
                }
            } else {
                Ok(acquired)
            }
        }
    }
/// Sets the lock count for this peer and semaphore to `amount`. Should the remainder of the
/// semaphore allow it.
///
/// * `peer_id`: Identifies the peer for which we are setting the lock count
/// * `semaphore`: Name of the semaphore to which we want to acquire the lock count.
/// * `amount`: The count of the lock. Outside of revenants (i.e. expired peers, which do
/// return). Throttle is goingt to see to it that the combined lock count is not going beyond
/// the configured max count.
/// * `wait_for`: The future 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 future
/// returns immediatly.
/// * `expires_in`: Used to prolong the expiration timestamp of the affected peer. This saves us
/// an extra heartbeat request.
Sets the lock count for this peer and semaphore to `amount`. Should the remainder of the
semaphore allow it.

`peer_id`: Identifies the peer for which we are setting the lock count
`semaphore`: Name of the semaphore to which we want to acquire the lock count.
`amount`: The count of the lock. Outside of revenants . Throttle is goingt to see to it that the combined lock count is not going beyond
the configured max count.
`wait_for`: The future 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 future
returns immediatly.
`expires_in`: Used to prolong the expiration timestamp of the affected peer. This saves us
an extra heartbeat request.
Sets the lock count for this peer and semaphore to ` amount ` . Should the remainder of the semaphore allow it . ` peer_id ` : Identifies the peer for which we are setting the lock count ` semaphore ` : Name of the semaphore to which we want to acquire the lock count . ` amount ` : The count of the lock . Outside of revenants . Throttle is goingt to see to it that the combined lock count is not going beyond the configured max count . ` wait_for ` : The future 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 future returns immediatly . ` expires_in ` : Used to prolong the expiration timestamp of the affected peer . This saves us an extra heartbeat request .
pub async fn acquire(
        &self,
        peer_id: PeerId,
        semaphore: &str,
        amount: u32,
        wait_for: Option<Duration>,
        expires_in: Duration,
    ) -> Result<bool, ThrottleError> {
        let max = *self
            .semaphores
            .get(semaphore)
            .ok_or(ThrottleError::UnknownSemaphore)?;
        // Return early if lease can never be acquired
        if max < amount as i64 {
            return Err(ThrottleError::ForeverPending {
                asked: amount as i64,
                max,
            });
        }
        let mut leases = self.leases.lock().unwrap();
        let valid_until = Instant::now() + expires_in;
        leases.update_valid_until(peer_id, valid_until)?;
        let acquired = leases.acquire(peer_id, semaphore, amount, Some(max))?;
        if acquired {
            // Resolve this immediatly, if we can
            debug!("Peer {} acquired lock to '{}'.", peer_id, semaphore);
            Ok(true)
        } else {
            debug!("Peer {} waiting for lock to '{}'", peer_id, semaphore);
            // We could not acquire the lock immediatly. Are we going to wait for it?
            if let Some(wait_for) = wait_for {
                // keep holding the lock to `leases` until everything is registered. So we don't miss
                // the call to `resolve_with`.
                let acquire_or_timeout =
                    time::timeout(wait_for, self.wakers.wait_for_resolving(peer_id));
                // Release lock on leases, while waiting for acquire_or_timeout! Otherwise, we might
                // deadlock.
                drop(leases);
                // The outer `Err` indicates a timeout.
                match acquire_or_timeout.await {
                    // Locks could be acquired
                    Ok(Ok(())) => {
                        debug!("Peer {} acquired lock to '{}'.", peer_id, semaphore);
                        Ok(true)
                    }
                    // Failure
                    Ok(Err(e)) => Err(e),
                    // Lock could not be acquired in time
                    Err(_) => Ok(false),
                }
            } else {
                Ok(acquired)
            }
        }
    }
pub async fn acquire ( & self , peer_id : PeerId , semaphore : & str , amount : u32 , wait_for : Option < Duration > , expires_in : Duration , ) -> Result < bool , ThrottleError > { let max = * self . semaphores . get ( semaphore ) . ok_or ( ThrottleError :: UnknownSemaphore ) ? ; // Return early if lease can never be acquired if max < amount as i64 { return Err ( ThrottleError :: ForeverPending { asked : amount as i64 , max , } ) ; } let mut leases = self . leases . lock ( ) . unwrap ( ) ; let valid_until = Instant :: now ( ) + expires_in ; leases . update_valid_until ( peer_id , valid_until ) ? ; let acquired = leases . acquire ( peer_id , semaphore , amount , Some ( max ) ) ? ; if acquired { // Resolve this immediatly, if we can debug ! ( " " peer_id semaphore ) ; Ok ( true ) } else { debug ! ( " " peer_id semaphore ) ; // We could not acquire the lock immediatly. Are we going to wait for it? if let Some ( wait_for ) = wait_for { // keep holding the lock to `leases` until everything is registered. So we don't miss // the call to `resolve_with`. let acquire_or_timeout = time :: timeout ( wait_for , self . wakers . wait_for_resolving ( peer_id ) ) ; // Release lock on leases, while waiting for acquire_or_timeout! Otherwise, we might // deadlock. drop ( leases ) ; // The outer `Err` indicates a timeout. match acquire_or_timeout . await { // Locks could be acquired Ok ( Ok ( ( ) ) ) => { debug ! ( " " peer_id semaphore ) ; Ok ( true ) } // Failure Ok ( Err ( e ) ) => Err ( e ) , // Lock could not be acquired in time Err ( _ ) => Ok ( false ) , } } else { Ok ( acquired ) } } }
Sets the lock count for this peer and semaphore to `amount`.
Sets the lock count for this peer and semaphore to ` amount ` .
&self
peer_id
PeerId
semaphore
&str
amount
u32
wait_for
Option<Duration>
expires_in
Duration
&self
peer_id
PeerId
semaphore
&str
amount
u32
wait_for
Option<Duration>
expires_in
Duration