Skip to main content

moq_net/stats/
outcomes.rs

1//! Fixed session-outcome counters shared by embedders and relay metrics.
2
3use serde::{Deserialize, Serialize};
4use std::sync::atomic::{AtomicU64, Ordering};
5
6/// The reason a session was refused.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8#[non_exhaustive]
9pub enum RefusalKind {
10	/// The admission decider refused the session.
11	Refused,
12	/// The admission decider could not answer.
13	Unavailable,
14	/// The admission request cannot be decided.
15	Request,
16	/// The grant covers none of the requested operations.
17	Forbidden,
18	/// A membership proof was missing or invalid.
19	Membership,
20	/// The verified credential expired before admission.
21	Expired,
22}
23
24impl RefusalKind {
25	/// Every currently defined kind, in display order.
26	pub const ALL: &'static [Self] = &[
27		Self::Refused,
28		Self::Unavailable,
29		Self::Request,
30		Self::Forbidden,
31		Self::Membership,
32		Self::Expired,
33	];
34	/// The stable stats and metrics name.
35	pub const fn as_str(self) -> &'static str {
36		match self {
37			Self::Refused => "refused",
38			Self::Unavailable => "unavailable",
39			Self::Request => "request",
40			Self::Forbidden => "forbidden",
41			Self::Membership => "membership",
42			Self::Expired => "expired",
43		}
44	}
45}
46
47/// Sessions refused before admission, with one cumulative count per kind.
48#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
49#[serde(default)]
50#[non_exhaustive]
51pub struct Refusals {
52	/// The admission decider refused the session.
53	pub refused: u64,
54	/// The admission decider could not answer.
55	pub unavailable: u64,
56	/// The admission request cannot be decided.
57	pub request: u64,
58	/// The grant covers none of the requested operations.
59	pub forbidden: u64,
60	/// A membership proof was missing or invalid.
61	pub membership: u64,
62	/// The verified credential expired before admission.
63	pub expired: u64,
64}
65
66impl Refusals {
67	/// The cumulative count for this kind.
68	pub fn count(&self, kind: RefusalKind) -> u64 {
69		match kind {
70			RefusalKind::Refused => self.refused,
71			RefusalKind::Unavailable => self.unavailable,
72			RefusalKind::Request => self.request,
73			RefusalKind::Forbidden => self.forbidden,
74			RefusalKind::Membership => self.membership,
75			RefusalKind::Expired => self.expired,
76		}
77	}
78	/// Add every kind from another readout.
79	pub fn add(&mut self, other: Self) {
80		self.refused = self.refused.saturating_add(other.refused);
81		self.unavailable = self.unavailable.saturating_add(other.unavailable);
82		self.request = self.request.saturating_add(other.request);
83		self.forbidden = self.forbidden.saturating_add(other.forbidden);
84		self.membership = self.membership.saturating_add(other.membership);
85		self.expired = self.expired.saturating_add(other.expired);
86	}
87	/// Whether any cumulative count went backwards.
88	pub fn regressed(&self, previous: &Self) -> bool {
89		self.refused < previous.refused
90			|| self.unavailable < previous.unavailable
91			|| self.request < previous.request
92			|| self.forbidden < previous.forbidden
93			|| self.membership < previous.membership
94			|| self.expired < previous.expired
95	}
96}
97
98#[derive(Debug, Default)]
99pub(super) struct RefusalCounters {
100	refused: AtomicU64,
101	unavailable: AtomicU64,
102	request: AtomicU64,
103	forbidden: AtomicU64,
104	membership: AtomicU64,
105	expired: AtomicU64,
106}
107
108impl RefusalCounters {
109	pub(super) fn counter(&self, kind: RefusalKind) -> &AtomicU64 {
110		match kind {
111			RefusalKind::Refused => &self.refused,
112			RefusalKind::Unavailable => &self.unavailable,
113			RefusalKind::Request => &self.request,
114			RefusalKind::Forbidden => &self.forbidden,
115			RefusalKind::Membership => &self.membership,
116			RefusalKind::Expired => &self.expired,
117		}
118	}
119	pub(super) fn snapshot(&self) -> Refusals {
120		Refusals {
121			refused: self.refused.load(Ordering::Relaxed),
122			unavailable: self.unavailable.load(Ordering::Relaxed),
123			request: self.request.load(Ordering::Relaxed),
124			forbidden: self.forbidden.load(Ordering::Relaxed),
125			membership: self.membership.load(Ordering::Relaxed),
126			expired: self.expired.load(Ordering::Relaxed),
127		}
128	}
129}
130
131/// The reason a session ended abnormally.
132#[derive(Debug, Clone, Copy, PartialEq, Eq)]
133#[non_exhaustive]
134pub enum Failure {
135	/// The transport reached its idle deadline.
136	IdleTimeout,
137	/// The transport failed.
138	Transport,
139	/// The application or protocol failed.
140	Application,
141	/// The session credential expired.
142	Expired,
143}
144
145impl Failure {
146	/// How a session that ended with `err` failed, or `None` for a clean end: anything the
147	/// peer is told is "no error", by either side, and a drain that reached its deadline,
148	/// which is the draining side ending it on purpose.
149	#[cfg(any(feature = "async", test))]
150	pub(crate) fn of(err: &crate::Error) -> Option<Self> {
151		use crate::{Error, SessionError};
152		match err {
153			Error::IdleTimeout => Some(Self::IdleTimeout),
154			Error::Transport(_) => Some(Self::Transport),
155			_ => match SessionError::from(err) {
156				SessionError::Cancel | SessionError::GoawayTimeout => None,
157				SessionError::Expired => Some(Self::Expired),
158				_ => Some(Self::Application),
159			},
160		}
161	}
162
163	/// Every currently defined kind, in display order.
164	pub const ALL: &'static [Self] = &[Self::IdleTimeout, Self::Transport, Self::Application, Self::Expired];
165	/// The stable stats and metrics name.
166	pub const fn as_str(self) -> &'static str {
167		match self {
168			Self::IdleTimeout => "idle_timeout",
169			Self::Transport => "transport",
170			Self::Application => "application",
171			Self::Expired => "expired",
172		}
173	}
174}
175
176/// Admitted sessions ending abnormally, with one cumulative count per kind.
177#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
178#[serde(default)]
179#[non_exhaustive]
180pub struct Failures {
181	/// The transport reached its idle deadline.
182	pub idle_timeout: u64,
183	/// The transport failed.
184	pub transport: u64,
185	/// The application or protocol failed.
186	pub application: u64,
187	/// The session credential expired.
188	pub expired: u64,
189}
190
191impl Failures {
192	/// The cumulative count for this kind.
193	pub fn count(&self, kind: Failure) -> u64 {
194		match kind {
195			Failure::IdleTimeout => self.idle_timeout,
196			Failure::Transport => self.transport,
197			Failure::Application => self.application,
198			Failure::Expired => self.expired,
199		}
200	}
201	/// Add every kind from another readout.
202	pub fn add(&mut self, other: Self) {
203		self.idle_timeout = self.idle_timeout.saturating_add(other.idle_timeout);
204		self.transport = self.transport.saturating_add(other.transport);
205		self.application = self.application.saturating_add(other.application);
206		self.expired = self.expired.saturating_add(other.expired);
207	}
208	/// Whether any cumulative count went backwards.
209	pub fn regressed(&self, previous: &Self) -> bool {
210		self.idle_timeout < previous.idle_timeout
211			|| self.transport < previous.transport
212			|| self.application < previous.application
213			|| self.expired < previous.expired
214	}
215}
216
217#[derive(Debug, Default)]
218pub(super) struct FailureCounters {
219	idle_timeout: AtomicU64,
220	transport: AtomicU64,
221	application: AtomicU64,
222	expired: AtomicU64,
223}
224
225impl FailureCounters {
226	pub(super) fn counter(&self, kind: Failure) -> &AtomicU64 {
227		match kind {
228			Failure::IdleTimeout => &self.idle_timeout,
229			Failure::Transport => &self.transport,
230			Failure::Application => &self.application,
231			Failure::Expired => &self.expired,
232		}
233	}
234	pub(super) fn snapshot(&self) -> Failures {
235		Failures {
236			idle_timeout: self.idle_timeout.load(Ordering::Acquire),
237			transport: self.transport.load(Ordering::Acquire),
238			application: self.application.load(Ordering::Acquire),
239			expired: self.expired.load(Ordering::Acquire),
240		}
241	}
242}