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 << 16) - 1)) ⇒ ChannelIdAllocator

Returns a new instance of ChannelIdAllocator

Parameters:

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

    Max allowed channel id



20
21
22
23
# File 'lib/bunny/channel_id_allocator.rb', line 20

def initialize(max_channel = ((1 << 16) - 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


59
60
61
62
63
# File 'lib/bunny/channel_id_allocator.rb', line 59

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


32
33
34
35
36
# File 'lib/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.

Parameters:

  • i (Integer)

    Channel id to release

See Also:

  • ChannelManager#next_channel_id
  • ChannelManager#reset_channel_id_allocator


44
45
46
47
48
# File 'lib/bunny/channel_id_allocator.rb', line 44

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


69
70
71
72
73
# File 'lib/bunny/channel_id_allocator.rb', line 69

def reset_channel_id_allocator
  @mutex.synchronize do
    @allocator.reset
  end
end