Class ConnectionScratch

java.lang.Object
dev.relism.flash.transport.ConnectionScratch

public final class ConnectionScratch extends Object
ThreadLocal on HttpServer: the decimal-formatting scratch, the streaming relay buffer, and the WebSocket-handshake MessageDigest.

Why not ThreadLocal

ThreadLocal is the right idiom for a bounded platform-thread pool, where "one per thread" means "one per core". Flash runs one virtual thread per connection (Executors.newVirtualThreadPerTaskExecutor()), so a ThreadLocal here means one per connection, not one per core — with no upper bound. At 100 000 concurrent connections, an 8 KB relay buffer alone is ~800 MB of memory that a bounded pool would instead cap. ConnectionScratch is therefore explicit and pooled (ScratchPool), not thread-local.

Lifetime and thread-safety contract

Allocated once per connection (or reused from ScratchPool), owned exclusively by the single virtual thread driving that connection for its whole lifetime, and returned to the pool when the connection closes. Never shared between two connections at once — there is no synchronization here because none is needed. class: routing has no dependency on transport today, and folding the router's the opaque-per-connection-object mechanism (AbstractRouter#newScratch) used instead. This class gains HTTP/2 write/HPACK scratch in later phases, where h2 already depends on transport and no such boundary concern applies.
  • Field Details

    • RELAY_BUFFER_SIZE

      public static final int RELAY_BUFFER_SIZE
      Matches the relay-buffer size the ThreadLocal it replaces used.
      See Also:
    • RESPONSE_HEAD_INITIAL_SIZE

      public static final int RESPONSE_HEAD_INITIAL_SIZE
      Initial capacity for responseHead; grows on demand like any ByteWriter.
      See Also:
    • relayBuffer

      public final byte[] relayBuffer
      Scratch for relaying a streaming or chunked response body without allocating per response.
    • responseHead

      public final ByteWriter responseHead
      (status line, Content-Type, Date, custom headers, Content-Length/ Connection, and — for small fixed bodies — the body itself) into before issuing a single bulk write(), instead of ~10 small OutputStream.write calls.
    • sha1

      public final MessageDigest sha1
      Scratch for the WebSocket handshake's Sec-WebSocket-Accept SHA-1 digest.