use std::net::ToSocketAddrs;
use std::sync::Arc;
use std::time::Duration;
use crate::PajamaxService;
pub struct ConfigedServer {
pub(crate) config: Config,
pub(crate) services: Vec<Arc<dyn PajamaxService + Send + Sync + 'static>>,
}
impl ConfigedServer {
pub fn add_service<S>(mut self, svc: S) -> Self
where
S: crate::PajamaxService + Send + Sync + 'static,
{
self.services.push(Arc::new(svc));
self
}
pub fn serve<A>(self, addr: A) -> std::io::Result<()>
where
A: ToSocketAddrs,
{
crate::connection::serve_with_config(self.services, self.config, addr)
}
}
#[derive(Clone, Copy, Debug)]
pub struct Config {
pub(crate) max_concurrent_connections: usize,
pub(crate) max_concurrent_streams: usize,
pub(crate) max_frame_size: usize,
pub(crate) max_flush_requests: usize,
pub(crate) max_flush_size: usize,
pub(crate) idle_timeout: Duration,
pub(crate) write_timeout: Duration,
}
impl Config {
pub fn new() -> Self {
Self {
max_concurrent_connections: 100,
max_concurrent_streams: 1000,
max_frame_size: 16 * 1024,
max_flush_requests: 50,
max_flush_size: 15000,
idle_timeout: Duration::from_secs(60),
write_timeout: Duration::from_secs(10),
}
}
pub fn max_concurrent_connections(self, n: usize) -> Self {
Self {
max_concurrent_connections: n,
..self
}
}
pub fn max_concurrent_streams(self, n: usize) -> Self {
Self {
max_concurrent_streams: n,
..self
}
}
pub fn max_frame_size(self, n: usize) -> Self {
Self {
max_frame_size: n,
..self
}
}
pub fn max_flush_requests(self, n: usize) -> Self {
Self {
max_flush_requests: n,
..self
}
}
pub fn max_flush_size(self, n: usize) -> Self {
Self {
max_frame_size: n,
..self
}
}
pub fn idle_timeout(self, d: Duration) -> Self {
Self {
idle_timeout: d,
..self
}
}
pub fn write_timeout(self, d: Duration) -> Self {
Self {
write_timeout: d,
..self
}
}
pub fn add_service<S>(self, svc: S) -> ConfigedServer
where
S: crate::PajamaxService + Send + Sync + 'static,
{
ConfigedServer {
config: self,
services: vec![Arc::new(svc)],
}
}
}