Class: EventMachine::Pool
- Inherits:
-
Object
- Object
- EventMachine::Pool
- Defined in:
- lib/em/pool.rb
Overview
A simple async resource pool based on a resource and work queue. Resources are enqueued and work waits for resources to become available.
Resources are expected to be controlled by an object responding to a deferrable/completion style API with callback and errback blocks.
Instance Method Summary collapse
- #add(resource) ⇒ Object
-
#contents ⇒ Object
Returns a list for introspection purposes only.
-
#initialize ⇒ Pool
constructor
A new instance of Pool.
-
#num_waiting ⇒ Object
A peek at the number of enqueued jobs waiting for resources.
-
#on_error(*a, &b) ⇒ Object
Define a default catch-all for when the deferrables returned by work blocks enter a failed state.
-
#perform(*a, &b) ⇒ Object
(also: #reschedule)
Perform a given #call-able object or block.
- #remove(resource) ⇒ Object
-
#removed?(resource) ⇒ Boolean
Removed will show resources in a partial pruned state.
Constructor Details
Instance Method Details
#add(resource) ⇒ Object
52 53 54 55 |
# File 'lib/em/pool.rb', line 52 def add resource @contents << resource requeue resource end |
#contents ⇒ Object
Returns a list for introspection purposes only. You should NEVER call modification or work oriented methods on objects in this list. A good example use case is periodic statistics collection against a set of connection resources.
69 70 71 |
# File 'lib/em/pool.rb', line 69 def contents @contents.dup end |
#num_waiting ⇒ Object
A peek at the number of enqueued jobs waiting for resources
107 108 109 |
# File 'lib/em/pool.rb', line 107 def num_waiting @resources.num_waiting end |
#on_error(*a, &b) ⇒ Object
Define a default catch-all for when the deferrables returned by work blocks enter a failed state. By default all that happens is that the resource is returned to the pool. If on_error is defined, this block is responsible for re-adding the resource to the pool if it is still usable. In other words, it is generally assumed that on_error blocks explicitly handle the rest of the lifetime of the resource.
79 80 81 |
# File 'lib/em/pool.rb', line 79 def on_error *a, &b @on_error = EM::Callback(*a, &b) end |
#perform(*a, &b) ⇒ Object Also known as: reschedule
Perform a given #call-able object or block. The callable object will be called with a resource from the pool as soon as one is available, and is expected to return a deferrable.
The deferrable will have callback and errback added such that when the deferrable enters a finished state, the object is returned to the pool.
If on_error is defined, then objects are not automatically returned to the pool.
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/em/pool.rb', line 92 def perform(*a, &b) work = EM::Callback(*a, &b) @resources.pop do |resource| if removed? resource @removed.delete resource reschedule work else process work, resource end end end |
#remove(resource) ⇒ Object
57 58 59 60 |
# File 'lib/em/pool.rb', line 57 def remove resource @contents.delete resource @removed << resource end |
#removed?(resource) ⇒ Boolean
Removed will show resources in a partial pruned state. Resources in the removed list may not appear in the contents list if they are currently in use.
114 115 116 |
# File 'lib/em/pool.rb', line 114 def removed? resource @removed.include? resource end |