JFLib

netio/server.rs#

Source: lib/netio/server.rs

server -- a blocking HTTP/1.1 server: one accept thread, a pool of worker threads, each
worker serves one connection at a time (keep-alive, pipelined requests in order)

  Server::start(&addr, ServerConfig, handler) -> NetResult<Server>
  Server::start_with(.., Some(wrap))   wrap turns an accepted TcpStream into the Stream HTTP
                                       runs over (TLS) and the identity it proved
  server.local_addr() / server.stop()

(G7d) A handler's answer with status 101 and Body::Upgrade (ws::Acceptor::accept) takes the
connection out of HTTP: the head goes out, the upgrade runs on a thread of its own (the worker
is free again), and stop() sets the Upgraded's stop flag and waits for those threads too.

The handler runs on a worker thread; it must not panic (panics abort the process).
Requests with both Content-Length and Transfer-Encoding are refused (request smuggling).
A slow client holds a worker only so long: the head must arrive within read_ms, a body at
MIN_BODY_RATE on average after a first read_ms (over TLS too: records without data end a read
after read_ms).
/// the slowest request body a server waits for, in bytes a second on average (after a first
/// read_ms): slower ones end the connection
MIN_BODY_RATE: u32 = 1024
  • struct ServerConfig

    • threads: usize -- worker threads (connections served at once)
    • max_body: usize -- the largest request body
    • idle_ms: u32 -- how long an idle keep-alive connection stays open
    • read_ms: u32 -- the longest wait for the next bytes of a request in progress; the head must be complete within it, a body must bring 1 KiB a second on average after it (MIN_BODY_RATE)
    • write_ms: u32
    • max_requests: usize -- requests per connection
  • type Handler = Arc<dyn Fn(&Request) -> Response + Send + Sync>

  • type Wrap = Arc<dyn Fn(TcpStream) -> Option<(Box<dyn Stream>, Option<String>)> + Send + Sync> turns an accepted connection into the stream HTTP runs over and the identity it proved (None: drop the connection, e.g. a failed TLS handshake)

  • struct Server

impl Server

  • fn start(addr: &SocketAddr, cfg: ServerConfig, handler: Handler) -> NetResult<Server>
  • fn start_with(addr: &SocketAddr, cfg: ServerConfig, handler: Handler, wrap: Option<Wrap>) -> NetResult<Server>
  • fn local_addr(&self) -> SocketAddr
  • fn upgraded(&self) -> usize (G7d) upgraded connections (WebSockets) still running
  • fn stop(self) stop accepting, finish the requests in progress, wait for the threads; upgraded connections see their stop flag set and are waited for as well

Source: docs/API.md in the JFLib repository.