Class: Async::HTTP::Pool

Inherits:
Object
  • Object
show all
Defined in:
lib/async/http/pool.rb

Overview

Pool behaviours

  • Single request per connection (HTTP/1 without keep-alive)

  • Multiple sequential requests per connection (HTTP1 with keep-alive)

  • Multiplex requests per connection (HTTP2)

In general we don’t know the policy until connection is established.

This pool doesn’t impose a maximum number of open resources, but it WILL block if there are no available resources and trying to allocate another one fails.

Resources must respond to #multiplex -> 1 or more. #reusable? -> can be used again.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(limit = nil, &block) ⇒ Pool

Returns a new instance of Pool.



38
39
40
41
42
43
44
45
# File 'lib/async/http/pool.rb', line 38

def initialize(limit = nil, &block)
	@available = {} # resource => count
	@waiting = []
	
	@limit = limit
	
	@constructor = block
end

Instance Attribute Details

#availableObject (readonly)

Returns the value of attribute available.



47
48
49
# File 'lib/async/http/pool.rb', line 47

def available
  @available
end

Instance Method Details

#acquireObject



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/async/http/pool.rb', line 53

def acquire
	resource = wait_for_next_available
	
	return resource unless block_given?
	
	begin
		yield resource
	ensure
		release(resource)
	end
end

#closeObject



75
76
77
78
# File 'lib/async/http/pool.rb', line 75

def close
	@available.each_key(&:close)
	@available.clear
end

#empty?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/async/http/pool.rb', line 49

def empty?
	@available.empty?
end

#release(resource) ⇒ Object

Make the resource available and let waiting tasks know that there is something available.



66
67
68
69
70
71
72
73
# File 'lib/async/http/pool.rb', line 66

def release(resource)
	# A resource that is not good should also not be reusable.
	if resource.reusable?
		reuse(resource)
	else
		retire(resource)
	end
end