Class: Bunny::ChannelIdAllocator
- Inherits:
-
Object
- Object
- Bunny::ChannelIdAllocator
- Defined in:
- lib/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 << 11) - 1)) ⇒ ChannelIdAllocator
constructor
A new instance of ChannelIdAllocator.
-
#next_channel_id ⇒ Integer
Returns next available channel id.
-
#release_channel_id(i) ⇒ Object
Releases previously allocated channel id.
-
#reset_channel_id_allocator ⇒ Object
Resets channel allocator.
Constructor Details
#initialize(max_channel = ((1 << 11) - 1)) ⇒ ChannelIdAllocator
Returns a new instance of ChannelIdAllocator.
20 21 22 23 24 25 |
# File 'lib/bunny/channel_id_allocator.rb', line 20 def initialize(max_channel = ((1 << 11) - 1)) # channel 0 has special meaning in the protocol, so start # allocator at 1 @allocator = AMQ::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.
61 62 63 64 65 |
# File 'lib/bunny/channel_id_allocator.rb', line 61 def allocated_channel_id?(i) @mutex.synchronize do @allocator.allocated?(i) end end |
#next_channel_id ⇒ Integer
Returns next available channel id. This method is thread safe.
34 35 36 37 38 |
# File 'lib/bunny/channel_id_allocator.rb', line 34 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.
46 47 48 49 50 |
# File 'lib/bunny/channel_id_allocator.rb', line 46 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.
71 72 73 74 75 |
# File 'lib/bunny/channel_id_allocator.rb', line 71 def reset_channel_id_allocator @mutex.synchronize do @allocator.reset end end |