|
13 | 13 | import hglib |
14 | 14 | import structlog |
15 | 15 | from redis import asyncio as aioredis |
| 16 | +from redis.asyncio.retry import Retry |
| 17 | +from redis.backoff import ExponentialBackoff |
| 18 | +from redis.exceptions import ConnectionError |
| 19 | +from redis.exceptions import ExecAbortError |
| 20 | +from redis.exceptions import PubSubError |
| 21 | +from redis.exceptions import RedisError |
| 22 | +from redis.exceptions import ResponseError |
| 23 | +from redis.exceptions import TimeoutError |
| 24 | +from redis.exceptions import WatchError |
16 | 25 |
|
17 | 26 | log = structlog.get_logger(__name__) |
18 | 27 |
|
@@ -175,16 +184,33 @@ class AsyncRedis(object): |
175 | 184 | @staticmethod |
176 | 185 | async def connect(): |
177 | 186 | if AsyncRedis.redis.get(None) is None: |
| 187 | + retry_on_error = [ |
| 188 | + ConnectionError, |
| 189 | + ExecAbortError, |
| 190 | + PubSubError, |
| 191 | + RedisError, |
| 192 | + ResponseError, |
| 193 | + TimeoutError, |
| 194 | + WatchError, |
| 195 | + ] |
| 196 | + |
| 197 | + RETRIES = 5 |
178 | 198 | AsyncRedis.redis.set( |
179 | 199 | await aioredis.from_url( |
180 | 200 | os.environ["REDIS_TLS_URL"], |
181 | 201 | decode_responses=False, |
182 | 202 | ssl_cert_reqs=None, |
| 203 | + health_check_interval=30, |
| 204 | + retry_on_error=retry_on_error, |
| 205 | + retry=Retry(ExponentialBackoff(cap=7, base=1), RETRIES), |
183 | 206 | ) |
184 | 207 | if os.getenv("REDIS_TLS_URL", None) is not None |
185 | 208 | else await aioredis.from_url( |
186 | 209 | os.environ["REDIS_URL"], |
187 | 210 | decode_responses=False, |
| 211 | + health_check_interval=30, |
| 212 | + retry_on_error=retry_on_error, |
| 213 | + retry=Retry(ExponentialBackoff(cap=7, base=1), RETRIES), |
188 | 214 | ) |
189 | 215 | ) |
190 | 216 | return AsyncRedis.redis.get() |
0 commit comments