Skip to main content

radicle_protocol/fetcher/state/
event.rs

1use std::collections::{BTreeMap, VecDeque};
2use std::time;
3
4use radicle_core::{NodeId, RepoId};
5
6use crate::fetcher::RefsToFetch;
7
8use super::{ActiveFetch, QueuedFetch};
9
10/// Event returned from [`FetchState::handle`].
11///
12/// [`FetchState::handle`]: FetchState::handle.
13#[derive(Clone, Debug, PartialEq, Eq)]
14pub enum Event {
15    Fetch(Fetch),
16    Fetched(Fetched),
17    Cancel(Cancel),
18}
19
20impl From<Cancel> for Event {
21    fn from(v: Cancel) -> Self {
22        Self::Cancel(v)
23    }
24}
25
26impl From<Fetched> for Event {
27    fn from(v: Fetched) -> Self {
28        Self::Fetched(v)
29    }
30}
31
32impl From<Fetch> for Event {
33    fn from(v: Fetch) -> Self {
34        Self::Fetch(v)
35    }
36}
37
38/// Events that occur when a repository is requested to be fetched.
39#[derive(Clone, Debug, PartialEq, Eq)]
40pub enum Fetch {
41    /// The fetch can be started by the caller.
42    Started {
43        /// The repository to be fetched.
44        rid: RepoId,
45        /// The node to fetch from.
46        from: NodeId,
47        /// The references to be fetched.
48        refs: RefsToFetch,
49        /// The timeout for the fetch process.
50        timeout: time::Duration,
51    },
52    /// The repository is already being fetched from the given node.
53    AlreadyFetching {
54        /// The repository being actively fetched.
55        rid: RepoId,
56        /// The node being fetched from.
57        from: NodeId,
58    },
59    /// The queue for the given node is at capacity, and can no longer accept
60    /// any more fetch requests.
61    QueueAtCapacity {
62        /// The rejected repository.
63        rid: RepoId,
64        /// The node who's queue is at capacity.
65        from: NodeId,
66        /// The references expected to be fetched.
67        refs: RefsToFetch,
68        /// The timeout for the fetch process.
69        timeout: time::Duration,
70        /// The capacity of the queue.
71        capacity: usize,
72    },
73    /// The fetch was queued for later processing.
74    Queued {
75        /// The repository to be fetched.
76        rid: RepoId,
77        /// The node to fetch from.
78        from: NodeId,
79    },
80}
81
82/// Events that occur after a repository has been fetched.
83#[derive(Clone, Debug, PartialEq, Eq)]
84pub enum Fetched {
85    /// There was no ongoing fetch for the given [`NodeId`] and [`RepoId`].
86    NotFound { from: NodeId, rid: RepoId },
87    /// The active fetch was marked as completed and removed from the active
88    /// set.
89    Completed {
90        /// The node the repository was fetched from.
91        from: NodeId,
92        /// The repository that was fetched.
93        rid: RepoId,
94        /// The references that were fetched.
95        refs: RefsToFetch,
96    },
97}
98
99/// Events that occur when a fetch was canceled for a given node.
100#[derive(Clone, Debug, PartialEq, Eq)]
101pub enum Cancel {
102    /// There were no active or queued fetches for the given node.
103    Unexpected { from: NodeId },
104    /// The were active or queued fetches that were canceled for the given node.
105    Canceled {
106        /// The node which was canceled.
107        from: NodeId,
108        /// The active fetches that were canceled.
109        active: BTreeMap<RepoId, ActiveFetch>,
110        /// The queued fetched that were canceled.
111        queued: VecDeque<QueuedFetch>,
112    },
113}