@@ -64,6 +64,7 @@ cdef class ThinPoolImpl(BasePoolImpl):
6464 self .homogeneous = params.homogeneous
6565 self .set_getmode(params.getmode)
6666 self .set_wait_timeout(params.wait_timeout)
67+ self .set_timeout(params.timeout)
6768 self ._stmt_cache_size = params.stmtcachesize
6869 self ._ping_interval = params.ping_interval
6970 self ._free_new_conn_impls = []
@@ -90,6 +91,7 @@ cdef class ThinPoolImpl(BasePoolImpl):
9091 conn_impl._cclass = params._default_description.cclass
9192 conn_impl._pool = self
9293 conn_impl.connect(self .connect_params)
94+ conn_impl._time_in_pool = time.monotonic()
9395 return conn_impl
9496
9597 def _bg_thread_func (self ):
@@ -257,6 +259,10 @@ cdef class ThinPoolImpl(BasePoolImpl):
257259 ThinConnImpl conn_impl
258260 ssize_t i
259261
262+ if self ._timeout > 0 :
263+ self ._timeout_helper(self ._free_new_conn_impls)
264+ self ._timeout_helper(self ._free_used_conn_impls)
265+
260266 # initialize values used in determining which connection can be
261267 # returned from the pool
262268 cclass = params._default_description.cclass
@@ -313,6 +319,19 @@ cdef class ThinPoolImpl(BasePoolImpl):
313319 self ._busy_conn_impls.remove(conn_impl)
314320 self ._condition.notify()
315321
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+
316335 def acquire (self , ConnectParamsImpl params ):
317336 """
318337 Internal method for acquiring a connection from the pool.
@@ -353,7 +372,7 @@ cdef class ThinPoolImpl(BasePoolImpl):
353372 continue
354373 if self ._ping_interval == 0 :
355374 requires_ping = True
356- elif self ._ping_interval > 0 and conn_impl._time_in_pool > 0 :
375+ elif self ._ping_interval > 0 :
357376 elapsed_time = time.monotonic() - conn_impl._time_in_pool
358377 if elapsed_time > self ._ping_interval:
359378 requires_ping = True
0 commit comments