1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
# -*- coding: utf-8 -*-
import threading
import time
import queue
class TooManyConnections(Exception):
'''当连接数过多时,抛出该异常'''
class Expired(Exception):
'''当连接不可用时,抛出该异常'''
class UsageExceeded(Expired):
'''连接的使用次数超出限制'''
class TtlExceeded(Expired):
'''连接使用时间超出 ttl 指定的生命周期'''
class IdleExceeded(Expired):
'''闲置时间超出 idle 指定的时间'''
class WrapperConnection(object):
'''用于包装连接池中的数据库连接,以便处理生命周期逻辑'''
def __init__(self, pool, connection):
self.pool = pool
self.connection = connection
self.usage = 0
self.last = self.created = time.time()
def using(self):
'''当连接被调用时使用该方法,使用次数 +1'''
self.usage += 1
self.last = time.time()
return self
def reset(self):
'''重置连接包装状态'''
self.usage = self.last = self.created = 0
def __enter__(self):
return self.connection
def __exit__(self, *exc_info):
self.pool.release(self)
class ConnectionPool(object):
'''连接池类,可用于 pymysql/memcache/redis/... 等
可通过如下方式调用:
pool = ConnectionPool(create=redis.Redis)
也可以通过 lambda 的方式指定 create 调用:
pool = ConnectionPool(create=lambda: redis.Redis(host='127.0.0.1'))
或者通过 functools.partial
from functools import partial
pool = ConnectionPool(create=partial(redis.Redis, host='127.0.0.1'))
'''
__wrappers = {}
def __init__(self, create, close=None, max_size=10, max_usage=0, ttl=0, idle=60, block=True):
'''初始化参数
create: 必须是一个可 callback 的函数
must be a callback function, creates connection
close: optional callback to close connection
max_size: 最大连接数,当为 0 的时候则没有限制,不建议设置为 0
the maximum number of connections, 0 is no limit (not recommended)
max_usage: 连接可使用次数,达到该次数后,连接将被释放/关闭
the number of times the connection can be used,
after this number of times, the connection will be released/closed
ttl: 连接可使用时间,单位(秒),当连接使用达到指定时间后,
连接将被释放/关闭
connection use time limit (seconds), when reached, the connection
will be released/closed
idle: 连接空闲时间,单位(秒),当连接在闲置指定时间后,将被释放/关闭
connection idle time (seconds), when reached, the connection
will be released/closed
block: 当连接数满的时候,是否阻塞等待连接被释放,
输入 False 则在连接池满时会抛出异常
When the number of connections is full,
whether to block and wait for the connection to be released.
Enter False to throw an exception when the connection pool is full
'''
if not hasattr(create, '__call__'):
raise ValueError('"create" argument is not callable')
if close is not None and not hasattr(close, '__call__'):
raise ValueError('"close" argument is not callable')
self._create = create
self._close = close
self._max_size = int(max_size)
self._max_usage = int(max_usage)
self._ttl = int(ttl)
self._idle = int(idle)
self._block = bool(block)
self._lock = threading.Condition()
self._pool = queue.Queue()
self._size = 0
def item(self):
'''可通过 with ... as ... 语法调用
pool = ConnectionPool(create=redis.Redis)
with pool.item() as redis:
redis.set('foo', 'bar)
'''
self._lock.acquire()
try:
while (self._max_size and self._pool.empty() and self._size >= self._max_size):
if not self._block:
raise TooManyConnections('Too many connections')
self._lock.wait() # 等待闲置连接
try:
wrapped = self._pool.get_nowait() # 从空闲连接池中获取一个
if self._idle and (wrapped.last + self._idle) < time.time():
self._destroy(wrapped)
raise IdleExceeded('Idle exceeds %d secs' % self._idle)
except (queue.Empty, IdleExceeded):
wrapped = self._wrapper(self._create()) # 创建新连接
self._size += 1
finally:
self._lock.release()
return wrapped.using()
def release(self, conn):
'''释放一个连接,让连接重回到连接池中
当连接使用超过限制/超过限定时间时,连接将被销毁
'''
self._lock.acquire()
wrapped = self._wrapper(conn)
try:
self._test(wrapped)
except Expired:
self._destroy(wrapped)
else:
self._pool.put_nowait(wrapped)
self._lock.notifyAll() # 通知其它线程,有闲置连接可用
finally:
self._lock.release()
def _destroy(self, wrapped):
'''销毁一个连接'''
if self._close:
self._close(wrapped.connection)
self._unwrapper(wrapped)
self._size -= 1
def _wrapper(self, conn):
'''利用 id 地址,对连接进行包装'''
if isinstance(conn, WrapperConnection):
return conn
_id = id(conn)
if _id not in self.__wrappers:
self.__wrappers[_id] = WrapperConnection(self, conn)
return self.__wrappers[_id]
def _unwrapper(self, wrapped):
'''取消对连接的包装'''
if not isinstance(wrapped, WrapperConnection):
return
_id = id(wrapped.connection)
wrapped.reset()
del wrapped
if _id in self.__wrappers:
del self.__wrappers[_id]
def _test(self, wrapped):
'''测试连接的可用性,当不可用时,抛出 Expired 异常'''
if self._max_usage and wrapped.usage >= self._max_usage:
raise UsageExceeded('Usage exceeds %d times' % self._max_usage)
if self._ttl and (wrapped.created + self._ttl) < time.time():
raise TtlExceeded('TTL exceeds %d secs' % self._ttl)
|