Class: JRPC::SharedClient::Registry
- Inherits:
-
Object
- Object
- JRPC::SharedClient::Registry
- Defined in:
- lib/jrpc/shared_client/registry.rb
Constant Summary collapse
- TERMINAL_STATES =
Signals every ticket with error and clears the registry atomically. Idempotent: tickets already in :done/:cancelled are skipped.
%i[done cancelled].freeze
Instance Method Summary collapse
- #delete(ticket) ⇒ Object
- #drain_all_with(error) ⇒ Object
-
#each_ticket(&blk) ⇒ Object
Yields each ticket while holding the registry mutex.
- #empty? ⇒ Boolean
- #fetch_and_delete(id) ⇒ Object
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
- #register(ticket) ⇒ Object
Constructor Details
#initialize ⇒ Registry
Returns a new instance of Registry.
6 7 8 9 |
# File 'lib/jrpc/shared_client/registry.rb', line 6 def initialize @mutex = Mutex.new @tickets = {} end |
Instance Method Details
#delete(ticket) ⇒ Object
19 20 21 |
# File 'lib/jrpc/shared_client/registry.rb', line 19 def delete(ticket) @mutex.synchronize { @tickets.delete(ticket.id) } end |
#drain_all_with(error) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/jrpc/shared_client/registry.rb', line 32 def drain_all_with(error) tickets = @mutex.synchronize { @tickets.values.tap { @tickets.clear } } tickets.each do |ticket| next if TERMINAL_STATES.include?(ticket.state) ticket.signal_error(error) end end |
#each_ticket(&blk) ⇒ Object
Yields each ticket while holding the registry mutex. Block must be quick — no I/O.
24 25 26 |
# File 'lib/jrpc/shared_client/registry.rb', line 24 def each_ticket(&blk) @mutex.synchronize { @tickets.each_value(&blk) } end |
#empty? ⇒ Boolean
41 42 43 |
# File 'lib/jrpc/shared_client/registry.rb', line 41 def empty? @mutex.synchronize { @tickets.empty? } end |
#fetch_and_delete(id) ⇒ Object
15 16 17 |
# File 'lib/jrpc/shared_client/registry.rb', line 15 def fetch_and_delete(id) @mutex.synchronize { @tickets.delete(id) } end |
#register(ticket) ⇒ Object
11 12 13 |
# File 'lib/jrpc/shared_client/registry.rb', line 11 def register(ticket) @mutex.synchronize { @tickets[ticket.id] = ticket } end |