@@ -201,7 +201,7 @@ def __init__(self, host='localhost', userid='guest', password='guest',
201201 client_properties = client_properties ,
202202 )
203203 self .channels = {}
204- self ._avail_channel_ids = array ('H' , xrange ( self . channel_max , 0 , - 1 ) )
204+ self ._used_channel_ids = array ('H' )
205205 if not lazy :
206206 self .connect ()
207207
@@ -247,15 +247,21 @@ def _remove_channel(self, channel):
247247 pass
248248 self .channels .pop (channel .channel_id , None )
249249 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
251255
252256 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 ))
259265
260266 def close (self ):
261267 try :
0 commit comments