Skip to content
This repository was archived by the owner on Sep 11, 2022. It is now read-only.

Commit 98073bb

Browse files
authored
Update channel_id_allocator.rb
Don't lazy initialize mutex. You may end up with more than one mutex.
1 parent 00443a5 commit 98073bb

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

lib/amqp/channel_id_allocator.rb

+9-9
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@ module ChannelIdAllocator
33

44
MAX_CHANNELS_PER_CONNECTION = (2**16) - 1
55

6+
def initialize(*args, &block)
7+
super
8+
9+
@channel_id_mutex = Mutex.new
10+
end
11+
612
# Resets channel allocator. This method is thread safe.
713
# @api public
814
# @see Channel.next_channel_id
915
# @see Channel.release_channel_id
1016
def reset_channel_id_allocator
11-
channel_id_mutex.synchronize do
17+
@channel_id_mutex.synchronize do
1218
int_allocator.reset
1319
end
1420
end
@@ -20,7 +26,7 @@ def reset_channel_id_allocator
2026
# @see Channel.next_channel_id
2127
# @see Channel.reset_channel_id_allocator
2228
def release_channel_id(i)
23-
channel_id_mutex.synchronize do
29+
@channel_id_mutex.synchronize do
2430
int_allocator.release(i)
2531
end
2632
end
@@ -32,7 +38,7 @@ def release_channel_id(i)
3238
# @see Channel.release_channel_id
3339
# @see Channel.reset_channel_id_allocator
3440
def next_channel_id
35-
channel_id_mutex.synchronize do
41+
@channel_id_mutex.synchronize do
3642
result = int_allocator.allocate
3743
raise "No further channels available. Please open a new connection." if result < 0
3844
result
@@ -41,12 +47,6 @@ def next_channel_id
4147

4248
private
4349

44-
# @private
45-
# @api private
46-
def channel_id_mutex
47-
@channel_id_mutex ||= Mutex.new
48-
end
49-
5050
# @private
5151
def int_allocator
5252
# TODO: ideally, this should be in agreement with agreed max number of channels of the connection,

0 commit comments

Comments
 (0)