persistent connection#3
Open
Piyush-Goenka wants to merge 1 commit intolow-rb:mainfrom
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Content-LengthandConnectionheader negotiation as prerequisites for connection reuse.Changes
request_parser.rbcreate_stream(socket)so the IO stream is created once per connection and reused across requests.parsenow acceptsstream:and optionalversion:(cached after the first request) instead of creating a new stream fromsocket:each time.nilon EOF instead of raising, letting the connection loop exit cleanly.parse_bodyreads exactlyContent-Lengthbytes instead ofstream.read(which would block forever on a persistent connection).nilfromstream.getsduring header parsing (client disconnect mid-request).response_builder.rbContent-Lengthheader (file size for file bodies, bytesize for buffered bodies).Connection: keep-aliveorConnection: closebased on negotiation.socket.close— connection lifecycle is now managed byLowLoop#handle_connection.socket.writefor the body instead ofsocket.puts(avoids trailing newline that would breakContent-Length).Content-LengthandConnectionfrom the response headers loop so we don't emit duplicates when a handler sets them.low_loop.rbhandle_connection(socket)that owns the full connection lifecycle: create stream → loop (wait → parse → process → respond → check keep-alive) → close.keep_alive?negotiates per RFC 9112: HTTP/1.1 defaults keep-alive, HTTP/1.0 defaults close. Handles multi-tokenConnectionheaders (e.g.Connection: Upgrade, close) by splitting on commas.socket.wait_readable(keep_alive_timeout)for idle timeout (Fiber-scheduler-aware, unlikeIO.select).socket.timeout = request_timeoutduring parsing to bound each read — prevents slowloris-style attacks where a client drip-feeds bytes to hold the connection open.IO::TimeoutErrorexits the loop cleanly.config.keep_alive_timeout(default 30s) andconfig.request_timeout(default 10s).response_factory.rbhttp/1.1toHTTP/1.1so clients correctly identify the protocol (curl was falling back to HTTP/1.0 parsing otherwise).spec/units/low_loop_spec.rb1.1sto1.2sto account for the small keep-alive connection lifecycle overhead (one extra loop iteration per connection to detect client disconnect).