The Zuri Standard Library Zuri

Networking

http.loop

import http.loop
Zuri

http does not re-export this module, so it is reached only by importing it directly.

A single thread serving many connections at once.

A thread that's dedicated to one connection can't look at another until that connection releases it, and clients aren't always quick. A client can sit on an idle keep-alive connection, send half a request and stop, or stop reading the response it asked for. ConnectionLoop keeps every connection on a poller instead, and only does the work for each one that can be done without waiting.

Reading uses buffering. Each time a connection is readable, whatever has arrived is buffered, and the request pipeline only runs once a complete request is in the buffer, so the pipeline's own reads never touch the socket. Writing uses queueing. Everything a connection produces waits in its output queue until the pipeline has finished, and is sent as the socket has room, which also combines a batch of responses into a single write.

Timeouts are handled here as well, as a deadline for each connection, because the socket's own timeouts only apply to a call that's waiting.

Two things can still keep the thread busy, and both are under the application's control: a handler runs until it finishes, and a streamed body or an upgraded connection writes as it goes, limited by write_timeout.

Classes

ConnectionLoop

class http.loop.ConnectionLoop
Zuri

The connections one thread serves, and the poller that says which of them can be served right now.

HttpServer.listen() runs one of these over its listener, and every worker of http.serve() runs one over the sockets handed to it.

Constructor

http.loop.ConnectionLoop(server, limit: number)
Zuri

Parameters

  • server (HttpServer) — what answers the requests
  • limit (number) — the most connections to hold at once

ConnectionLoop.watch()

http.loop.ConnectionLoop.watch(acceptor)
Zuri

Accepts from acceptor whenever its listener has a connection waiting and this loop has room for it.

Parameters

  • acceptor (Acceptor)

ConnectionLoop.take()

http.loop.ConnectionLoop.take(client)
Zuri

Takes on a connection that has already been accepted. Which protocol it speaks is decided later, once it has said something.

Parameters

  • client (TcpStream)

ConnectionLoop.length()

http.loop.ConnectionLoop.length() -> number
Zuri

How many connections are held.

Returns number

ConnectionLoop.is_empty()

http.loop.ConnectionLoop.is_empty() -> bool
Zuri

Whether no connections are held.

Returns bool

ConnectionLoop.has_room()

http.loop.ConnectionLoop.has_room() -> bool
Zuri

Whether another connection may be taken on.

Returns bool

ConnectionLoop.turn()

http.loop.ConnectionLoop.turn(timeout)
Zuri

Waits up to timeout for something to do and does it.

Parameters

  • timeout (number) — milliseconds

ConnectionLoop.finish()

http.loop.ConnectionLoop.finish()
Zuri

Stops accepting and closes every connection, giving output already queued a bounded chance to reach its peer.


2026, Richard Ore and Zuri contributors