fn select_p2c_peer_from_list(&self, ready_service_list: &HashSet<D::Key>) -> Option<D::Key> {
        match ready_service_list.len() {
            0 => None,
            1 => Some(
                *ready_service_list
                    .iter()
                    .next()
                    .expect("just checked there is one service"),
            ),
            len => {
                // If there are only 2 peers, randomise their order.
                // Otherwise, choose 2 random peers in a random order.
                let (a, b) = {
                    let idxs = rand::seq::index::sample(&mut rand::thread_rng(), len, 2);
                    let a = idxs.index(0);
                    let b = idxs.index(1);

                    let a = *ready_service_list
                        .iter()
                        .nth(a)
                        .expect("sample returns valid indexes");
                    let b = *ready_service_list
                        .iter()
                        .nth(b)
                        .expect("sample returns valid indexes");

                    (a, b)
                };

                let a_load = self.query_load(&a).expect("supplied services are ready");
                let b_load = self.query_load(&b).expect("supplied services are ready");

                let selected = if a_load <= b_load { a } else { b };

                trace!(
                    a.key = ?a,
                    a.load = ?a_load,
                    b.key = ?b,
                    b.load = ?b_load,
                    selected = ?selected,
                    ?len,
                    "selected service by p2c"
                );

                Some(selected)
            }
        }
    }
/// Performs P2C on `ready_service_list` to randomly select a less-loaded ready service.
Performs P2C on `ready_service_list` to randomly select a less-loaded ready service.
Performs P2C on ` ready_service_list ` to randomly select a less - loaded ready service .
fn select_p2c_peer_from_list(&self, ready_service_list: &HashSet<D::Key>) -> Option<D::Key> {
        match ready_service_list.len() {
            0 => None,
            1 => Some(
                *ready_service_list
                    .iter()
                    .next()
                    .expect("just checked there is one service"),
            ),
            len => {
                // If there are only 2 peers, randomise their order.
                // Otherwise, choose 2 random peers in a random order.
                let (a, b) = {
                    let idxs = rand::seq::index::sample(&mut rand::thread_rng(), len, 2);
                    let a = idxs.index(0);
                    let b = idxs.index(1);
                    let a = *ready_service_list
                        .iter()
                        .nth(a)
                        .expect("sample returns valid indexes");
                    let b = *ready_service_list
                        .iter()
                        .nth(b)
                        .expect("sample returns valid indexes");
                    (a, b)
                };
                let a_load = self.query_load(&a).expect("supplied services are ready");
                let b_load = self.query_load(&b).expect("supplied services are ready");
                let selected = if a_load <= b_load { a } else { b };
                trace!(
                    a.key = ?a,
                    a.load = ?a_load,
                    b.key = ?b,
                    b.load = ?b_load,
                    selected = ?selected,
                    ?len,
                    "selected service by p2c"
                );
                Some(selected)
            }
        }
    }
fn select_p2c_peer_from_list ( & self , ready_service_list : & HashSet < D :: Key > ) -> Option < D :: Key > { match ready_service_list . len ( ) { 0 => None , 1 => Some ( * ready_service_list . iter ( ) . next ( ) . expect ( " " ) , ) , len => { // If there are only 2 peers, randomise their order. // Otherwise, choose 2 random peers in a random order. let ( a , b ) = { let idxs = rand :: seq :: index :: sample ( & mut rand :: thread_rng ( ) , len , 2 ) ; let a = idxs . index ( 0 ) ; let b = idxs . index ( 1 ) ; let a = * ready_service_list . iter ( ) . nth ( a ) . expect ( " " ) ; let b = * ready_service_list . iter ( ) . nth ( b ) . expect ( " " ) ; ( a , b ) } ; let a_load = self . query_load ( & a ) . expect ( " " ) ; let b_load = self . query_load ( & b ) . expect ( " " ) ; let selected = if a_load <= b_load { a } else { b } ; trace ! ( a key a a load a_load b key b b load b_load selected selected len " " ) ; Some ( selected ) } } }
Performs P2C on `ready_service_list` to randomly select a less-loaded ready service.
Performs P2C on ` ready_service_list ` to randomly select a less - loaded ready service .
&self
ready_service_list
&HashSet<D::Key>