abla/http/event
Generated from source.
Import with import "abla/http/event".
Source: abla/http/event/entry.ab
HttpEventServerOptions (class)
class HttpEventServerOptions(
val maximumConnections: int,
val maximumHeaderBytes: int,
val maximumBodyBytes: int,
val maximumPendingBytes: int,
val maximumRequestsPerConnection: int,
val idleTimeoutMilliseconds: int,
val gzipMinimumBytes: int,
val drainTimeoutMilliseconds: int,
val requestHeadTimeoutMilliseconds: int = 30000,
val requestTimeoutMilliseconds: int = 0,
val responseTimeoutMilliseconds: int = 0,
// The request bytes the whole server may hold for requests whose handler has not run yet (their heads and buffered bodies, over all connections, plain, HTTPS and
// HTTP/2): default 256 MiB, 0 = no limit. The per-connection limits above bound one connection, and a server takes `maximumConnections` of them. At the limit a new HTTP/2 stream
// is refused (REFUSED_STREAM, a client retries), an HTTP/1 connection holding request bytes is not read until room opens (the request waits; nothing fails), and a connection holding
// none can still send a request head (up to 16 KiB over the limit each, so a small request is answered while the budget is full of waiting bodies). A server still at the limit after
// `idleTimeoutMilliseconds` (30 s when that is 0) closes the connection that has been idle longest among those holding request bytes, one per timeout. A request's bytes are released
// when its handler starts, and when its connection or stream ends for any reason.
val maximumBufferedRequestBytes: int = 268435456
)
Sets maximum connections, input header and body bytes, pending output bytes, requests per connection, idle timeout, minimum size for gzip, and shutdown drain timeout. The idle timeout restarts on every byte received or written, so it does not end a peer that trickles bytes. The three deadlines after it do: requestHeadTimeoutMilliseconds closes a connection whose request head is not complete that long after its first byte (default 30 s: a head is a few hundred bytes), requestTimeoutMilliseconds one whose whole request (head and body) is not complete that long after its first byte, and responseTimeoutMilliseconds one that has not taken a queued response that long after it was queued; the last two are 0 (off) by default, since a large upload or download on a slow link is legitimate. A connection is closed without an answer. Run a server on an open network behind a proxy that limits connections per address.
HTTP_EVENT_HEAD_ALLOWANCE (val)
val HTTP_EVENT_HEAD_ALLOWANCE
The most request-head bytes that a connection holding nothing may read when the server's buffered-request budget is used up.
HttpEventBudget (class)
class HttpEventBudget(var held: int, var overSince: int, val paused: array<bool>)
The server-wide budget of maximumBufferedRequestBytes: held is the request bytes of the open connections (kept as reads and handlers go, and counted afresh at the end of every pass, so it cannot drift), overSince when the budget was first found used up (0: it is not), paused the connections whose read waits for room.
httpEventBudget (fun)
fun httpEventBudget(): HttpEventBudget
Creates a budget with nothing held and no connection slots.
HttpEventBudget.room (extension fun)
fun HttpEventBudget.room(limit: int, slotHeld: int): int
The most bytes a connection that holds slotHeld request bytes may read now under limit (0: none): the room left, and at least one request head for a connection holding nothing. Zero or less: the read waits.
HttpEventDeadlines (class)
class HttpEventDeadlines(
val requestStarted: array<int>,
val requestStamp: array<int>,
val headSeen: array<int>,
val outputStarted: array<int>
)
The clocks of a server's connections for a request being received and a response being sent: when the request that is being received began (0: none), which request that was and whether its head has been seen, and when the queued response began. One per server, indexed by slot; deadlineExpired says whether a connection has run out of the time the options give it.
httpEventDeadlines (fun)
fun httpEventDeadlines(): HttpEventDeadlines
Creates the clocks of a server with no connection slots.
HttpEventDeadlines.deadlineGrow (extension fun)
fun HttpEventDeadlines.deadlineGrow(): void
Adds the clocks of a new connection slot.
HttpEventDeadlines.deadlineReset (extension fun)
fun HttpEventDeadlines.deadlineReset(slot: int): void
Stops the clocks of a slot (its connection closed or a new one took it); stores integers only.
HttpEventDeadlines.deadlineHeadSeen (extension fun)
fun HttpEventDeadlines.deadlineHeadSeen(slot: int, requestsServed: int): void
Records that the head of the request being received on slot is complete, requestsServed requests having been answered before it.
HttpEventDeadlines.deadlineExpired (extension fun)
fun HttpEventDeadlines.deadlineExpired(
slot: int, now: int, options: HttpEventServerOptions, receiving: bool, headKnown: bool, requestsServed: int, sending: bool
): bool
Whether the connection of slot has used the time options give a request it is receiving (any byte of one is held) or a response it is sending (queued output). headKnown says that the head of the request is complete by other means (a chunked request being assembled). The clocks start on the first call that finds the request or the response, so how finely they measure is how often this is called.
HttpEventFrame (class)
class HttpEventFrame(
val length: int,
val request: HttpRequest?,
// An HTTP/1.0 request: its connection closes unless it asks for keep-alive.
val http10: bool = false,
// For incomplete input whose body length is known: the input size at which the request can first be complete
// (the head and the whole body). Zero when unknown. The server then holds small reads without re-framing them.
val need: int = 0,
// A chunked request whose body is not complete: its head, the body decoded so far, and how many bytes of the input are
// consumed into those (the server drops them and continues with `HttpEventChunkState.assemble`). Length is zero.
val head: HttpRequest? = null,
val partial: string = "",
val trim: int = 0
)
Holds the parsed request, HTTP version flag, and a frame length of zero for incomplete or -1 for invalid input.
HttpEventServer (class)
resource class HttpEventServer(
val router: HttpRouter,
val handler: (HttpRequest) -> HttpResponse,
val noEscapeHandler: FnNoEscape(HttpRequest) -> HttpResponse,
val useHandler: bool,
val useNoEscapeHandler: bool,
val listener: TcpListener,
val poller: SocketPoller,
val options: HttpEventServerOptions,
val descriptors: array<int>,
val descriptorSlots: array<int>,
val inputs: array<string>,
val outputs: array<string>,
val outputOffsets: array<int>,
val writeWatching: array<bool>,
val requestCounts: array<int>,
val closing: array<bool>,
val lastActivityMilliseconds: array<int>,
var draining: bool,
var drainDeadlineMilliseconds: int,
var stopped: bool,
// The clock when this pass of step began: a request needn't read it again.
var passMilliseconds: int,
// One socket lookup per accepted connection, shared by persistent requests.
val peerAddresses: array<SocketPeerAddress?> = [],
// Reads that cannot complete the request yet wait here, unjoined, until `needs[slot]` bytes are held: joining and
// re-framing the whole buffer on every read made a body sent a byte at a time cost quadratic CPU (#1139 area 4).
val needs: array<int> = [],
val tails: array<ByteBuffer> = [],
// A chunked request whose body is not complete, per connection (see `HttpEventChunkState`).
val chunkStates: array<HttpEventChunkState> = [],
// The clocks of the request being received and the response being sent, per connection (see HttpEventServerOptions).
val deadlines: HttpEventDeadlines = httpEventDeadlines(),
// The server-wide budget of buffered request bytes (see HttpEventServerOptions.maximumBufferedRequestBytes).
val budget: HttpEventBudget = httpEventBudget()
)
Owns the listener, kernel poller, and per-connection state for an event-driven HTTP server.
HttpEventServer.drop (method)
fun drop(): void
Closes the open client descriptors when the server is released.
httpEventServerOptions (fun)
fun httpEventServerOptions(): HttpEventServerOptions
Returns the default event server limits for connections, headers, request and response bodies, polling, idle time, per-pass work and shutdown drain.
httpEventUnusedHandler (fun)
noescape fun httpEventUnusedHandler(request: HttpRequest): HttpResponse
Returns a 500 response for a handler slot that has no assigned handler.
httpEventServerCollector (fun)
fun httpEventServerCollector(): void
A server's collections: what they find unreachable is freed by a helper thread, and four threads mark and sweep (ABLA_GC_WORKERS overrides; a program built without a C library starts them with its own clone routine). The pauses of a burst of requests halve. The compiler sees these calls in this function (bootstrapProgramWantsBackgroundFree).
httpEventServer (fun)
fun httpEventServer(
router: HttpRouter,
address: Ipv4SocketAddress,
options: HttpEventServerOptions = httpEventServerOptions(),
reusePort: bool = false
): HttpEventServer
Creates an IPv4 event server over router, at address, using options. Set reusePort to request port sharing. Inspect listenError for bind, listen or poller failures.
httpEventServerHandler (fun)
fun httpEventServerHandler(
handler: (HttpRequest) -> HttpResponse,
address: Ipv4SocketAddress,
options: HttpEventServerOptions = httpEventServerOptions(),
reusePort: bool = false
): HttpEventServer
Creates an IPv4 event server dispatching each request to handler. Sets its address, limits, and optional port sharing from the remaining arguments. Check listenError before running.
httpEventServerNoEscapeHandler (fun)
fun httpEventServerNoEscapeHandler(
handler: FnNoEscape(HttpRequest) -> HttpResponse,
address: Ipv4SocketAddress,
options: HttpEventServerOptions = httpEventServerOptions(),
reusePort: bool = false
): HttpEventServer
Returns an event server for a handler that cannot retain its request, with an inactive listener on bind failure.
HttpEventServer.activeConnections (extension fun)
fun HttpEventServer.activeConnections(): int
Returns the number of connection slots holding open descriptors.
HttpEventServer.findDescriptor (extension fun)
fun HttpEventServer.findDescriptor(descriptor: int): int
Returns the connection slot for a registered descriptor, or -1 if none is mapped.
HttpEventServer.allocateSlot (extension fun)
fun HttpEventServer.allocateSlot(descriptor: int): int
Registers a connected descriptor in a reusable slot and returns the slot, or -1 when registration fails or the connection limit is reached.
HttpEventServer.closeSlot (extension fun)
fun HttpEventServer.closeSlot(slot: int): void
Closes and clears an active connection slot, ignoring invalid or already closed slots.
HttpEventServer.slotHeldBytes (extension fun)
fun HttpEventServer.slotHeldBytes(slot: int): int
The request bytes a connection holds that no handler has taken yet: the input waiting to be framed, the reads held back (see needs) and the chunked body assembled so far. A closed slot holds none (its buffers stay until the slot is reused, and are not counted).
HttpEventServer.heldRequestBytes (extension fun)
fun HttpEventServer.heldRequestBytes(): int
The request bytes held by all open connections, counted afresh: what maximumBufferedRequestBytes limits. Zero when nothing is buffered, whatever came and went.
HttpEventServer.budgetEnforce (extension fun)
fun HttpEventServer.budgetEnforce(): void
Counts the held bytes afresh, lets the connections that waited for room read again once there is some, and closes the connection idle longest among those holding request bytes when the budget has stayed used up for a whole idle timeout (one per timeout). Called at the end of every pass.
httpEventRequestLength (fun)
fun httpEventRequestLength(
input: string,
maximumHeader: int,
maximumBody: int
): int
Returns the framed wire byte count, zero for incomplete input, or -1 for malformed framing or exceeded limits.
httpEventFrame (fun)
fun httpEventFrame(
input: string,
maximumHeader: int,
maximumBody: int,
peer: SocketPeerAddress? = null
): HttpEventFrame
Returns the parsed request and consumed length, with zero for incomplete input or -1 for rejected framing.
httpEventResponse (fun)
fun httpEventResponse(
request: HttpRequest,
response: HttpResponse,
keepAlive: bool,
gzipMinimumBytes: int
): string
Renders a response with request-aware body suppression and optional gzip compression.
httpEventResponsePortable (fun)
@noinline
fun httpEventResponsePortable(
request: HttpRequest, encoded: HttpResponse, keepAlive: bool
): string
Renders the HTTP/1.1 status, headers, and permitted body bytes with the requested connection behavior.
httpEventResponseNative (fun)
trusted fun httpEventResponseNative(
request: HttpRequest, encoded: HttpResponse, keepAlive: bool
): string
Serialize into one exact-size allocation instead of constructing a chain of temporary strings and flattening its ropes at the socket boundary.
HttpEventChunkState (class)
class HttpEventChunkState(
var head: HttpRequest?,
var http10: bool,
var body: ByteBuffer
)
Process one framed request in the caller's region. Keeping this separate lets readReady place the socket buffer, parser graph, handler result, and response framing in one allocation region. Only an incomplete input or a backpressured output is promoted across that boundary. A chunked request whose body is not complete, kept in pieces: its head, whether it is HTTP/1.0, and the body decoded so far. An event server keeps one per connection and frames its input through assemble.
httpEventChunkState (fun)
fun httpEventChunkState(): HttpEventChunkState
Creates the state of a connection that is not assembling a chunked request.
HttpEventChunkState.reset (extension fun)
fun HttpEventChunkState.reset(): void
Forgets the request being assembled.
HttpEventChunkState.assemble (extension fun)
fun HttpEventChunkState.assemble(input: string, maximumHeader: int, maximumBody: int, peer: SocketPeerAddress?): HttpEventFrame
Frames the input of a connection like httpEventFrame, except that a chunked request whose body is not complete is kept in this state: its head and the body decoded so far stay here, trim bytes of the input (everything before the first incomplete chunk element) are the caller's to drop, and the next call decodes only what follows (a body of many chunks sent a piece at a time used to be decoded again from its start on every read). need is relative to the input once trimmed.
HttpEventServer.dispatchNoEscape (extension fun)
fun HttpEventServer.dispatchNoEscape(slot: int, request: HttpRequest, http10: bool): void
Hands a framed request to the no-escape handler and writes (or queues) its response. Runs inside the region the no-escape server reads in, and from processNoEscapeChunked outside one: it stores only ints and promoted strings into the server.
HttpEventServer.processNoEscapeInputInRegion (extension fun)
fun HttpEventServer.processNoEscapeInputInRegion(
slot: int,
input: string
): int
Frames one request from a slot's input inside the caller's region, runs the no-escape handler on it and queues the response; returns the bytes consumed, 0 when more input is needed, -3 (nothing consumed) when a chunked request is being assembled and the caller must continue with processNoEscapeChunked outside the region, or another negative value for a malformed request (answered with 400, then closed).
HttpEventServer.processNoEscapeChunked (extension fun)
fun HttpEventServer.processNoEscapeChunked(slot: int): void
Frames and dispatches what a no-escape connection holds once a chunked request is being assembled: outside any region, where the chunk state (a parsed head, a buffer that grows) can be kept in the server. The region path hands over to this when it sees a chunked request that is not complete (it returns -3, nothing consumed) and until the connection holds no such request.
HttpEventServer.processNoEscapeSlot (extension fun)
fun HttpEventServer.processNoEscapeSlot(slot: int): void
Dispatches complete requests in a connection slot through the no-escape handler and queues their responses.
HttpEventServer.processSlot (extension fun)
fun HttpEventServer.processSlot(slot: int): void
Dispatches buffered requests and queues a response when the connection is open and has no pending output.
HttpEventServer.acceptReady (extension fun)
fun HttpEventServer.acceptReady(): void
Accepts ready clients until accept fails, draining starts, or the connection limit is reached.
HttpEventServer.readReady (extension fun)
fun HttpEventServer.readReady(slot: int): void
Reads and processes ready bytes for a valid open slot that has no pending output.
HttpEventServer.writeReady (extension fun)
fun HttpEventServer.writeReady(slot: int): void
Writes pending response bytes for a valid open slot and updates its progress or closes it on failure.
HttpEventServer.step (extension fun)
fun HttpEventServer.step(timeoutMilliseconds: int = 1000): int
Polls for up to timeoutMilliseconds, handles ready connections, and expires idle ones. Returns the number of handled readiness events.
HttpEventServer.stop (extension fun)
fun HttpEventServer.stop(): void
Begins draining by stopping acceptance of new connections. Call drain to service and close the remaining ones.
HttpEventServer.beginDrain (extension fun)
fun HttpEventServer.beginDrain(): void
Stops accepting new clients and starts the configured deadline for completing existing responses.
HttpEventServer.forceStop (extension fun)
fun HttpEventServer.forceStop(): void
Closes every client slot and marks the server stopped.
HttpEventServer.drain (extension fun)
fun HttpEventServer.drain(): int
Services active connections until all close or the drain deadline expires, then closes the server. Returns zero.
HttpEventServer.listenError (extension fun)
fun HttpEventServer.listenError(): int
0 while the server listens; otherwise the errno of the failed bind or listen (98 is an address already in use, 13 no permission, 22 an invalid address), or of the failed poller. A server like that never accepts: run() returns 1 at once.
HttpEventServer.run (extension fun)
fun HttpEventServer.run(): int
Runs the readiness loop until stop or graceful shutdown. Returns 1 for a startup listen or poller error and zero after shutdown.
Built from Abla v0.2.79 source ba5d6b7d. Examples and generated signatures follow that source revision.