Make db_pool close connections after time-out as documented,
and fix get-after-close race condition that occurs when using TpooledConnectionPool.
This commit is contained in:
@@ -4,7 +4,9 @@ import time
|
|||||||
|
|
||||||
from eventlet.pools import Pool
|
from eventlet.pools import Pool
|
||||||
from eventlet import timeout
|
from eventlet import timeout
|
||||||
from eventlet import greenthread
|
from eventlet import hubs
|
||||||
|
from eventlet.hubs.timer import Timer
|
||||||
|
from eventlet.greenthread import GreenThread
|
||||||
|
|
||||||
|
|
||||||
class ConnectTimeout(Exception):
|
class ConnectTimeout(Exception):
|
||||||
@@ -88,8 +90,9 @@ class BaseConnectionPool(Pool):
|
|||||||
|
|
||||||
if next_delay > 0:
|
if next_delay > 0:
|
||||||
# set up a continuous self-calling loop
|
# set up a continuous self-calling loop
|
||||||
self._expiration_timer = greenthread.spawn_after(next_delay,
|
self._expiration_timer = Timer(next_delay, GreenThread(hubs.get_hub().greenlet).switch,
|
||||||
self._schedule_expiration)
|
self._schedule_expiration, [], {})
|
||||||
|
self._expiration_timer.schedule()
|
||||||
|
|
||||||
def _expire_old_connections(self, now):
|
def _expire_old_connections(self, now):
|
||||||
""" Iterates through the open connections contained in the pool, closing
|
""" Iterates through the open connections contained in the pool, closing
|
||||||
@@ -103,8 +106,6 @@ class BaseConnectionPool(Pool):
|
|||||||
conn
|
conn
|
||||||
for last_used, created_at, conn in self.free_items
|
for last_used, created_at, conn in self.free_items
|
||||||
if self._is_expired(now, last_used, created_at)]
|
if self._is_expired(now, last_used, created_at)]
|
||||||
for conn in expired:
|
|
||||||
self._safe_close(conn, quiet=True)
|
|
||||||
|
|
||||||
new_free = [
|
new_free = [
|
||||||
(last_used, created_at, conn)
|
(last_used, created_at, conn)
|
||||||
@@ -117,6 +118,9 @@ class BaseConnectionPool(Pool):
|
|||||||
# connections
|
# connections
|
||||||
self.current_size -= original_count - len(self.free_items)
|
self.current_size -= original_count - len(self.free_items)
|
||||||
|
|
||||||
|
for conn in expired:
|
||||||
|
self._safe_close(conn, quiet=True)
|
||||||
|
|
||||||
def _is_expired(self, now, last_used, created_at):
|
def _is_expired(self, now, last_used, created_at):
|
||||||
""" Returns true and closes the connection if it's expired."""
|
""" Returns true and closes the connection if it's expired."""
|
||||||
if ( self.max_idle <= 0
|
if ( self.max_idle <= 0
|
||||||
|
Reference in New Issue
Block a user