Runtime and Build
wisp::main!() is wisp::app!() plus a main that calls wisp::run::<App>(). Write your own main if the app sets things up first. wisp::serve::<App>(addr) is the async form for apps that own their runtime. Base path, slots, fonts and layers: Parity features.
Server
wisp::run::<App>() serves on $HOST:$PORT (default 3000) with one worker per CPU (WISP_THREADS). Each worker is a single-threaded tokio runtime with its own I/O driver, and a connection stays on one worker. There is no work stealing, so a handler that blocks its thread stalls that thread's connections. Dev builds log any handler that holds a thread for 100 ms or more.
- SIGTERM or Ctrl+C stops accepting, answers requests under way with
connection: close, and closes idle connections. WithWISP_STOP_DELAYset, it first waits that many seconds while/_wisp/healthanswers 503. The drain lasts up toWISP_DRAINseconds (10 by default). Then the server runsshutdownfromsrc/hooks.rs, if the app has one, for up to 3 seconds. Then the server returns. A second signal at any of these steps ends the stop at once. A hook that holds its thread stops only at its first yield, or at the end of the process. The watchdog ends the process at most 4.5 seconds after the hook starts. See Environment variables. - Under
wisp devthe app exits when the CLI's stdin pipe closes, so a killedwisp devnever leaves an app on the port. - A panic in a handler is a 500 for that request. The connection survives.
- TLS is opt-in (feature
tls,WISP_TLS_CERT, Deploy). Certificate renewal, HTTP/3 and compression belong to the proxy or CDN (Caddy, nginx, Cloudflare). Or run Wisp as a tower service under hyper or axum: Testing and mixing with Rust code.
Settings
All settings are environment variables. A set but invalid value stops the server with a message. The full list is in Environment variables.
| Setting | What it does |
|---|---|
PORT, HOST | listen address (defaults: port 3000, host 127.0.0.1 in dev and 0.0.0.0 otherwise) |
WISP_DEV | dev mode: on in debug builds, off in release |
WISP_THREADS | worker threads, one per CPU |
WISP_BODY_LIMIT | largest request body (512KB or 10MB, default 1 MB) |
WISP_SECRET | signs cookies, at least 32 characters |
WISP_SECRET_OLD | the previous secret, for rotation |
ORIGIN | the site's address, for a proxy that does not pass Host |
WISP_CLIENT_IP_HEADER | header the proxy puts the client address in, for cx.client_ip(). The last address counts, the one your proxy added. A visitor can forge the first |
WISP_ONION | on, off or auto: serve as a Tor onion service |
WISP_MAX_CONNS | open connections before new ones get a 503. Unset: on Linux half of an open-file limit of 2048 or less, else the limit less 1024. It stays within a quarter of memory (cgroup memory.max in a container) at 16 KiB each. Unset elsewhere: 10000. 0 for no cap of its own. On Linux, the server raises the soft open-file limit to the hard one at start. A set number is at most half of a limit of 2048 or less, else at most the limit less 1024 (64 at least). The server writes a note when it cuts a number of 1 or more. |
WISP_KEEP_ALIVE_TIMEOUT | seconds an idle keep-alive connection stays open before the server closes it. Default 60 on the native server (HTTP/1 and HTTP/2), 65 on Node, Bun and Deno targets. Bun.serve takes at most 255. Whole seconds, 1 or more. The native server stops at start for a value of 0 or a non-number. The Node, Bun and Deno targets use 65 for it. |
WISP_HEADER_TIMEOUT | seconds to receive a request head after its first byte. Default 10. Whole seconds, 1 or more. A value of 0 or a non-number stops the server at start with a message. Native server only. |
WISP_BODY_TIMEOUT | seconds allowed for each part of a request body. Each piece of a fragmented WebSocket message and each HTTP/2 body part gets the same time. Default 10. After that time, the body must still average 1 KB a second. Whole seconds, 1 or more. A value of 0 or a non-number stops the server at start with a message. Native server only. |
WISP_WRITE_TIMEOUT | seconds a client may take none of a response before the server drops it. Default 30. After that time, the client must still take 1 KB a second. Whole seconds, 1 or more. A value of 0 or a non-number stops the server at start with a message. Native server only. |
WISP_IO | Linux: epoll instead of io_uring. uring fails at start where it cannot run |
Behind a proxy, the server checks form posts against Host, X-Forwarded-Host or ORIGIN, and cx.client_ip() trusts only the header WISP_CLIENT_IP_HEADER names. Behind a Tor onion service every visitor is the Tor daemon, 127.0.0.1 when it runs on the same host. RATE_LIMIT and cx.limit count by cx.visitor() instead. Behind Tor, this is a signed ticket cookie that each visitor carries. The whole site's allowance sits on top. A client that never returns its cookie shares one limit with all such clients. Limit by account or your own key with RateLimit::check(key) (cx.login already does). To count each Tor circuit on its own, set HiddenServiceExportCircuitID haproxy in torrc, put HAProxy in front (bind 127.0.0.1:8080 accept-proxy, option forwardfor) and set WISP_CLIENT_IP_HEADER=x-forwarded-for. This needs WISP_ONION=off. With on, or with an onion ORIGIN or SITE_URL, Wisp ignores WISP_CLIENT_IP_HEADER and logs that it does. With auto, a client address that differs from the peer ends the onion rules for that request. A client can open more circuits, so this stops a runaway browser, not a flood.
I/O Drivers
io_uring (Linux 6.1+, crates/wisp/src/uring.rs) is the default where it works. Each worker has its own ring and its own SO_REUSEPORT listener, and one io_uring_enter per turn submits every response. At start, a throwaway ring proves it works. It receives and sends. Three test requests over TCP must each finish by eventfd wakes within 500 ms. One request can try again. Then a request of 160 KiB must finish within 1 s. Its receive pauses on a full inbox and starts again, its echo needs more than one send, and the ring closes the connection (OP_CLOSE). With WISP_IO not set, the workers serve on epoll at once and the proof runs on a thread of its own. When it passes, each worker takes its listener out of its epoll (it does not close it, so no waiting connection is lost) and gives it to its ring. A worker whose ring then cannot start keeps its listener on epoll, and the server writes wisp: io: epoll (io_uring stopped working: ...) once. The epoll driver clears O_NONBLOCK before it gives the listener. If that fails, the worker keeps the listener on epoll, and the server writes wisp: io: epoll (the listener stays O_NONBLOCK: ...) once. The connections that epoll has stay on epoll until they end.
With WISP_IO=uring the start waits for the proof. With no loopback there is no proof. With DEFER_TASKRUN on Linux 6.18.54.1-microsoft-standard-WSL2 and Ubuntu 7.0.0-42-generic, the eventfd of a ring stops waking for new connections. The check fails there, and the workers use a ring without DEFER_TASKRUN. The kernel source may explain it. io_eventfd_do_signal (io_uring/eventfd.c:42 at tags v6.18 and v7.0) does not clear IO_EVENTFD_OP_SIGNAL_BIT. Linux commit 04fe9aeb4f3c adds the clear. The order is: buffer ring with DEFER_TASKRUN, buffer ring without it, PROVIDE_BUFFERS with it, PROVIDE_BUFFERS without it, epoll. The speed without DEFER_TASKRUN is not measured. One stderr line (wisp: io: ...) says which driver runs. The two ways with a buffer ring always get a proof. A later way starts only within 2 s of the first proof. If no way passes, epoll starts after 17 s at most. The driver itself answers a request whose handler never waits, without the scheduler, as on epoll. epoll already did this. It does not change. Where the kernel refuses buffer rings (Ubuntu 24.04's 6.8), the driver falls back to PROVIDE_BUFFERS. A page head and a body of 16 KiB or more go in one send: one sendmsg on epoll, one OP_SENDMSG on io_uring. At start a test send checks OP_SENDMSG. When it fails, the head and the body go in two sends, and the stderr line adds , no OP_SENDMSG (<cause>).
epoll (crates/wisp/src/epoll.rs) runs where io_uring cannot run (before 6.1, a seccomp profile such as Docker's default, the io_uring_disabled sysctl), or with WISP_IO=epoll. The driver answers a request whose handler never waits, without the scheduler. An async fn before in hooks.rs takes that path away from every route, so keep it sync.
Connections and Limits
An idle keep-alive connection gives its read and write buffers back to the thread's pool. On io_uring the kernel receives into the worker's own buffers. A worker gets 28 buffers. It gets a quarter more (at least 16) whenever a receive finds none, up to 256 with a buffer ring and 4096 without. A write buffer of 16 KB or less stays with the connection for its next response. A larger one goes to the worker's spares (64 at most). The spares shrink to the size of the response that last used them. The worker frees a spare once no send took it for a second. On Linux with glibc, the clock thread trims the heap (malloc_trim) when a worker frees buffers that it kept. It also trims after a worker that had at least 170 connections open has none open for 2 s.
| Limit | Value |
|---|---|
| Headers | 16 KB, 100 headers |
| Too large a head | over 16 KB or 100 headers: 431. A request line over 16 KB with no line end: 414 |
| HTTP version | HTTP/2.0, HTTP/1.2 and the like: 505 (the server speaks 1.0 and 1.1) |
| JSON body | 200 000 values (keys counted) and 128 levels deep, else 400 |
| Body | 1 MB (WISP_BODY_LIMIT or a route's BODY_LIMIT), checked against Content-Length first. The server unpacks a gzip body (Content-Encoding: gzip or x-gzip, HTTP/1.1 and HTTP/2) before the handler reads it. The limit holds for the unpacked size too (413). A body that is not one gzip file, or ends too soon, gets 400. So does a body with too many dynamic blocks. A body can hold one dynamic block for each 24 bytes of its deflate data (not the gzip header and trailer), and 64 more. Any other coding, a list and a repeated header get 415. The unpacked request has no content-encoding and no content-length header. A route with an Upload holds a packed body in memory, and its early RATE_LIMIT and SIGNED_IN check does not run for it, over HTTP/1.1 and HTTP/2. The edge build does not unpack |
| Receive | 10 s for the head (WISP_HEADER_TIMEOUT), then 10 s per part of the body (WISP_BODY_TIMEOUT). After the first 10 s, a body must average 1024 bytes a second (MIN_BODY_RATE, crates/wisp/src/policy.rs:27) |
| Keep-alive idle | 60 s (WISP_KEEP_ALIVE_TIMEOUT) |
| Client taking a response | 30 s (WISP_WRITE_TIMEOUT) |
| Out of descriptors | accepting pauses 50 ms at a time |
The four times above are defaults. Each env var sets whole seconds, 1 or more, once at start. A value of 0 or a non-number stops the server at start. The clock counts whole seconds, so a time can end up to one second late. The 1 KB a second minimum rate stays fixed.
- HTTP/1.1 without
Host, or with two, is 400. The server parses chunked bodies strictly (other codings are 501,Content-LengthwithTransfer-Encodingis 400). - With
WISP_ALLOWED_HOSTSset, a request with anotherHost, or with none (HTTP/1.0), is a 421. It holds on the native server and on thenode,deno,bunandcloudflaretargets. Oncloudflare, the files ofstatic/come from Cloudflare and skip the check. Thepages,vercel,netlifyandazurehosts also serve those files before the app. Not tested on those hosts. - The server gzips embedded files (
wisp.js,live.js, CSS,static/) once, with Huffman codes made for each file, off the request path. - The build makes Brotli copies of embedded text and prerendered pages and embeds them as
.br. It useswisp-buildand thebrotlicrate (quality 11, a build dependency only). The request path readsaccept-encodingonly for an asset that has a copy, and sends the copy as is.
HTTP/2
The h2 feature compiles in h2c with prior knowledge (src/h2.rs, own HPACK, no new dependency). A request over HTTP/1 pays nothing, with the feature on or off. There is no Upgrade: h2c. ALPN offers h2 only on the HTTPS port (feature tls).
- Limits: 100 concurrent streams, 16 KiB header lists, the route's body limit (413).
- A reset, CONTINUATION or HPACK flood ends the connection with GOAWAY
ENHANCE_YOUR_CALM. - A stream can wait on the windows of the client. Then the client must take the answers at 1 KB/s or more after a 10 s grace (
WISP_BODY_TIMEOUT). Else the server closes the connection. This holds also while other streams run their handlers, so a starved answer cannot stay in memory. - WebSockets use extended CONNECT (RFC 8441). A plain GET to a WebSocket route is a 426. The
:pathof the CONNECT must start with/. DATA that comes with the CONNECT goes to the socket, in order.
HTTPS
The tls feature (src/http/tls.rs) serves HTTPS when WISP_TLS_CERT and WISP_TLS_KEY name a PEM pair. It adds no dependency. rustls through tokio-rustls is already there for wisp::fetch. The code names the ring provider, so a second provider in the build cannot make it panic.
- A TLS connection is tokio's socket under rustls (
Conn::Tls). On Linux, the io_uring and epoll drivers move bytes between the socket and the connection buffer with no layer between. So HTTPS takes the tokio accept path on every OS. With no pair set, Linux keeps its drivers. - Without the feature, the compiler does not build the TLS code.
- A bad pair stops the server at start. A build with no
tlsfeature and a value inWISP_TLS_CERTorWISP_TLS_KEYstops too. The server never serves plain HTTP in its place, because a downgrade is a security fault.WISP_DEV_FRONTturns TLS off only in a debug build that has theWISP_DEV_PEERtoken ofwisp dev. After start, a failed handshake closes that connection and nothing else. The code ofsrc/http/tls.rshas nounwrap,expectorpanic!outside its tests. - An idle HTTPS keep-alive connection keeps its buffers and its rustls state. It cannot wait without a read, so it cannot hand its buffers back. The
WISP_MAX_CONNSdefault counts 16 KiB for each connection. An HTTPS connection may use more. Not measured. - A handshake that lasts more than 10 seconds ends. A separate thread checks the two files once a minute, so a new connection never waits for that read. A changed pair that works serves new connections only.
- Session tickets are on (
ring::Ticketer), so a returning client skips the full handshake. If the ticketer cannot start, the server runs without it.
One Request Entry Point
The built-in server is one front end. Everything else calls the same code, parser and limits (Testing and mixing with Rust code, Deploying):
| Entry | What it is |
|---|---|
wisp::prepare::<A>() | runs init |
wisp::handle::<A>(Request) -> Reply | answers one request in process |
wisp::test::client::<A>() | handle with cookies, for tests |
wisp::test::browser::<A>() (feature browser) | the server on a free port, driven in headless Chrome or Edge |
wisp::tower::service::<A>() (feature tower) | a tower::Service |
wisp build --static | runs handle for each page and writes files |
wisp build --target | compiles the same code to WebAssembly (crates/wisp/src/edge.rs) with a small JS bridge |
The App Trait
Generated code implements one trait:
pub trait App: 'static {
fn init() -> impl Future<Output = Result<()>>;
fn handle(
route: Option<usize>,
cx: &mut Cx,
out: &mut Out,
) -> impl Future<Output = Result<()>> + Send;
// + static tables: shell, assets, templates (dev)
}
handle calls before, then is one match over the route id, so the compiler monomorphizes the whole server with the app. It has no handler trait objects.
Templates need less Rust:
Datafields are in scope:${count}for${data.count}.class:won=${data.won}toggles a class on the server.<a ${href}>ishref=${href}.- An
Optionattribute (aria-current=${current}) drops out whenNone. #[derive(Json)]gives JSON without serde: return the value, orResponse::json_of(&value).
Hooks in the Request Flow
before runs first, then the route's guards, load and render, then html on the finished text/html body, then after. before returns Result<Option<Response>>. Ok(Some(response)) answers at once, and Ok(Some(Response::rewrite("/x"))) routes again to /x. See Parity features.
What wisp build Does
wisp_build::run() in the app's build.rs:
- Walks
src/routes, builds the route table and rejects conflicts. - Parses every
.wispfile into a node list. Errors arefile:line:col: msg. - Scans
+page.rs,+layout.rs,+server.rs, the blocks' items,src/hooks.rsandsrc/NAME.rsforfn load,#[action]functions, HTTP-method functions, hooks andconst BODY_LIMIT, and reads each signature. - Writes
$OUT_DIR/wisp.rs: a module per user file, a render function per template, the router and the asset tables. Each block line carries a// file.wisp:linecomment, whichwisp devuses to retell rustc's errors against the file.
Release builds embed static/ and the built CSS. The CSS and each built browser file have their content hash in the name (/_app/app.<hash>.css, /_app/c/t3.<hash>.js), and wisp.js has the runtime version in its name. Only these names get Cache-Control: immutable. An old name gets a 404, also from a static host that ignores the query. An old tab can get this 404 for a template module that live.js imports. Then wisp.js loads the page whole, by GET and in place, once for each address and build in the tab session. It does not do this offline, on the answer of a post, or when sessionStorage throws. The tab loses the values that the user typed. See Version skew in the features. live.js and queue.js change their names only with the version of the Wisp runtime, and a 404 of them does not load the page again. An import('...') in app code does not load the page again. A host with an SPA fallback can answer an old name with its index.html instead (--spa with a rest route at the root). Files of static/ keep their names and revalidate by ETag. wisp dev names the built files with ?v=hash and sends no immutable. A script that reads or changes built files after a release build must use the hashed names (/_app/c/<name>.<hash>.js), not ?v=<hash>. A file whose path has a segment that starts with . is not embedded, except under a top-level .well-known.
- Baked pages. A page that is the same for every request (no load, statements or
+page.js, literal holes and props) is onestaticin the binary. Answering is two copies and the date, and a matchingif-none-matchis a 304 without hashing. Hooks still run first. - Folded text. In release, adjacent text and literal holes become one
push_str, escaped at build time. - Router. The router matches a path with no parameter whole, by length then bytes.
Is This Page Useful?