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 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 Method Details

#acquireObject



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/async/http/pool.rb', line 47

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

#closeObject



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

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

#release(resource) ⇒ Object

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



60
61
62
63
64
65
66
67
# File 'lib/async/http/pool.rb', line 60

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