1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use std::sync::atomic::AtomicUsize;
use std::sync::Arc;

use crate::config::Config;
use crate::gateway::GatewayClient;
use crate::network::P2pClient;
use crate::status::{ClientStatus, ServiceStatus, ServiceType};
use crate::store::StoreClient;
use anyhow::{Context, Result};
use futures::{Stream, StreamExt};

#[derive(Debug, Clone)]
pub struct Client {
    pub gateway: Option<GatewayClient>,
    p2p: P2pLBClient,
    store: StoreLBClient,
}

/// Provides a load balanced client for the store service
/// The client will round robin between all available StoreClients
#[derive(Debug, Clone)]
pub(crate) struct StoreLBClient {
    clients: Vec<StoreClient>,
    pos: Arc<AtomicUsize>,
}

impl Default for StoreLBClient {
    fn default() -> Self {
        Self::new()
    }
}

impl StoreLBClient {
    /// round robin load balancing
    pub fn get(&self) -> Option<StoreClient> {
        if self.clients.is_empty() {
            return None;
        }
        let pos = self.pos.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        let c = self.clients.get(pos % self.clients.len()).unwrap();
        Some(c.clone())
    }

    pub fn new() -> Self {
        Self {
            clients: vec![],
            pos: Arc::new(AtomicUsize::new(0)),
        }
    }
}

/// Provides a load balanced client for the p2p service
/// The client will round robin between all available P2pClients
#[derive(Debug, Clone)]
pub(crate) struct P2pLBClient {
    clients: Vec<P2pClient>,
    pos: Arc<AtomicUsize>,
}

impl Default for P2pLBClient {
    fn default() -> Self {
        Self::new()
    }
}

impl P2pLBClient {
    /// round robin load balancing
    pub fn get(&self) -> Option<P2pClient> {
        if self.clients.is_empty() {
            return None;
        }
        let pos = self.pos.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        let c = self.clients.get(pos % self.clients.len()).unwrap();
        Some(c.clone())
    }

    pub fn new() -> Self {
        Self {
            clients: vec![],
            pos: Arc::new(AtomicUsize::new(0)),
        }
    }
}

impl Client {
    pub async fn new(cfg: Config) -> Result<Self> {
        let Config {
            gateway_addr,
            p2p_addr,
            store_addr,
            channels,
        } = cfg;

        let gateway = if let Some(addr) = gateway_addr {
            Some(
                GatewayClient::new(addr)
                    .await
                    .context("Could not create gateway rpc client")?,
            )
        } else {
            None
        };

        let n_channels = channels.unwrap_or(1);

        let mut p2p = P2pLBClient::new();
        if let Some(addr) = p2p_addr {
            for _i in 0..n_channels {
                let sc = P2pClient::new(addr.clone())
                    .await
                    .context("Could not create store rpc client")?;
                p2p.clients.push(sc);
            }
        }

        let mut store = StoreLBClient::new();
        if let Some(addr) = store_addr {
            for _i in 0..n_channels {
                let sc = StoreClient::new(addr.clone())
                    .await
                    .context("Could not create store rpc client")?;
                store.clients.push(sc);
            }
        }

        Ok(Client {
            gateway,
            p2p,
            store,
        })
    }

    pub fn try_p2p(&self) -> Result<P2pClient> {
        self.p2p.get().context("missing rpc p2p connnection")
    }

    pub fn try_gateway(&self) -> Result<&GatewayClient> {
        self.gateway
            .as_ref()
            .context("missing rpc gateway connnection")
    }

    pub fn try_store(&self) -> Result<StoreClient> {
        self.store.get().context("missing rpc store connection")
    }

    pub async fn check(&self) -> crate::status::ClientStatus {
        let g = if let Some(ref g) = self.gateway {
            let (s, v) = g.check().await;
            Some(ServiceStatus::new(ServiceType::Gateway, s, v))
        } else {
            None
        };
        let p = if let Some(ref p) = self.p2p.get() {
            let (s, v) = p.check().await;
            Some(ServiceStatus::new(ServiceType::P2p, s, v))
        } else {
            None
        };
        let s = if let Some(ref s) = self.store.get() {
            let (s, v) = s.check().await;
            Some(ServiceStatus::new(ServiceType::Store, s, v))
        } else {
            None
        };
        ClientStatus::new(g, p, s)
    }

    pub async fn watch(self) -> impl Stream<Item = ClientStatus> {
        async_stream::stream! {
            let mut status: ClientStatus = Default::default();
            let mut streams = Vec::new();

            if let Some(ref g) = self.gateway {
                let g = g.watch().await;
                let g = g.map(|(status, version)| ServiceStatus::new(ServiceType::Gateway, status, version));
                streams.push(g.boxed());
            }
            if let Some(ref p) = self.p2p.get() {
                let p = p.watch().await;
                let p = p.map(|(status, version)| ServiceStatus::new(ServiceType::P2p, status, version));
                streams.push(p.boxed());
            }
            if let Some(ref s) = self.store.get() {
                let s = s.watch().await;
                let s = s.map(|(status, version)| ServiceStatus::new(ServiceType::Store, status, version));
                streams.push(s.boxed());
            }

            let mut stream = futures::stream::select_all(streams);
            while let Some(s) = stream.next().await {
                status.update(s);
                yield status.clone();
            }
        }
    }
}