Skip to content

Commit

Permalink
Simplify connection count checking by reporting zero connections for …
Browse files Browse the repository at this point in the history
…connection-less oriented server metrics.
  • Loading branch information
ximon18 committed Apr 4, 2024
1 parent 68b91cd commit ccc5187
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/net/server/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ impl ServerMetrics {
impl ServerMetrics {
/// The number of current connections, if applicable.
///
/// This will be None for connection-less servers such as [`DgramServer`].
/// This will be zero for connection-less servers such as [`DgramServer`].
///
/// [`DgramServer`]: crate::net::server::dgram::DgramServer
pub fn num_connections(&self) -> Option<usize> {
pub fn num_connections(&self) -> usize {
self.num_connections
.as_ref()
.map(|atomic| atomic.load(Ordering::Relaxed))
.unwrap_or_default()
}

/// Set the number of current connections metric.
Expand Down
5 changes: 1 addition & 4 deletions src/net/server/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,10 +523,7 @@ where
accept_res = self.accept() => {
match accept_res {
Ok((stream, addr)) => {
// SAFETY: This is a connection-oriented server so there
// must always be a connection count metric avasilable to
// unwrap.
let num_conn = self.metrics.num_connections().unwrap();
let num_conn = self.metrics.num_connections();
if num_conn < self.config.load().max_concurrent_connections {
self.spawn_connection_handler(stream, addr);
}
Expand Down
2 changes: 1 addition & 1 deletion src/net/server/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ async fn service_test() {

// Verify that no requests or responses are in progress still in
// the server.
assert_eq!(srv.metrics().num_connections(), Some(0));
assert_eq!(srv.metrics().num_connections(), 0);
assert_eq!(srv.metrics().num_inflight_requests(), 0);
assert_eq!(srv.metrics().num_pending_writes(), 0);
assert_eq!(srv.metrics().num_received_requests(), num_messages);
Expand Down

0 comments on commit ccc5187

Please sign in to comment.