gossip_kv/
membership.rs

1use std::fmt::Debug;
2use std::hash::Hash;
3
4use serde::{Deserialize, Serialize};
5
6pub type MemberId = String;
7
8/// Information about a member in the cluster.
9///
10/// A member is a process that is part of the cluster. Leaving or failing is a terminal
11/// state for a member. When a process restarts and rejoins the cluster, it is considered a
12/// new member.
13///
14/// # Generic Parameters
15/// -- `A`: The transport of the endpoint on which the protocol is running. In production, this will
16/// likely be a `SocketAddr`.
17#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
18pub struct MemberData<A>
19where
20    A: Debug + Clone + Eq + Hash + Serialize,
21{
22    /// The name of the member. Usually, this is a randomly generated identifier, based on the
23    /// hostname on which the member is running.
24    pub id: MemberId,
25
26    /// The protocols that the member supports.
27    pub protocols: Vec<Protocol<A>>,
28}
29
30/// A builder for `MemberData`.
31pub struct MemberDataBuilder<A>
32where
33    A: Debug + Clone + Eq + Hash + Serialize,
34{
35    id: MemberId,
36    protocols: Vec<Protocol<A>>,
37}
38
39impl<A> MemberDataBuilder<A>
40where
41    A: Debug + Clone + Eq + Hash + Serialize,
42{
43    /// Creates a new `MemberDataBuilder`.
44    pub fn new(id: MemberId) -> Self {
45        MemberDataBuilder {
46            id,
47            protocols: Vec::new(),
48        }
49    }
50
51    /// Adds a protocol to the member.
52    pub fn add_protocol(mut self, protocol: Protocol<A>) -> Self {
53        self.protocols.push(protocol);
54        self
55    }
56
57    /// Builds the `MemberData`.
58    pub fn build(self) -> MemberData<A> {
59        MemberData {
60            id: self.id,
61            protocols: self.protocols,
62        }
63    }
64}
65
66/// A protocol supported by a member.
67///
68/// # Generic Parameters
69/// -- `A`: The transport of the endpoint on which the protocol is running. In production, this will
70/// likely be a `SocketAddr`.
71#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
72pub struct Protocol<A> {
73    /// The name of the protocol.
74    pub name: String,
75
76    /// The endpoint on which the protocol is running.
77    pub endpoint: A,
78}
79
80impl<A> Protocol<A> {
81    /// Creates a new `Protocol`.
82    pub fn new(name: String, endpoint: A) -> Self {
83        Protocol { name, endpoint }
84    }
85}