Class ListenerOptions.Builder

java.lang.Object
com.jsgalactic.axiom.lifecycle.ListenerOptions.Builder
Enclosing class:
ListenerOptions

public static final class ListenerOptions.Builder extends Object
Collects settings before build(). Each setter validates its value at once and throws IllegalArgumentException with the setting's name and valid range. Not thread-safe; the built options are immutable.
  • Method Details

    • name

      public ListenerOptions.Builder name(String name)
      Names the listener. The name is the value of the listener tag of the listener-level metrics (connections, bytes and answers given before admission), so listeners that share an application can be told apart. It is chosen by the operator, never by a client, and should come from a small fixed set.
      Parameters:
      name - lowercase letters, digits and underscores starting with a letter, at most 32 characters; the default is default
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if invalid
    • shutdownGrace

      public ListenerOptions.Builder shutdownGrace(Duration grace)
      Sets how long closing the listener lets running exchanges finish. After it, remaining connections close and their handlers are interrupted. Zero interrupts at once.
      Parameters:
      grace - from zero to one day; the default is 5 seconds
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if out of range
    • idleTimeout

      public ListenerOptions.Builder idleTimeout(Duration timeout)
      Sets the network inactivity (no read and no write progress) after which a connection closes. It does not interrupt a waiting or running request.
      Parameters:
      timeout - 1 millisecond to one day; the default is 30 seconds
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if out of range
    • headTimeout

      public ListenerOptions.Builder headTimeout(Duration timeout)
      Sets how long a request head may take from its first byte; a slower client receives 408.
      Parameters:
      timeout - 1 millisecond to one day; the default is 10 seconds
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if out of range
    • responseTimeout

      public ListenerOptions.Builder responseTimeout(Duration timeout)
      Sets how long writing one response may take, from handing it to the socket until the operating system has accepted its last byte; a slower reader loses the connection.
      Parameters:
      timeout - 1 millisecond to one day; the default is 30 seconds
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if out of range
    • lingerTimeout

      public ListenerOptions.Builder lingerTimeout(Duration timeout)
      Sets how long a connection may linger after its last response, reading and discarding input so a client that is still sending reads the whole response.
      Parameters:
      timeout - total bound, 1 millisecond to one day; the default is 2 seconds
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if out of range
    • lingerQuietTimeout

      public ListenerOptions.Builder lingerQuietTimeout(Duration timeout)
      Sets the silence after which lingering ends because the client is taken to have finished. Each arriving byte restarts it; it never extends lingerTimeout.
      Parameters:
      timeout - 1 millisecond to one day; the default is 500 milliseconds
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if out of range
    • shutdownLingerTimeout

      public ListenerOptions.Builder shutdownLingerTimeout(Duration timeout)
      Sets the total linger bound once the listener is closing (the smaller of this and lingerTimeout applies).
      Parameters:
      timeout - 1 millisecond to one day; the default is 500 milliseconds
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if out of range
    • handshakeTimeout

      public ListenerOptions.Builder handshakeTimeout(Duration timeout)
      Sets how long a new connection may take to finish its TLS handshake. A client that is slower, or that sends something other than TLS, loses the connection; until then it holds a connection slot (see maxConnections(int)) but no admission capacity. Ignored without tls(TlsOptions).
      Parameters:
      timeout - 1 millisecond to one day; the default is 10 seconds
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if out of range
    • tls

      public ListenerOptions.Builder tls(TlsOptions options)
      Serves HTTPS instead of plain HTTP on this listener. The material is validated when the listener starts. Null serves plain HTTP, which is the default.
      Parameters:
      options - the TLS settings, or null
      Returns:
      this builder
    • http2

      public ListenerOptions.Builder http2(boolean enabled)
      Accepts HTTP/2 on this listener in addition to HTTP/1.1. Off by default, so a listener without this setting behaves exactly as before. With TLS, ALPN offers h2 followed by http/1.1: a client that negotiates h2 speaks HTTP/2 and one that negotiates or falls back to http/1.1 speaks HTTP/1.1. On a cleartext listener, a connection starting with the HTTP/2 client preface speaks HTTP/2; all other connections speak HTTP/1.1. An HTTP/1.1 Upgrade to h2c is not supported.

      This is a transport choice only. Both protocols use the same routes, handlers, codecs, execution limits, body budgets, deadlines, error mapping and shutdown behavior. Requests keep the same shape, which has no HTTP version; HTTP/2 pseudo-headers become ordinary request headers. A TestClient stays in-memory and HTTP/1-shaped regardless of this setting.

      Parameters:
      enabled - true to accept HTTP/2; false to accept HTTP/1.x only
      Returns:
      this builder
    • compression

      public ListenerOptions.Builder compression(boolean enabled)
      Decodes gzip and deflate request bodies and compresses eligible response bodies on this listener. Off by default, so a listener that does not opt in behaves exactly as before.

      This is a transport choice only: no application API or dependency changes. Request bodies are decoded before the handler sees them and are bounded by maxInFlightBodyBytes(long) and the application's body limit after decoding as well as on the wire. Only one coding is decoded. Response compression is negotiated per request from Accept-Encoding, applies only to buffered responses above a small size, and never changes a HEAD, streamed, partial or WebSocket upgrade response. An in-memory TestClient stays uncompressed regardless of this setting.

      Parameters:
      enabled - true to enable request decompression and response compression
      Returns:
      this builder
    • compressionLevel

      public ListenerOptions.Builder compressionLevel(int level)
      Sets the deflater level for compressed responses. Lower levels spend less CPU per byte and produce larger bodies; higher levels spend more CPU and produce smaller ones. The setting has no effect unless compression(boolean) is enabled, and does not change request decompression, whose level the sender chose.

      The default, 6, is the JDK's default and a balanced choice. Compression still runs on the listener's I/O threads with a bounded, copy-free buffer: an offloaded pool would add a queue and extra copies, and at most maxRequestBody bytes are compressed at a time. The axiom.http.compression.* metrics report the cost when tuning this value.

      Parameters:
      level - from 1 (fastest) to 9 (smallest); the default is 6
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if out of range
    • maxDiscardedInput

      public ListenerOptions.Builder maxDiscardedInput(int bytes)
      Sets how many bytes a connection discards unread after an error or its last response before it is closed as abusive.
      Parameters:
      bytes - from zero to 1 GiB; the default is 16 MiB
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if out of range
    • maxConnections

      public ListenerOptions.Builder maxConnections(int connections)
      Sets the open connections per listener. A connection over the cap is closed at once without a response and without consuming a slot. Connections that only linger count against maxLingeringConnections instead while that pool has room.

      Relation to admission: a connection carries at most maxPipelinedRequests outstanding requests, but a client usually keeps one in flight, so the requests a listener can hold at once are roughly bounded by this cap. A cap below the application's admission capacity (active plus queued requests) leaves part of that capacity unreachable: clients are refused at the connection level before admission could queue or reject them with 503. Set the cap at or above that capacity, plus headroom for idle keep-alive connections.

      Parameters:
      connections - 1 to 1,000,000; the default is 128
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if out of range
    • maxLingeringConnections

      public ListenerOptions.Builder maxLingeringConnections(int connections)
      Sets how many lingering connections move to a separate pool and stop counting against maxConnections. Beyond it a lingering connection keeps its regular slot, so a listener has at most maxConnections + maxLingeringConnections open sockets. Zero keeps lingering connections on their regular slots.
      Parameters:
      connections - 0 to 1,000,000; the default is 32
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if out of range
    • maxPipelinedRequests

      public ListenerOptions.Builder maxPipelinedRequests(int requests)
      Sets the outstanding requests per connection, including the running one. A further request is answered 503 after the earlier responses and the connection closes.
      Parameters:
      requests - 1 to 1024; the default is 8
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if out of range
    • maxInFlightBodyBytes

      public ListenerOptions.Builder maxInFlightBodyBytes(long bytes)
      Sets the request body bytes the listener holds at once, summed over all its connections: bodies being received, bodies of pipelined requests waiting behind a running one, and the bodies of requests whose handler is running. A declared Content-Length is reserved in full when the request head arrives and a chunked body is counted as it arrives; the reservation is returned when the request's response is final or its connection ends. A request that does not fit is answered 503 and its connection closes after the earlier responses; the handler is never invoked. The default equals the largest configurable maxRequestBody, so any permitted body fits when the listener is otherwise idle. A listener refuses to start when the budget is smaller than the application's maxRequestBody, because no such body could ever be accepted.
      Parameters:
      bytes - 1 to 1 TiB; the default is 64 MiB
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if out of range
    • maxRequestLine

      public ListenerOptions.Builder maxRequestLine(int bytes)
      Sets the longest request line; a longer one is answered 414.
      Parameters:
      bytes - 256 to 65,536; the default is 4096
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if out of range
    • maxHeaderBytes

      public ListenerOptions.Builder maxHeaderBytes(int bytes)
      Sets the largest header section; a larger one is answered 431.
      Parameters:
      bytes - 256 to 1 MiB; the default is 8192
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if out of range
    • ioThreads

      public ListenerOptions.Builder ioThreads(int threads)
      Sets the I/O threads of the listener. User handlers never run on them.
      Parameters:
      threads - 1 to 1024; the default is the number of available processors, at least 2
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if out of range
    • transport

      public ListenerOptions.Builder transport(TransportKind kind)
      Sets the I/O mechanism of the listener. Behavior, limits and timeouts do not depend on it. TransportKind.AUTO uses a native transport when its library is on the class path and usable, and the JDK's NIO otherwise; naming a native kind that is not usable makes the listener fail to start with an IllegalStateException.
      Parameters:
      kind - the requested transport; the default is TransportKind.AUTO
      Returns:
      this builder
      Throws:
      NullPointerException - if null
    • rejectionObserver

      public ListenerOptions.Builder rejectionObserver(RejectionObserver observer)
      Sets an observer called for each error response the listener generates itself, such as 413, 431 or 503, for metrics and logging. It is read-only; see RejectionObserver.
      Parameters:
      observer - the observer; one is replaced by the next call
      Returns:
      this builder
    • build

      public ListenerOptions build()
      Builds immutable options.
      Returns:
      the options