@@ -64,6 +64,7 @@ cdef class ThinPoolImpl(BasePoolImpl):
64
64
self .homogeneous = params.homogeneous
65
65
self .set_getmode(params.getmode)
66
66
self .set_wait_timeout(params.wait_timeout)
67
+ self .set_timeout(params.timeout)
67
68
self ._stmt_cache_size = params.stmtcachesize
68
69
self ._ping_interval = params.ping_interval
69
70
self ._free_new_conn_impls = []
@@ -90,6 +91,7 @@ cdef class ThinPoolImpl(BasePoolImpl):
90
91
conn_impl._cclass = params._default_description.cclass
91
92
conn_impl._pool = self
92
93
conn_impl.connect(self .connect_params)
94
+ conn_impl._time_in_pool = time.monotonic()
93
95
return conn_impl
94
96
95
97
def _bg_thread_func (self ):
@@ -257,6 +259,10 @@ cdef class ThinPoolImpl(BasePoolImpl):
257
259
ThinConnImpl conn_impl
258
260
ssize_t i
259
261
262
+ if self ._timeout > 0 :
263
+ self ._timeout_helper(self ._free_new_conn_impls)
264
+ self ._timeout_helper(self ._free_used_conn_impls)
265
+
260
266
# initialize values used in determining which connection can be
261
267
# returned from the pool
262
268
cclass = params._default_description.cclass
@@ -313,6 +319,19 @@ cdef class ThinPoolImpl(BasePoolImpl):
313
319
self ._busy_conn_impls.remove(conn_impl)
314
320
self ._condition.notify()
315
321
322
+ cdef int _timeout_helper(self , list conn_impls_to_check) except - 1 :
323
+ """
324
+ Helper method which checks the free and used connection lists before
325
+ acquiring to drop off timed out connections
326
+ """
327
+ cdef ThinConnImpl conn_impl
328
+ current_time = time.monotonic()
329
+ while self .get_open_count() > self .min and conn_impls_to_check:
330
+ conn_impl = conn_impls_to_check[0 ]
331
+ if current_time - conn_impl._time_in_pool > self ._timeout:
332
+ conn_impls_to_check.pop(0 )
333
+ self ._drop_conn_impl(conn_impl)
334
+
316
335
def acquire (self , ConnectParamsImpl params ):
317
336
"""
318
337
Internal method for acquiring a connection from the pool.
@@ -353,7 +372,7 @@ cdef class ThinPoolImpl(BasePoolImpl):
353
372
continue
354
373
if self ._ping_interval == 0 :
355
374
requires_ping = True
356
- elif self ._ping_interval > 0 and conn_impl._time_in_pool > 0 :
375
+ elif self ._ping_interval > 0 :
357
376
elapsed_time = time.monotonic() - conn_impl._time_in_pool
358
377
if elapsed_time > self ._ping_interval:
359
378
requires_ping = True
0 commit comments