Server

Struct Server 

Source
pub struct Server { /* private fields */ }

Implementations§

Source§

impl Server

Source

pub fn new() -> Self

Source

pub fn bind(self, addr: SocketAddr) -> Self

Source

pub fn bind_unix<P: AsRef<Path>>(self, path: P) -> Self

Source

pub fn listen<T: Listen + Send + Sync + 'static>(self, listener: T) -> Self

Source

pub fn set_shutdown_callback<F>(self, callback: F) -> Self
where F: Fn() + Send + Sync + 'static,

Source

pub fn on_listen<F>(self, callback: F) -> Self
where F: Fn(&[CoreSocketAddr]) + Send + Sync + 'static,

Source

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);
Source

pub fn with_shutdown(self, graceful_wait: Duration) -> Self

配置优雅关停等待时间。

当收到关停信号(Ctrl-C 或 SIGTERM)时:

  1. 停止接受新连接
  2. 等待活动连接在 graceful_wait 时间内完成
  3. 超时后强制取消剩余连接

默认值为 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));
Source

pub fn with_config(self, config: ServerConfig) -> Self

配置统一入口(连接限速、超时、请求体大小等)。

Source

pub fn with_connection_limits(self, limits: ConnectionLimits) -> Self

设置连接级别超时/请求体大小限制。

Source

pub async fn serve<H>(self, handler: H)

Source

pub fn run<H>(self, handler: H)

Trait Implementations§

Source§

impl Default for Server

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more