Async::HTTPSourceAsyncHTTPProtocolHTTP1Finishable

class Finishable

Keeps track of whether a body is being read, and if so, waits for it to be closed.

Definitions

def initialize(body)

Initialize the finishable wrapper.

Signature

parameter body Protocol::HTTP::Body::Readable

The body to wrap.

Implementation

def initialize(body)
	super(body)
	
	@closed = Async::Variable.new
	@error = nil
	
	@reading = false
end

def reading?

Signature

returns Boolean

Whether the body has started reading.

Implementation

def reading?
	@reading
end

def read

Read the next chunk from the body.

Signature

returns String | Nil

The next chunk of data.

Implementation

def read
	@reading = true
	
	super
end

def close(error = nil)

Close the body and signal any waiting tasks.

Implementation

def close(error = nil)
	super
	
	unless @closed.resolved?
		@error = error
		@closed.value = true
	end
end

def wait(persistent = true)

Wait for the body to be fully consumed or discard it.

Signature

parameter persistent Boolean

Whether the connection will be reused.

Implementation

def wait(persistent = true)
	if @reading
		@closed.wait
	elsif persistent
		# If the connection can be reused, let's gracefully discard the body:
		self.discard
	else
		# Else, we don't care about the body, so we can close it immediately:
		self.close
	end
end

def inspect

Signature

returns String

A detailed representation of this finishable body.

Implementation

def inspect
	"#<#{self.class} closed=#{@closed} error=#{@error}> | #{super}"
end