Ensure that we never stop reconnecting to redis (#9391)

This commit is contained in:
Erik Johnston 2021-02-11 16:06:29 +00:00 committed by GitHub
parent 8a33d217bd
commit 6aa87f8ce3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 2 deletions

1
changelog.d/9391.bugfix Normal file
View File

@ -0,0 +1 @@
Fix bug where Synapse would occaisonally stop reconnecting after the connection was lost.

View File

@ -15,8 +15,9 @@
import logging import logging
from inspect import isawaitable from inspect import isawaitable
from typing import TYPE_CHECKING, Optional, Type, cast from typing import TYPE_CHECKING, Generic, Optional, Type, TypeVar, cast
import attr
import txredisapi import txredisapi
from synapse.logging.context import PreserveLoggingContext, make_deferred_yieldable from synapse.logging.context import PreserveLoggingContext, make_deferred_yieldable
@ -42,6 +43,24 @@ if TYPE_CHECKING:
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
T = TypeVar("T")
V = TypeVar("V")
@attr.s
class ConstantProperty(Generic[T, V]):
"""A descriptor that returns the given constant, ignoring attempts to set
it.
"""
constant = attr.ib() # type: V
def __get__(self, obj: Optional[T], objtype: Type[T] = None) -> V:
return self.constant
def __set__(self, obj: Optional[T], value: V):
pass
class RedisSubscriber(txredisapi.SubscriberProtocol, AbstractConnection): class RedisSubscriber(txredisapi.SubscriberProtocol, AbstractConnection):
"""Connection to redis subscribed to replication stream. """Connection to redis subscribed to replication stream.
@ -195,6 +214,10 @@ class SynapseRedisFactory(txredisapi.RedisFactory):
we detect dead connections. we detect dead connections.
""" """
# We want to *always* retry connecting, txredisapi will stop if there is a
# failure during certain operations, e.g. during AUTH.
continueTrying = cast(bool, ConstantProperty(True))
def __init__( def __init__(
self, self,
hs: "HomeServer", hs: "HomeServer",
@ -243,7 +266,6 @@ class RedisDirectTcpReplicationClientFactory(SynapseRedisFactory):
""" """
maxDelay = 5 maxDelay = 5
continueTrying = True
protocol = RedisSubscriber protocol = RedisSubscriber
def __init__( def __init__(