libp2p_kad/query/peers.rs
1// Copyright 2019 Parity Technologies (UK) Ltd.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the "Software"),
5// to deal in the Software without restriction, including without limitation
6// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7// and/or sell copies of the Software, and to permit persons to whom the
8// Software is furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19// DEALINGS IN THE SOFTWARE.
20
21//! Peer selection strategies for queries in the form of iterator-like state machines.
22//!
23//! Using a peer iterator in a query involves performing the following steps
24//! repeatedly and in an alternating fashion:
25//!
26//! 1. Calling `next` to observe the next state of the iterator and determine what to do, which is
27//! to either issue new requests to peers or continue waiting for responses.
28//!
29//! 2. When responses are received or requests fail, providing input to the iterator via the
30//! `on_success` and `on_failure` callbacks, respectively, followed by repeating step (1).
31//!
32//! When a call to `next` returns [`Finished`], no more peers can be obtained
33//! from the iterator and the results can be obtained from `into_result`.
34//!
35//! A peer iterator can be finished prematurely at any time through `finish`.
36//!
37//! [`Finished`]: PeersIterState::Finished
38
39pub(crate) mod closest;
40pub(crate) mod fixed;
41use std::borrow::Cow;
42
43use libp2p_identity::PeerId;
44
45/// The state of a peer iterator.
46#[derive(Debug, Clone, PartialEq, Eq)]
47pub enum PeersIterState<'a> {
48 /// The iterator is waiting for results.
49 ///
50 /// `Some(peer)` indicates that the iterator is now waiting for a result
51 /// from `peer`, in addition to any other peers for which it is already
52 /// waiting for results.
53 ///
54 /// `None` indicates that the iterator is waiting for results and there is no
55 /// new peer to contact, despite the iterator not being at capacity w.r.t.
56 /// the permitted parallelism.
57 Waiting(Option<Cow<'a, PeerId>>),
58
59 /// The iterator is waiting for results and is at capacity w.r.t. the
60 /// permitted parallelism.
61 WaitingAtCapacity,
62
63 /// The iterator finished.
64 Finished,
65}