pub struct Server { /* private fields */ }Implementations§
Source§impl Server
impl Server
pub fn new() -> Self
pub fn bind(self, addr: SocketAddr) -> Self
pub fn bind_unix<P: AsRef<Path>>(self, path: P) -> Self
pub fn listen<T: Listen + Send + Sync + 'static>(self, listener: T) -> Self
pub fn set_shutdown_callback<F>(self, callback: F) -> Self
pub fn on_listen<F>(self, callback: F) -> Self
Sourcepub fn with_rate_limiter(self, config: RateLimiterConfig) -> Self
pub fn with_rate_limiter(self, config: RateLimiterConfig) -> Self
配置连接限流器(令牌桶算法)。
限流器用于控制连接接受速率,防止服务器过载。
§Examples
use silent::{Server, RateLimiterConfig};
use std::time::Duration;
let config = RateLimiterConfig {
capacity: 10,
refill_every: Duration::from_millis(10),
max_wait: Duration::from_secs(2),
};
let server = Server::new()
.bind("127.0.0.1:8080".parse().unwrap())
.with_rate_limiter(config);Sourcepub fn with_shutdown(self, graceful_wait: Duration) -> Self
pub fn with_shutdown(self, graceful_wait: Duration) -> Self
配置优雅关停等待时间。
当收到关停信号(Ctrl-C 或 SIGTERM)时:
- 停止接受新连接
- 等待活动连接在
graceful_wait时间内完成 - 超时后强制取消剩余连接
默认值为 0,表示立即强制关停。
§Examples
等待最多 30 秒让连接优雅完成:
use silent::Server;
use std::time::Duration;
let server = Server::new()
.bind("127.0.0.1:8080".parse().unwrap())
.with_shutdown(Duration::from_secs(30));Sourcepub fn with_config(self, config: ServerConfig) -> Self
pub fn with_config(self, config: ServerConfig) -> Self
配置统一入口(连接限速、超时、请求体大小等)。
Sourcepub fn with_connection_limits(self, limits: ConnectionLimits) -> Self
pub fn with_connection_limits(self, limits: ConnectionLimits) -> Self
设置连接级别超时/请求体大小限制。
pub async fn serve<H>(self, handler: H)where
H: ConnectionService + Clone,
pub fn run<H>(self, handler: H)where
H: ConnectionService + Clone,
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Server
impl !RefUnwindSafe for Server
impl Send for Server
impl Sync for Server
impl Unpin for Server
impl !UnwindSafe for Server
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more