Class: Bunny::ChannelIdAllocator

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(max_channel = ((1 << 11) - 1)) ⇒ ChannelIdAllocator

Returns a new instance of ChannelIdAllocator.

Parameters:

  • max_channel (Integer) (defaults to: ((1 << 11) - 1))

    Max allowed channel id



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.

Parameters:

  • i (Integer)

    Channel id to check

Returns:

  • (Boolean)

    true if given channel id has been previously allocated and not yet released

See Also:

  • ChannelManager#next_channel_id
  • ChannelManager#release_channel_id


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_idInteger

Returns next available channel id. This method is thread safe.

Returns:

  • (Integer)

See Also:

  • ChannelManager#release_channel_id
  • ChannelManager#reset_channel_id_allocator


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.

Parameters:

  • i (Integer)

    Channel id to release

See Also:

  • ChannelManager#next_channel_id
  • ChannelManager#reset_channel_id_allocator


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_allocatorObject

Resets channel allocator. This method is thread safe.

See Also:

  • Channel.next_channel_id
  • Channel.release_channel_id


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