Class: Async::HTTP::Protocol::HTTP1::Finishable

Inherits:
Protocol::HTTP::Body::Wrapper
  • Object
show all
Defined in:
lib/async/http/protocol/http1/finishable.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(body) ⇒ Finishable

Initialize the finishable wrapper.



17
18
19
20
21
22
23
24
# File 'lib/async/http/protocol/http1/finishable.rb', line 17

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

Instance Method Details

#close(error = nil) ⇒ Object

Close the body and signal any waiting tasks.



40
41
42
43
44
45
46
47
# File 'lib/async/http/protocol/http1/finishable.rb', line 40

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

#inspectObject



64
65
66
# File 'lib/async/http/protocol/http1/finishable.rb', line 64

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

#readObject

Read the next chunk from the body.



33
34
35
36
37
# File 'lib/async/http/protocol/http1/finishable.rb', line 33

def read
	@reading = true
	
	super
end

#reading?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/async/http/protocol/http1/finishable.rb', line 27

def reading?
	@reading
end

#wait(persistent = true) ⇒ Object

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



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/async/http/protocol/http1/finishable.rb', line 51

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