stratum_server/
session_list.rs

1use crate::{session::Session, ConfigManager, Result};
2use dashmap::DashMap;
3use extended_primitives::Buffer;
4use std::{net::SocketAddr, sync::Arc};
5use tracing::{info, warn};
6
7//@todo performance test using a Sephamore for this similar to how Tokio does it in mini-redis
8
9//@todo would love to get a data structure maybe stratumstats that is just recording all of the
10//data and giving us some fucking baller output. Like shares/sec unit.
11//
12//Maybe total hashrate that we are processing at the moment.
13
14//@todo would be nice to track the number of agent connections on here.
15#[derive(Default, Clone)]
16pub struct SessionList<CState: Clone> {
17    inner: Arc<Inner<CState>>,
18    pub(crate) config_manager: ConfigManager,
19}
20
21#[derive(Default)]
22struct Inner<CState> {
23    state: DashMap<SocketAddr, Session<CState>>,
24}
25
26impl<CState: Clone> SessionList<CState> {
27    #[must_use]
28    pub fn new(config_manager: ConfigManager) -> Self {
29        SessionList {
30            inner: Arc::new(Inner {
31                state: DashMap::new(),
32            }),
33            config_manager,
34        }
35    }
36
37    pub fn add_miner(&self, addr: SocketAddr, miner: Session<CState>) {
38        self.inner.state.insert(addr, miner);
39        // gauge!(
40        //     "stratum.num_connections",
41        //     self.miners.read().await.len() as f64
42        // );
43    }
44
45    pub fn remove_miner(&self, addr: SocketAddr) {
46        self.inner.state.remove(&addr);
47        // gauge!(
48        //     "stratum.num_connections",
49        //     self.miners.read().await.len() as f64
50        // );
51    }
52
53    #[must_use]
54    pub fn get_all_miners(&self) -> Vec<Session<CState>> {
55        self.inner.state.iter().map(|x| x.value().clone()).collect()
56    }
57
58    #[must_use]
59    pub fn len(&self) -> usize {
60        self.inner.state.len()
61    }
62
63    #[must_use]
64    pub fn is_empty(&self) -> bool {
65        self.inner.state.is_empty()
66    }
67
68    #[must_use]
69    pub fn is_full(&self) -> bool {
70        if let Some(max) = self
71            .config_manager
72            .current_config()
73            .connection
74            .max_connections
75        {
76            self.len() >= max
77        } else {
78            false
79        }
80    }
81
82    //@todo we need to revamp this as it needs to be variable.
83    pub fn shutdown_msg(&self, msg: Option<Buffer>) -> Result<()> {
84        // @todo use this for deluge
85        if let Some(msg) = msg {
86            info!(
87                "Session List sending {} miners reconnect message.",
88                self.inner.state.len()
89            );
90            for entry in &self.inner.state {
91                let miner = entry.value();
92                if let Err(e) = miner.send_raw(msg.clone()) {
93                    warn!(connection_id = %miner.id(), cause = %e, "Failed to send shutdown message");
94                }
95            }
96        }
97
98        Ok(())
99    }
100
101    pub fn shutdown(&self) {
102        info!(
103            "Session List shutting down {} miners",
104            self.inner.state.len()
105        );
106
107        //@todo we need to parallize this async - now we can do it without async though.
108        for entry in &self.inner.state {
109            entry.value().shutdown();
110        }
111    }
112}