|
@@ -73,9 +73,24 @@ class Database(object):
|
|
|
|
|
|
|
|
@classmethod
|
|
@classmethod
|
|
|
def init_conn_redis(cls):
|
|
def init_conn_redis(cls):
|
|
|
|
|
+ """
|
|
|
|
|
+ * Added TCP Keep-alive support by passing use the socket_keepalive=True
|
|
|
|
|
+ option. Finer grain control can be achieved using the
|
|
|
|
|
+ socket_keepalive_options option which expects a dictionary with any of
|
|
|
|
|
+ the keys (socket.TCP_KEEPIDLE, socket.TCP_KEEPCNT, socket.TCP_KEEPINTVL)
|
|
|
|
|
+ and integers for values. Thanks Yossi Gottlieb.
|
|
|
|
|
+ TCP_KEEPDILE 设置连接上如果没有数据发送的话,多久后发送keepalive探测分组,单位是秒
|
|
|
|
|
+ TCP_KEEPINTVL 前后两次探测之间的时间间隔,单位是秒
|
|
|
|
|
+ TCP_KEEPCNT 关闭一个非活跃连接之前的最大重试次数
|
|
|
|
|
+ """
|
|
|
|
|
+ import socket
|
|
|
cls.r = redis.StrictRedis(host=app.config.get('redis_host', '127.0.0.1'),
|
|
cls.r = redis.StrictRedis(host=app.config.get('redis_host', '127.0.0.1'),
|
|
|
port=app.config.get('redis_port', 6379),
|
|
port=app.config.get('redis_port', 6379),
|
|
|
- db=app.config.get('redis_dbid', 0), decode_responses=True)
|
|
|
|
|
|
|
+ db=app.config.get('redis_dbid', 0), decode_responses=True, socket_timeout=5,
|
|
|
|
|
+ socket_connect_timeout=5, socket_keepalive=True,
|
|
|
|
|
+ socket_keepalive_options={socket.TCP_KEEPIDLE: 2, socket.TCP_KEEPINTVL: 5,
|
|
|
|
|
+ socket.TCP_KEEPCNT: 10},
|
|
|
|
|
+ retry_on_timeout=True)
|
|
|
|
|
|
|
|
try:
|
|
try:
|
|
|
cls.r.ping()
|
|
cls.r.ping()
|
|
@@ -84,7 +99,11 @@ class Database(object):
|
|
|
cls.r = redis.StrictRedis(host=app.config.get('redis_host', '127.0.0.1'),
|
|
cls.r = redis.StrictRedis(host=app.config.get('redis_host', '127.0.0.1'),
|
|
|
port=app.config.get('redis_port', 6379),
|
|
port=app.config.get('redis_port', 6379),
|
|
|
db=app.config.get('redis_dbid', 0), password=app.config.get('redis_password', ''),
|
|
db=app.config.get('redis_dbid', 0), password=app.config.get('redis_password', ''),
|
|
|
- decode_responses=True)
|
|
|
|
|
|
|
+ decode_responses=True, socket_timeout=5,
|
|
|
|
|
+ socket_connect_timeout=5, socket_keepalive=True,
|
|
|
|
|
+ socket_keepalive_options={socket.TCP_KEEPIDLE: 2, socket.TCP_KEEPINTVL: 5,
|
|
|
|
|
+ socket.TCP_KEEPCNT: 10},
|
|
|
|
|
+ retry_on_timeout=True)
|
|
|
|
|
|
|
|
cls.r.client_setname(ji.Common.get_hostname())
|
|
cls.r.client_setname(ji.Common.get_hostname())
|
|
|
|
|
|