pub fn is_banned(&self, address: &Address, shard: usize, role: Option<Role>) -> bool {
        let replicas_available = match role {
            Some(Role::Replica) => self.addresses[shard]
                .iter()
                .filter(|addr| addr.role == Role::Replica)
                .count(),
            None => self.addresses[shard].len(),
            Some(Role::Primary) => return false, // Primary cannot be banned.
        };

        debug!("Available targets for {:?}: {}", role, replicas_available);

        let guard = self.banlist.read();

        // Everything is banned = nothing is banned.
        if guard[shard].len() == replicas_available {
            drop(guard);
            let mut guard = self.banlist.write();
            guard[shard].clear();
            drop(guard);
            warn!("Unbanning all replicas.");
            return false;
        }

        // I expect this to miss 99.9999% of the time.
        match guard[shard].get(address) {
            Some(timestamp) => {
                let now = chrono::offset::Utc::now().naive_utc();
                let config = get_config();

                // Ban expired.
                if now.timestamp() - timestamp.timestamp() > config.general.ban_time {
                    drop(guard);
                    warn!("Unbanning {:?}", address);
                    let mut guard = self.banlist.write();
                    guard[shard].remove(address);
                    false
                } else {
                    debug!("{:?} is banned", address);
                    true
                }
            }

            None => {
                debug!("{:?} is ok", address);
                false
            }
        }
    }
/// Check if a replica can serve traffic. If all replicas are banned,
/// we unban all of them. Better to try then not to.
Check if a replica can serve traffic. If all replicas are banned,
we unban all of them. Better to try then not to.
Check if a replica can serve traffic . If all replicas are banned we unban all of them . Better to try then not to .
pub fn is_banned(&self, address: &Address, shard: usize, role: Option<Role>) -> bool {
        let replicas_available = match role {
            Some(Role::Replica) => self.addresses[shard]
                .iter()
                .filter(|addr| addr.role == Role::Replica)
                .count(),
            None => self.addresses[shard].len(),
            Some(Role::Primary) => return false, // Primary cannot be banned.
        };
        debug!("Available targets for {:?}: {}", role, replicas_available);
        let guard = self.banlist.read();
        // Everything is banned = nothing is banned.
        if guard[shard].len() == replicas_available {
            drop(guard);
            let mut guard = self.banlist.write();
            guard[shard].clear();
            drop(guard);
            warn!("Unbanning all replicas.");
            return false;
        }
        // I expect this to miss 99.9999% of the time.
        match guard[shard].get(address) {
            Some(timestamp) => {
                let now = chrono::offset::Utc::now().naive_utc();
                let config = get_config();
                // Ban expired.
                if now.timestamp() - timestamp.timestamp() > config.general.ban_time {
                    drop(guard);
                    warn!("Unbanning {:?}", address);
                    let mut guard = self.banlist.write();
                    guard[shard].remove(address);
                    false
                } else {
                    debug!("{:?} is banned", address);
                    true
                }
            }
            None => {
                debug!("{:?} is ok", address);
                false
            }
        }
    }
pub fn is_banned ( & self , address : & Address , shard : usize , role : Option < Role > ) -> bool { let replicas_available = match role { Some ( Role :: Replica ) => self . addresses [ shard ] . iter ( ) . filter ( | addr | addr . role == Role :: Replica ) . count ( ) , None => self . addresses [ shard ] . len ( ) , Some ( Role :: Primary ) => return false , // Primary cannot be banned. } ; debug ! ( " " role replicas_available ) ; let guard = self . banlist . read ( ) ; // Everything is banned = nothing is banned. if guard [ shard ] . len ( ) == replicas_available { drop ( guard ) ; let mut guard = self . banlist . write ( ) ; guard [ shard ] . clear ( ) ; drop ( guard ) ; warn ! ( " " ) ; return false ; } // I expect this to miss 99.9999% of the time. match guard [ shard ] . get ( address ) { Some ( timestamp ) => { let now = chrono :: offset :: Utc :: now ( ) . naive_utc ( ) ; let config = get_config ( ) ; // Ban expired. if now . timestamp ( ) - timestamp . timestamp ( ) > config . general . ban_time { drop ( guard ) ; warn ! ( " " address ) ; let mut guard = self . banlist . write ( ) ; guard [ shard ] . remove ( address ) ; false } else { debug ! ( " " address ) ; true } } None => { debug ! ( " " address ) ; false } } }
Check if a replica can serve traffic.
Check if a replica can serve traffic .
&self
address
&Address
shard
usize
role
Option<Role>
&self
address
&Address
shard
usize
role
Option<Role>