Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions docs/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,11 @@ HTTP/2 resource cleanup. The strict standalone Host composes that session.
The optional [Falcon package](../gems/connectrpc-falcon/README.md) composes it with
Falcon's process workers, raw endpoints and HTTP server/run lifecycle. Its local
HTTP/1 resource uses upstream parse/write hooks and preserves wire completion
and callback boundaries during retirement. A private response-body EOF marker
pairs with the pinned writer's next successful final flush to establish one
absolute 500 ms final-close expiry before callbacks. Successfully delivered
and callback boundaries during retirement. On a successful write,
protocol-http1 closes the body only after the final flush. A private
response-body wrapper establishes one absolute 500 ms final-close expiry at
that error-free close after EOF or HEAD, before the body's close callbacks. A
response without a body anchors when the writer returns. Successfully delivered
retiring Connect responses half-close raw TCP writes, retain the original
100 ms / 256 KiB bounded discard, then wait only the remaining total budget
without further reads. Admission/socket ownership joins original processing and
Expand Down
15 changes: 10 additions & 5 deletions docs/streaming.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,16 @@ including the adapter remainder. Every additional framed read reserves a full
Connect does not use `grpc-timeout`.

A successfully delivered response on a retiring connection uses one absolute
final-close expiry at the actual final HTTP flush plus 500 ms. A private body EOF
marker anchors the pinned writer's next successful final flush before body-close
callbacks or lifecycle observation; payload bytes cannot trigger it. Delayed
callbacks consume the same total budget. After completed delivery, Falcon
half-closes raw TCP writes and performs bounded discard. When discard stops,
final-close expiry at the actual final HTTP flush plus 500 ms. On a successful
write, protocol-http1 closes the body only after its final flush (for HTTP/1.0
close-delimited bodies, also after the write half-close). A private body wrapper
therefore anchors the expiry when the writer closes the body without an error
after EOF or a HEAD response, before the body's close callbacks or lifecycle
observation. A response without a body anchors when the writer returns. Payload
bytes cannot trigger it. Cancellation can also close the body without an error, but
the response is then undelivered and ignores the expiry. Delayed callbacks
consume the same total budget. After completed delivery, Falcon half-closes raw
TCP writes and performs bounded discard. When discard stops,
Falcon waits only the remaining part of that original 500 ms budget, without
reading more. It does not add another 500 ms after discard. Earlier owner
abort/shutdown can shorten either phase. Failed or incomplete delivery gets no
Expand Down
50 changes: 28 additions & 22 deletions gems/connectrpc-falcon/lib/connectrpc/falcon/http1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,28 @@

module ConnectRPC
module Falcon
# The pinned HTTP/1 writer flushes framing after body EOF, before close
# callbacks. Track that state without inspecting application payload bytes.
# protocol-http1 v0.37.1+ closes the body only after the response is fully
# written and flushed ("Defer `body.close` in `write_chunked_body`,
# `write_fixed_length_body`, and `write_body_and_close`"). A clean close
# after EOF or HEAD therefore marks the final flush, before close callbacks.
# EOF tracking never inspects application payload bytes.
class HTTP1ResponseBody < ::Protocol::HTTP::Body::Wrapper
attr_reader :eof
def initialize(body, head:, &final_flush)
super(body)
@head = head
@final_flush = final_flush
end

# The writer rescues only StandardError, so cancellation during the final
# flush closes with a nil error. Grace may start, but write_body raises
# and the owner is closed undelivered, which ignores grace.
def close(error = nil)
final_flush = @final_flush
@final_flush = nil
final_flush.call if final_flush && error.nil? && (@eof || @head)
ensure
super
end

def read
chunk = super
Expand Down Expand Up @@ -77,21 +95,21 @@ def each(...)
end
end

# Match the pinned writer's positional arguments.
# Match the writer's positional arguments. Unreleased protocol-http1
# adds a fifth `request` argument (socketry/protocol-http1#65).
# rubocop:disable-next Style/OptionalBooleanParameter
def write_body(version, body, head = false, trailer = nil)
if @resource.input_retirement
@response_eof = head || body.nil?
body = @response_body = HTTP1ResponseBody.new(body) if body
def write_body(version, body, head = false, trailer = nil, *rest)
if (retirement = @resource.input_retirement) && body
body = HTTP1ResponseBody.new(body, head: head) { retirement.final_flushed }
end
super
# write_empty_body flushes a nil body but has no body to close.
retirement&.final_flushed unless body
written = true
rescue StandardError
@resource.failed!
raise
ensure
@response_body = nil
@response_eof = false
# Upstream returns after final fixed/chunked bytes AND body callbacks.
begin
@resource.exchange&.finish(!!written, $ERROR_INFO)
Expand All @@ -106,18 +124,6 @@ def write_body(version, body, head = false, trailer = nil)
end
end
end

