subrpc_core/
endpoint_stats.rs

1use serde::{Deserialize, Serialize};
2
3#[allow(clippy::derived_hash_with_manual_eq)]
4/// Simple stats to help picking the best endpoint
5#[derive(Debug, Default, Deserialize, Serialize, Clone)]
6pub struct EndpointStats {
7	pub failures: u16,
8	pub success: u16,
9	pub latency: f32,
10}
11
12impl std::hash::Hash for EndpointStats {
13	fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
14		self.failures.hash(state);
15		self.success.hash(state);
16	}
17}
18
19impl EndpointStats {
20	pub fn add(&mut self, state: bool, latency: Option<f32>) {
21		if state {
22			self.success += 1;
23			if let Some(l) = latency {
24				self.latency = (l * self.success as f32 + l) / self.success as f32;
25			}
26		} else {
27			self.failures += 1;
28		}
29	}
30
31	pub fn score(&self) -> f32 {
32		(self.success - self.failures) as f32 * 1f32 / self.latency / 10f32
33	}
34}