From 6a95994098348a27b4c2a2f1ccaee67e2bd60127 Mon Sep 17 00:00:00 2001 From: pfreixes Date: Tue, 21 Jun 2016 22:15:52 +0200 Subject: [PATCH] POC fix for #732 --- redis/sentinel.py | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/redis/sentinel.py b/redis/sentinel.py index 518fec582f..a99d2e69a1 100644 --- a/redis/sentinel.py +++ b/redis/sentinel.py @@ -2,6 +2,10 @@ import random import weakref +from threading import Lock +from itertools import chain +from copy import copy + from redis.client import StrictRedis from redis.connection import ConnectionPool, Connection from redis.exceptions import (ConnectionError, ResponseError, ReadOnlyError, @@ -65,6 +69,25 @@ def read_response(self): raise ConnectionError('The previous master is now a slave') raise + def shutdown_socket(self): + """ + Thread safe function to reset the soket belonging to the connection. + The connection will end up cleaning the socket and its dependencies + in a safe way + """ + try: + self._sock.shutdown(socket.SHUT_RDWR) + except AttributeError: + # either _sock attribute does not exist or + # connection removed it. + pass + except OSError as e: + if e.errno == 107: + # Transport endpoint is not connected + pass + else: + raise + class SentinelConnectionPool(ConnectionPool): """ @@ -83,6 +106,7 @@ def __init__(self, service_name, sentinel_manager, **kwargs): self.connection_kwargs['connection_pool'] = weakref.proxy(self) self.service_name = service_name self.sentinel_manager = sentinel_manager + self._lock_get_master_address = Lock() def __repr__(self): return "%s