diff options
| author | Denis Bilenko <denis.bilenko@gmail.com> | 2009-06-12 15:31:40 +0700 |
|---|---|---|
| committer | Denis Bilenko <denis.bilenko@gmail.com> | 2009-06-12 15:31:40 +0700 |
| commit | 2c4d8d7468878953ed7d744742912c1190a4e66e (patch) | |
| tree | 67e0079ac321804bf8fabc17e596fc018c1e615e /eventlet/db_pool.py | |
| parent | db2a98ea31df87a0b41f191fd50db3dac531d5f7 (diff) | |
| download | eventlet-2c4d8d7468878953ed7d744742912c1190a4e66e.tar.gz | |
kill trailing whitespace
Diffstat (limited to 'eventlet/db_pool.py')
| -rw-r--r-- | eventlet/db_pool.py | 82 |
1 files changed, 41 insertions, 41 deletions
diff --git a/eventlet/db_pool.py b/eventlet/db_pool.py index cdb29db..337d338 100644 --- a/eventlet/db_pool.py +++ b/eventlet/db_pool.py @@ -77,11 +77,11 @@ The constructor arguments: >>> dc = DatabaseConnector(MySQLdb, {'db.internal.example.com': {'user':'internal', 'passwd':'s33kr1t'}, - 'localhost': + 'localhost': {'user':'root', 'passwd':''}) - + If the credentials contain a host named 'default', then the value for 'default' is used whenever trying to connect to a host that has no explicit entry in the database. This is useful if there is some pool of hosts that share arguments. - + * conn_pool : The connection pool class to use. Defaults to db_pool.ConnectionPool. The rest of the arguments to the DatabaseConnector constructor are passed on to the ConnectionPool. @@ -103,28 +103,28 @@ class ConnectTimeout(Exception): class BaseConnectionPool(Pool): - def __init__(self, db_module, - min_size = 0, max_size = 4, + def __init__(self, db_module, + min_size = 0, max_size = 4, max_idle = 10, max_age = 30, connect_timeout = 5, *args, **kwargs): """ Constructs a pool with at least *min_size* connections and at most *max_size* connections. Uses *db_module* to construct new connections. - + The *max_idle* parameter determines how long pooled connections can - remain idle, in seconds. After *max_idle* seconds have elapsed - without the connection being used, the pool closes the connection. - + remain idle, in seconds. After *max_idle* seconds have elapsed + without the connection being used, the pool closes the connection. + *max_age* is how long any particular connection is allowed to live. Connections that have been open for longer than *max_age* seconds are - closed, regardless of idle time. If *max_age* is 0, all connections are + closed, regardless of idle time. If *max_age* is 0, all connections are closed on return to the pool, reducing it to a concurrency limiter. - - *connect_timeout* is the duration in seconds that the pool will wait + + *connect_timeout* is the duration in seconds that the pool will wait before timing out on connect() to the database. If triggered, the timeout will raise a ConnectTimeout from get(). - + The remainder of the arguments are used as parameters to the *db_module*'s connection constructor. """ @@ -136,33 +136,33 @@ class BaseConnectionPool(Pool): self.max_age = max_age self.connect_timeout = connect_timeout self._expiration_timer = None - super(BaseConnectionPool, self).__init__(min_size=min_size, + super(BaseConnectionPool, self).__init__(min_size=min_size, max_size=max_size, order_as_stack=True) - + def _schedule_expiration(self): - """ Sets up a timer that will call _expire_old_connections when the + """ Sets up a timer that will call _expire_old_connections when the oldest connection currently in the free pool is ready to expire. This is the earliest possible time that a connection could expire, thus, the - timer will be running as infrequently as possible without missing a + timer will be running as infrequently as possible without missing a possible expiration. - - If this function is called when a timer is already scheduled, it does + + If this function is called when a timer is already scheduled, it does nothing. - + If max_age or max_idle is 0, _schedule_expiration likewise does nothing. """ if self.max_age is 0 or self.max_idle is 0: # expiration is unnecessary because all connections will be expired # on put return - - if ( self._expiration_timer is not None + + if ( self._expiration_timer is not None and not getattr(self._expiration_timer, 'called', False) and not getattr(self._expiration_timer, 'cancelled', False) ): # the next timer is already scheduled - return - + return + try: now = time.time() self._expire_old_connections(now) @@ -171,23 +171,23 @@ class BaseConnectionPool(Pool): idle_delay = (self.free_items[-1][0] - now) + self.max_idle oldest = min([t[1] for t in self.free_items]) age_delay = (oldest - now) + self.max_age - + next_delay = min(idle_delay, age_delay) except IndexError, ValueError: # no free items, unschedule ourselves self._expiration_timer = None return - + if next_delay > 0: # set up a continuous self-calling loop self._expiration_timer = api.call_after(next_delay, self._schedule_expiration) - + def _expire_old_connections(self, now): """ Iterates through the open connections contained in the pool, closing ones that have remained idle for longer than max_idle seconds, or have been in existence for longer than max_age seconds. - + *now* is the current time, as returned by time.time(). """ original_count = len(self.free_items) @@ -204,8 +204,8 @@ class BaseConnectionPool(Pool): if not self._is_expired(now, last_used, created_at)] self.free_items.clear() self.free_items.extend(new_free) - - # adjust the current size counter to account for expired + + # adjust the current size counter to account for expired # connections self.current_size -= original_count - len(self.free_items) @@ -217,7 +217,7 @@ class BaseConnectionPool(Pool): or now - created_at > self.max_age ): return True return False - + def _unwrap_connection(self, conn): """ If the connection was wrapped by a subclass of BaseConnectionWrapper and is still functional (as determined @@ -250,7 +250,7 @@ class BaseConnectionPool(Pool): def get(self): conn = super(BaseConnectionPool, self).get() - + # None is a flag value that means that put got called with # something it couldn't use if conn is None: @@ -270,7 +270,7 @@ class BaseConnectionPool(Pool): _last_used, created_at, conn = conn else: created_at = time.time() - + # wrap the connection so the consumer can call close() safely wrapped = PooledConnectionWrapper(conn, self) # annotating the wrapper so that when it gets put in the pool @@ -282,7 +282,7 @@ class BaseConnectionPool(Pool): created_at = getattr(conn, '_db_pool_created_at', 0) now = time.time() conn = self._unwrap_connection(conn) - + if self._is_expired(now, now, created_at): self._safe_close(conn, quiet=False) conn = None @@ -314,7 +314,7 @@ class BaseConnectionPool(Pool): self._schedule_expiration() def clear(self): - """ Close all connections that this pool still holds a reference to, + """ Close all connections that this pool still holds a reference to, and removes all references to them. """ if self._expiration_timer: @@ -322,10 +322,10 @@ class BaseConnectionPool(Pool): free_items, self.free_items = self.free_items, deque() for _last_used, _created_at, conn in free_items: self._safe_close(conn, quiet=True) - + def __del__(self): self.clear() - + class SaranwrappedConnectionPool(BaseConnectionPool): """A pool which gives out saranwrapped database connections. @@ -343,9 +343,9 @@ class SaranwrappedConnectionPool(BaseConnectionPool): return saranwrap.wrap(db_module).connect(*args, **kw) finally: timeout.cancel() - + connect = classmethod(connect) - + class TpooledConnectionPool(BaseConnectionPool): """A pool which gives out tpool.Proxy-based database connections. @@ -462,7 +462,7 @@ class PooledConnectionWrapper(GenericConnectionWrapper): if self and self._pool: self._pool.put(self) self._destroy() - + def __del__(self): self.close() @@ -471,7 +471,7 @@ class DatabaseConnector(object): """\ @brief This is an object which will maintain a collection of database connection pools on a per-host basis.""" - def __init__(self, module, credentials, + def __init__(self, module, credentials, conn_pool=None, *args, **kwargs): """\ @brief constructor |
