Class: Async::ResourcePool
- Inherits:
-
Object
- Object
- Async::ResourcePool
show all
- Defined in:
- lib/async/resource_pool.rb
Defined Under Namespace
Classes: AlreadyOwnError, DoesNotOwnError, Error, TimeoutError
Constant Summary
collapse
- VERSION =
'0.1.0'
Instance Method Summary
collapse
Constructor Details
Returns a new instance of ResourcePool.
28
29
30
31
32
|
# File 'lib/async/resource_pool.rb', line 28
def initialize(limit)
@limit = limit
@owners = []
@waiters = []
end
|
Instance Method Details
#acquire(timeout = nil) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/async/resource_pool.rb', line 34
def acquire(timeout = nil)
raise AlreadyOwnError.new if already_acquired?
unless can_be_acquired?
timeout.nil? ? wait : wait_with_timeout(timeout)
end
@owners.push(Fiber.current)
if block_given?
begin
yield
ensure
release
end
end
nil
end
|
#already_acquired? ⇒ Boolean
60
61
62
|
# File 'lib/async/resource_pool.rb', line 60
def already_acquired?
@owners.include?(Fiber.current)
end
|
#can_be_acquired? ⇒ Boolean
64
65
66
|
# File 'lib/async/resource_pool.rb', line 64
def can_be_acquired?
@owners.size < @limit
end
|
#info ⇒ Object
68
69
70
71
72
73
74
|
# File 'lib/async/resource_pool.rb', line 68
def info
{
waiters: @waiters.size,
owners: @owners.size,
limit: @limit
}
end
|
#release ⇒ Object
54
55
56
57
58
|
# File 'lib/async/resource_pool.rb', line 54
def release
raise DoesNotOwnError.new unless already_acquired?
@owners.delete(Fiber.current)
wakeup
end
|