File tree Expand file tree Collapse file tree 2 files changed +12
-7
lines changed Expand file tree Collapse file tree 2 files changed +12
-7
lines changed Original file line number Diff line number Diff line change @@ -419,18 +419,15 @@ def release(self, connection):
419
419
connection .in_use = False
420
420
421
421
def in_use_connection_count (self , address ):
422
+ """ Count the number of connections currently in use to a given
423
+ address.
424
+ """
422
425
try :
423
426
connections = self .connections [address ]
424
427
except KeyError :
425
428
return 0
426
429
else :
427
- in_use_count = 0
428
-
429
- for connection in list (connections ):
430
- if connection .in_use :
431
- in_use_count += 1
432
-
433
- return in_use_count
430
+ return sum (1 if connection .in_use else 0 for connection in connections )
434
431
435
432
def remove (self , address ):
436
433
""" Remove an address from the connection pool, if present, closing
Original file line number Diff line number Diff line change @@ -108,3 +108,11 @@ def test_cannot_acquire_after_close(self):
108
108
pool .close ()
109
109
with self .assertRaises (ServiceUnavailable ):
110
110
_ = pool .acquire_direct ("X" )
111
+
112
+ def test_in_use_count (self ):
113
+ address = ("127.0.0.1" , 7687 )
114
+ self .assertEqual (self .pool .in_use_connection_count (address ), 0 )
115
+ connection = self .pool .acquire_direct (address )
116
+ self .assertEqual (self .pool .in_use_connection_count (address ), 1 )
117
+ self .pool .release (connection )
118
+ self .assertEqual (self .pool .in_use_connection_count (address ), 0 )
You can’t perform that action at this time.
0 commit comments