class Output < ::Protocol::HTTP::Body::Readable
- Inherits from
::Protocol::HTTP::Body::Readable
A response body read from the worker output channel.
Definitions
def initialize(execution, headers, metadata)
Initialize a remote response body.
Signature
-
parameter
executionExecution The owning execution.
-
parameter
headersProtocol::HTTP::Headers The response headers to receive trailers.
-
parameter
metadataHash The remote response body metadata.
Implementation
def initialize(execution, headers, metadata)
@execution = execution
@headers = headers
@length = metadata[:length]
@stream = metadata[:stream]
@mode = nil
@finished = false
end
attr :length
Signature
-
attribute
Integer | Nil The remote response body length.
def stream?
Signature
-
returns
Boolean Whether the remote body prefers direct streaming.
Implementation
def stream?
@stream
end
def empty?
Signature
-
returns
Boolean Whether the remote body has finished.
Implementation
def empty?
@finished
end
def read
Read the next response body chunk.
Implementation
def read
return nil if @finished
start(:read)
return read_output
end
def call(stream)
Stream the remote body directly through the given duplex stream.
Implementation
def call(stream)
unless stream?
return super(stream)
end
start(:stream)
input_task = @execution.stream_input(stream)
error = nil
begin
while chunk = read_output
stream.write(chunk)
stream.flush
end
rescue => error
@execution.cancel(error)
raise
ensure
input_task&.cancel
stream.close(error)
@execution.finish unless error
end
end
def close(error = nil)
Close the response body and cancel unfinished execution.
Implementation
def close(error = nil)
unless @finished
@finished = true
@execution.cancel(error)
end
end