Class: GorgonBunny::ChannelIdAllocator
- Inherits:
-
Object
- Object
- GorgonBunny::ChannelIdAllocator
- Defined in:
- lib/gorgon_bunny/lib/gorgon_bunny/channel_id_allocator.rb
Overview
Bitset-based channel id allocator. When channels are closed, ids are released back to the pool.
Every connection has its own allocator.
Allocating and releasing ids is synchronized and can be performed from multiple threads.
Instance Method Summary collapse
-
#allocated_channel_id?(i) ⇒ Boolean
Returns true if given channel id has been previously allocated and not yet released.
-
#initialize(max_channel = ((1 << 16) - 1)) ⇒ ChannelIdAllocator
constructor
A new instance of ChannelIdAllocator.
-
#next_channel_id ⇒ Fixnum
Returns next available channel id.
-
#release_channel_id(i) ⇒ Object
Releases previously allocated channel id.
-
#reset_channel_id_allocator ⇒ Object
Resets channel allocator.
- #synchronize(&block) ⇒ Object
Constructor Details
#initialize(max_channel = ((1 << 16) - 1)) ⇒ ChannelIdAllocator
Returns a new instance of ChannelIdAllocator.
20 21 22 23 |
# File 'lib/gorgon_bunny/lib/gorgon_bunny/channel_id_allocator.rb', line 20 def initialize(max_channel = ((1 << 16) - 1)) @allocator = GorgonAMQ::IntAllocator.new(1, max_channel) @mutex = Monitor.new end |
Instance Method Details
#allocated_channel_id?(i) ⇒ Boolean
Returns true if given channel id has been previously allocated and not yet released. This method is thread safe.
59 60 61 62 63 |
# File 'lib/gorgon_bunny/lib/gorgon_bunny/channel_id_allocator.rb', line 59 def allocated_channel_id?(i) @mutex.synchronize do @allocator.allocated?(i) end end |
#next_channel_id ⇒ Fixnum
Returns next available channel id. This method is thread safe.
32 33 34 35 36 |
# File 'lib/gorgon_bunny/lib/gorgon_bunny/channel_id_allocator.rb', line 32 def next_channel_id @mutex.synchronize do @allocator.allocate end end |
#release_channel_id(i) ⇒ Object
Releases previously allocated channel id. This method is thread safe.
44 45 46 47 48 |
# File 'lib/gorgon_bunny/lib/gorgon_bunny/channel_id_allocator.rb', line 44 def release_channel_id(i) @mutex.synchronize do @allocator.release(i) end end |
#reset_channel_id_allocator ⇒ Object
Resets channel allocator. This method is thread safe.
69 70 71 72 73 |
# File 'lib/gorgon_bunny/lib/gorgon_bunny/channel_id_allocator.rb', line 69 def reset_channel_id_allocator @mutex.synchronize do @allocator.reset end end |
#synchronize(&block) ⇒ Object
76 77 78 |
# File 'lib/gorgon_bunny/lib/gorgon_bunny/channel_id_allocator.rb', line 76 def synchronize(&block) @mutex.synchronize(&block) end |