@@ -201,7 +201,7 @@ def __init__(self, host='localhost', userid='guest', password='guest',
201
201
client_properties = client_properties ,
202
202
)
203
203
self .channels = {}
204
- self ._avail_channel_ids = array ('H' , xrange ( self . channel_max , 0 , - 1 ) )
204
+ self ._used_channel_ids = array ('H' )
205
205
if not lazy :
206
206
self .connect ()
207
207
@@ -247,15 +247,21 @@ def _remove_channel(self, channel):
247
247
pass
248
248
self .channels .pop (channel .channel_id , None )
249
249
self .callbacks .pop (channel .channel_id , None )
250
- self ._avail_channel_ids .append (channel .channel_id )
250
+ try :
251
+ self ._used_channel_ids .remove (channel .channel_id )
252
+ except ValueError :
253
+ # channel id already removed
254
+ pass
251
255
252
256
def _get_free_channel_id (self ):
253
- try :
254
- return self ._avail_channel_ids .pop ()
255
- except IndexError :
256
- raise ConnectionError (
257
- 'No free channel ids, current=%d, channel_max=%d' % (
258
- len (self .channels ), self .channel_max ))
257
+ for channel_id in range (1 , self .channel_max ):
258
+ if channel_id not in self ._used_channel_ids :
259
+ self ._used_channel_ids .append (channel_id )
260
+ return channel_id
261
+
262
+ raise ConnectionError (
263
+ 'No free channel ids, current=%d, channel_max=%d' % (
264
+ len (self .channels ), self .channel_max ))
259
265
260
266
def close (self ):
261
267
try :
0 commit comments