private

def flush_stream
result = super
if @response_eof || @response_body&.eof
@resource.input_retirement&.final_flushed
@response_eof = false
@response_body = nil
end
result
end
end
private_constant :HTTP1Connection

Expand Down
23 changes: 14 additions & 9 deletions gems/connectrpc-falcon/lib/connectrpc/falcon/rack_guard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ def close(*)

class CompletionBody < ::Protocol::HTTP::Body::Wrapper
def initialize(callback, original)
super(::Protocol::HTTP::Body::Buffered.wrap([]))
@callback = callback
@original = original
begin
@body = yield || @body
body = yield
converted = true
ensure
close($ERROR_INFO) unless converted
complete($ERROR_INFO) unless converted
end
super(body || ::Protocol::HTTP::Body::Buffered.wrap([]))
end

def rewindable? = false
Expand All @@ -110,14 +110,19 @@ def close(error = $ERROR_INFO)
return if @closed

@closed = true
complete(error) { super }
end

private

# Release order: completion callback, wrapped body (when built), original.
def complete(error)
@callback.call(error)
ensure
begin
@callback.call(error)
yield if block_given?
ensure
begin
super
ensure
@original&.close(error)
end
@original&.close(error)
end
end
end
Expand Down
26 changes: 23 additions & 3 deletions gems/connectrpc-falcon/spec/http1_flush_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
RSpec.describe "Connect H1 final flush ownership" do
# Exercise the pinned writer itself with a controlled output boundary and clock.
# Body callbacks run after the final flush and must spend, not restart, grace.
def writer_fixture(length: nil, head: false, version: "HTTP/1.1", absent: false, callback_delay: 0.7)
def writer_fixture(length: nil, head: false, version: "HTTP/1.1", absent: false, empty: false,
callback_delay: 0.7)
clock = [10.0]
events = []
stream = Object.new
Expand All @@ -27,7 +28,7 @@ def writer_fixture(length: nil, head: false, version: "HTTP/1.1", absent: false,
chunks = length&.zero? ? [] : ["0\r\n\r\n", "later"]
body = Object.new
body.define_singleton_method(:length) { length }
body.define_singleton_method(:empty?) { length&.zero? }
body.define_singleton_method(:empty?) { empty || length&.zero? }
body.define_singleton_method(:ready?) { false }
body.define_singleton_method(:read) do
# At the next read the terminal-looking application payload has flushed.
Expand All @@ -44,7 +45,7 @@ def writer_fixture(length: nil, head: false, version: "HTTP/1.1", absent: false,

[
{ length: nil }, { length: 10 }, { length: 0 }, { absent: true },
{ length: 10, head: true }, { head: true }, { version: "HTTP/1.0" }
{ length: 10, head: true }, { head: true }, { version: "HTTP/1.0" }, { empty: true }
].each do |options|
it "anchors the pinned writer's final flush before callbacks for #{options}" do
reactor do
Expand Down Expand Up @@ -89,6 +90,25 @@ def writer_fixture(length: nil, head: false, version: "HTTP/1.1", absent: false,
end
end

it "stays undelivered when cancellation interrupts the final flush" do
reactor do
connection, _owner, body, events, = writer_fixture
eof = false
allow(body).to receive(:read).and_wrap_original do |read|
read.call.tap { |bytes| eof = bytes.nil? }
end
allow(connection.stream).to receive(:flush).and_wrap_original do |flush|
# Not a StandardError, so the writer still closes the body with nil.
raise Async::Stop if eof

flush.call
end
expect { connection.write_body("HTTP/1.1", body) }.to raise_error(Async::Stop)
expect(events).not_to include([:shutdown, Socket::SHUT_WR])
expect(events).to include(:raw_close)
end
end

it "spends delayed callback and discard time within one total 500 ms budget" do
reactor do |task|
connection, owner, body, events, clock, = writer_fixture(callback_delay: 0.2)
Expand Down
7 changes: 4 additions & 3 deletions typecheck/surface.json
Original file line number Diff line number Diff line change
Expand Up @@ -814,13 +814,12 @@
"gems/connectrpc-falcon/lib/connectrpc/falcon/http1.rb": {
"ConnectRPC::Falcon::HTTP1Connection#close_read": {"kind":"method","visibility":"public","parameters":"","classification":"internal"},
"ConnectRPC::Falcon::HTTP1Connection#each": {"kind":"method","visibility":"public","parameters":"...","classification":"internal"},
"ConnectRPC::Falcon::HTTP1Connection#flush_stream": {"kind":"method","visibility":"private","parameters":"","classification":"internal"},
"ConnectRPC::Falcon::HTTP1Connection#next_request": {"kind":"method","visibility":"public","parameters":"","classification":"internal"},
"ConnectRPC::Falcon::HTTP1Connection#rejected_input=": {"kind":"attribute","visibility":"public","classification":"internal"},
"ConnectRPC::Falcon::HTTP1Connection#resource": {"kind":"attribute","visibility":"public","classification":"internal"},
"ConnectRPC::Falcon::HTTP1Connection#resource=": {"kind":"attribute","visibility":"public","classification":"internal"},
"ConnectRPC::Falcon::HTTP1Connection#retiring_raw=": {"kind":"attribute","visibility":"public","classification":"internal"},
"ConnectRPC::Falcon::HTTP1Connection#write_body": {"kind":"method","visibility":"public","parameters":"version, body, head = false, trailer = nil","classification":"internal"},
"ConnectRPC::Falcon::HTTP1Connection#write_body": {"kind":"method","visibility":"public","parameters":"version, body, head = false, trailer = nil, *rest","classification":"internal"},
"ConnectRPC::Falcon::HTTP1RejectedInput#abort_read": {"kind":"method","visibility":"public","parameters":"","classification":"internal"},
"ConnectRPC::Falcon::HTTP1RejectedInput#close": {"kind":"method","visibility":"public","parameters":"error = $ERROR_INFO","classification":"internal"},
"ConnectRPC::Falcon::HTTP1RejectedInput#initialize": {"kind":"method","visibility":"public","parameters":"body, raw:, connection:, headers:","classification":"internal"},
Expand Down Expand Up @@ -870,8 +869,9 @@
"ConnectRPC::Falcon::HTTP1Resource#track": {"kind":"method","visibility":"public","parameters":"task","classification":"internal"},
"ConnectRPC::Falcon::HTTP1Resource#wait": {"kind":"method","visibility":"private","parameters":"task, expires","classification":"internal"},
"ConnectRPC::Falcon::HTTP1Resource#web_input": {"kind":"method","visibility":"public","parameters":"body, headers:","classification":"internal"},
"ConnectRPC::Falcon::HTTP1ResponseBody#close": {"kind":"method","visibility":"public","parameters":"error = nil","classification":"internal"},
"ConnectRPC::Falcon::HTTP1ResponseBody#empty?": {"kind":"method","visibility":"public","parameters":"","classification":"internal"},
"ConnectRPC::Falcon::HTTP1ResponseBody#eof": {"kind":"attribute","visibility":"public","classification":"internal"},
"ConnectRPC::Falcon::HTTP1ResponseBody#initialize": {"kind":"method","visibility":"public","parameters":"body, head:, &final_flush","classification":"internal"},
"ConnectRPC::Falcon::HTTP1ResponseBody#read": {"kind":"method","visibility":"public","parameters":"","classification":"internal"},
"ConnectRPC::Falcon::HTTP1StreamInput#abort_read": {"kind":"method","visibility":"private","parameters":"","classification":"internal"},
"ConnectRPC::Falcon::HTTP1StreamInput#close": {"kind":"method","visibility":"public","parameters":"delivered","classification":"internal"},
Expand All @@ -889,6 +889,7 @@
"gems/connectrpc-falcon/lib/connectrpc/falcon/rack_guard.rb": {
"ConnectRPC::Falcon::CloseOnce#close": {"kind":"method","visibility":"public","parameters":"error = nil","classification":"internal"},
"ConnectRPC::Falcon::CompletionBody#close": {"kind":"method","visibility":"public","parameters":"error = $ERROR_INFO","classification":"internal"},
"ConnectRPC::Falcon::CompletionBody#complete": {"kind":"method","visibility":"private","parameters":"error","classification":"internal"},
"ConnectRPC::Falcon::CompletionBody#initialize": {"kind":"method","visibility":"public","parameters":"callback, original","classification":"internal"},
"ConnectRPC::Falcon::CompletionBody#rewind": {"kind":"method","visibility":"public","parameters":"","classification":"internal"},
"ConnectRPC::Falcon::CompletionBody#rewindable?": {"kind":"method","visibility":"public","parameters":"","classification":"internal"},
Expand Down