Package dev.relism.flash.transport
Class ConnectionScratch
java.lang.Object
dev.relism.flash.transport.ConnectionScratch
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 fromScratchPool), 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 Summary
FieldsModifier and TypeFieldDescriptionstatic final intMatches the relay-buffer size theThreadLocalit replaces used.final byte[]Scratch for relaying a streaming or chunked response body without allocating per response.static final intInitial capacity forresponseHead; grows on demand like anyByteWriter.final ByteWriter(status line,Content-Type,Date, custom headers,Content-Length/Connection, and — for small fixed bodies — the body itself) into before issuing a single bulkwrite(), instead of ~10 smallOutputStream.writecalls.final MessageDigestScratch for the WebSocket handshake'sSec-WebSocket-AcceptSHA-1 digest. -
Method Summary
-
Field Details
-
RELAY_BUFFER_SIZE
public static final int RELAY_BUFFER_SIZEMatches the relay-buffer size theThreadLocalit replaces used.- See Also:
-
RESPONSE_HEAD_INITIAL_SIZE
public static final int RESPONSE_HEAD_INITIAL_SIZEInitial capacity forresponseHead; grows on demand like anyByteWriter.- See Also:
-
relayBuffer
public final byte[] relayBufferScratch for relaying a streaming or chunked response body without allocating per response. -
responseHead
(status line,Content-Type,Date, custom headers,Content-Length/Connection, and — for small fixed bodies — the body itself) into before issuing a single bulkwrite(), instead of ~10 smallOutputStream.writecalls. -
sha1
Scratch for the WebSocket handshake'sSec-WebSocket-AcceptSHA-1 digest.
-