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



22
23
24
25
26
27
# File 'lib/bunny/channel_id_allocator.rb', line 22

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


63
64
65
66
67
# File 'lib/bunny/channel_id_allocator.rb', line 63

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


36
37
38
39
40
# File 'lib/bunny/channel_id_allocator.rb', line 36

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


48
49
50
51
52
# File 'lib/bunny/channel_id_allocator.rb', line 48

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


73
74
75
76
77
# File 'lib/bunny/channel_id_allocator.rb', line 73

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