pub fn acquire(
        &mut self,
        peer_id: PeerId,
        semaphore: &str,
        amount: u32,
        max: Option<i64>,
    ) -> Result<bool, ThrottleError> {
        let amount = amount as i64;

        // Compare with previous state of the lock. If the same amount for the same semaphore has
        // been demanded already, do nothing.
        let peer = self
            .ledger
            .get(&peer_id)
            .ok_or(ThrottleError::UnknownPeer)?;
        let previous_demand = peer.count_demand(semaphore);
        if previous_demand != 0 {
            match amount.cmp(&previous_demand) {
                // TODO: Reduce lock count and notify if not pending.
                Ordering::Less => return Err(ThrottleError::NotImplemented),
                Ordering::Equal => return Ok(peer.all_acquired()),
                Ordering::Greater => return Err(ThrottleError::Deadlock),
            }
        }

        // Since this creates a new peer, we know it has the most recent timestamp. This implies
        // that due to fairness every other peer has priority over this one taking the lock.
        // Therfore we only take the lock (if a check on max is performed at all), if the total
        // demand is smaller than max.semaphore_service
        //
        // Note: This is different than taking the lock from a semaphore for which the count is
        // smaller than max in situations there e.g. a lock with a count of 5 is pending with while
        // the remainder is 3.
        let acquired = max
            .map(|max| self.demand_smaller_or_equal(semaphore, max - amount))
            .unwrap_or(true);

        self.ledger
            .get_mut(&peer_id)
            .unwrap()
            .add_lock(semaphore.to_owned(), amount, acquired);

        Ok(acquired)
    }
/// Acquires a lock for a peer. If the count of the semaphore is high enough, the lease is going
/// to be acquired, otherwise it remains pending. This method does not block.
///
/// # Parameters
///
/// * `peer_id`: Should this be None, a new peer_id is going to be generated,
///              otherwise the provided one is used.
/// * `max`: If set to `None` the new leasel are always going to be active. This is
///          useful to handle revenat peers with active leaves. If a value is set a
///          check is performed and the lease are only going to be active, if it
///          would not exceed the full counts of the involved semaphores.
///
/// # Return
///
/// Returns `true` if, and only if, all locks of the peer are acquired.
Acquires a lock for a peer. If the count of the semaphore is high enough, the lease is going
to be acquired, otherwise it remains pending. This method does not block.
Acquires a lock for a peer . If the count of the semaphore is high enough the lease is going to be acquired otherwise it remains pending . This method does not block .
pub fn acquire(
        &mut self,
        peer_id: PeerId,
        semaphore: &str,
        amount: u32,
        max: Option<i64>,
    ) -> Result<bool, ThrottleError> {
        let amount = amount as i64;
        // Compare with previous state of the lock. If the same amount for the same semaphore has
        // been demanded already, do nothing.
        let peer = self
            .ledger
            .get(&peer_id)
            .ok_or(ThrottleError::UnknownPeer)?;
        let previous_demand = peer.count_demand(semaphore);
        if previous_demand != 0 {
            match amount.cmp(&previous_demand) {
                // TODO: Reduce lock count and notify if not pending.
                Ordering::Less => return Err(ThrottleError::NotImplemented),
                Ordering::Equal => return Ok(peer.all_acquired()),
                Ordering::Greater => return Err(ThrottleError::Deadlock),
            }
        }
        // Since this creates a new peer, we know it has the most recent timestamp. This implies
        // that due to fairness every other peer has priority over this one taking the lock.
        // Therfore we only take the lock (if a check on max is performed at all), if the total
        // demand is smaller than max.semaphore_service
        //
        // Note: This is different than taking the lock from a semaphore for which the count is
        // smaller than max in situations there e.g. a lock with a count of 5 is pending with while
        // the remainder is 3.
        let acquired = max
            .map(|max| self.demand_smaller_or_equal(semaphore, max - amount))
            .unwrap_or(true);
        self.ledger
            .get_mut(&peer_id)
            .unwrap()
            .add_lock(semaphore.to_owned(), amount, acquired);
        Ok(acquired)
    }
pub fn acquire ( & mut self , peer_id : PeerId , semaphore : & str , amount : u32 , max : Option < i64 > , ) -> Result < bool , ThrottleError > { let amount = amount as i64 ; // Compare with previous state of the lock. If the same amount for the same semaphore has // been demanded already, do nothing. let peer = self . ledger . get ( & peer_id ) . ok_or ( ThrottleError :: UnknownPeer ) ? ; let previous_demand = peer . count_demand ( semaphore ) ; if previous_demand != 0 { match amount . cmp ( & previous_demand ) { // TODO: Reduce lock count and notify if not pending. Ordering :: Less => return Err ( ThrottleError :: NotImplemented ) , Ordering :: Equal => return Ok ( peer . all_acquired ( ) ) , Ordering :: Greater => return Err ( ThrottleError :: Deadlock ) , } } // Since this creates a new peer, we know it has the most recent timestamp. This implies // that due to fairness every other peer has priority over this one taking the lock. // Therfore we only take the lock (if a check on max is performed at all), if the total // demand is smaller than max.semaphore_service // // Note: This is different than taking the lock from a semaphore for which the count is // smaller than max in situations there e.g. a lock with a count of 5 is pending with while // the remainder is 3. let acquired = max . map ( | max | self . demand_smaller_or_equal ( semaphore , max - amount ) ) . unwrap_or ( true ) ; self . ledger . get_mut ( & peer_id ) . unwrap ( ) . add_lock ( semaphore . to_owned ( ) , amount , acquired ) ; Ok ( acquired ) }
Acquires a lock for a peer.
Acquires a lock for a peer .
&mut self
peer_id
PeerId
semaphore
&str
amount
u32
max
Option<i64>
Returns `true` if, and only if, all locks of the peer are acquired.
Returns ` true ` if and only if all locks of the peer are acquired .
&mut self
peer_id
PeerId
semaphore
&str
amount
u32
max
Option<i64>