pub struct StatsServer { /* private fields */ }Expand description
A bound TCP listener serving the stats endpoint.
Construct via StatsServer::bind then call
StatsServer::run to accept connections in a loop, or
StatsServer::accept_one for one-shot tests.
§Examples
use std::sync::Arc;
use dynomite::stats::{Snapshot, StatsServer};
use parking_lot::Mutex;
let sink = Arc::new(Mutex::new(Snapshot::default()));
let server = StatsServer::bind("127.0.0.1:0".parse().unwrap(), sink)?;
let _addr = server.local_addr()?;Implementations§
Source§impl StatsServer
impl StatsServer
Sourcepub fn bind(addr: SocketAddr, source: Arc<Mutex<Snapshot>>) -> Result<Self>
pub fn bind(addr: SocketAddr, source: Arc<Mutex<Snapshot>>) -> Result<Self>
Bind a listener at addr. Returns the bound server alongside
its actual local address (useful when binding to port 0).
§Examples
use std::sync::Arc;
use dynomite::stats::{Snapshot, StatsServer};
use parking_lot::Mutex;
let sink = Arc::new(Mutex::new(Snapshot::default()));
let _server = StatsServer::bind("127.0.0.1:0".parse().unwrap(), sink)?;Sourcepub fn with_cluster_info_provider(self, provider: ClusterInfoProvider) -> Self
pub fn with_cluster_info_provider(self, provider: ClusterInfoProvider) -> Self
Attach a ClusterInfoProvider so the server answers
GET /cluster-info.txt with a freshly assembled
snapshot. When no provider is registered the route
returns 503 Service Unavailable.
§Examples
use std::sync::Arc;
use dynomite::admin::cluster_info::ClusterInfoSnapshot;
use dynomite::stats::{Snapshot, StatsServer};
use parking_lot::Mutex;
let sink = Arc::new(Mutex::new(Snapshot::default()));
let server = StatsServer::bind("127.0.0.1:0".parse().unwrap(), sink)?
.with_cluster_info_provider(Arc::new(ClusterInfoSnapshot::synthetic));
drop(server);Sourcepub fn with_ring_provider(self, provider: RingProvider) -> Self
pub fn with_ring_provider(self, provider: RingProvider) -> Self
Attach a RingProvider so the server answers
GET /ring with a freshly assembled, JSON-serialized
view of every peer’s tokens, dc, rack, and state. When no
provider is registered the route returns 503 Service Unavailable.
§Examples
use std::sync::Arc;
use dynomite::admin::cluster_info::RingSnapshot;
use dynomite::stats::{Snapshot, StatsServer};
use parking_lot::Mutex;
let sink = Arc::new(Mutex::new(Snapshot::default()));
let server = StatsServer::bind("127.0.0.1:0".parse().unwrap(), sink)?
.with_ring_provider(Arc::new(RingSnapshot::default));
drop(server);Sourcepub fn local_addr(&self) -> Result<SocketAddr>
pub fn local_addr(&self) -> Result<SocketAddr>
Returns the local socket address the server is listening on.
§Examples
use std::sync::Arc;
use dynomite::stats::{Snapshot, StatsServer};
use parking_lot::Mutex;
let sink = Arc::new(Mutex::new(Snapshot::default()));
let server = StatsServer::bind("127.0.0.1:0".parse().unwrap(), sink)?;
let addr = server.local_addr()?;
assert!(addr.port() != 0);Sourcepub async fn accept_one(&self) -> Result<()>
pub async fn accept_one(&self) -> Result<()>
Accept a single connection, serve one HTTP/1.1 request, and return.
§Examples
use std::sync::Arc;
use dynomite::stats::{Snapshot, StatsServer};
use parking_lot::Mutex;
let sink = Arc::new(Mutex::new(Snapshot::default()));
let server = StatsServer::bind("127.0.0.1:0".parse().unwrap(), sink)?;
server.accept_one().await?;Sourcepub async fn run(self) -> Result<()>
pub async fn run(self) -> Result<()>
Run the accept loop until cancelled. Each connection is handled on a fresh task so a slow client cannot stall the listener.
§Examples
use std::sync::Arc;
use dynomite::stats::{Snapshot, StatsServer};
use parking_lot::Mutex;
let sink = Arc::new(Mutex::new(Snapshot::default()));
let server = StatsServer::bind("127.0.0.1:0".parse().unwrap(), sink)?;
let _ = tokio::spawn(async move { server.run().await });