Skip to content

Commit

Permalink
Add logging for redis connection setup (matrix-org#9590)
Browse files Browse the repository at this point in the history
  • Loading branch information
richvdh authored Mar 11, 2021
1 parent e55bd0e commit 464e5da
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.d/9590.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add logging for redis connection setup.
4 changes: 3 additions & 1 deletion stubs/txredisapi.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"""
from typing import Any, List, Optional, Type, Union

from twisted.internet import protocol

class RedisProtocol:
def publish(self, channel: str, message: bytes): ...
async def ping(self) -> None: ...
Expand Down Expand Up @@ -52,7 +54,7 @@ def lazyConnection(

class ConnectionHandler: ...

class RedisFactory:
class RedisFactory(protocol.ReconnectingClientFactory):
continueTrying: bool
handler: RedisProtocol
pool: List[RedisProtocol]
Expand Down
35 changes: 35 additions & 0 deletions synapse/replication/tcp/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
import attr
import txredisapi

from twisted.internet.address import IPv4Address, IPv6Address
from twisted.internet.interfaces import IAddress, IConnector
from twisted.python.failure import Failure

from synapse.logging.context import PreserveLoggingContext, make_deferred_yieldable
from synapse.metrics.background_process_metrics import (
BackgroundProcessLoggingContext,
Expand Down Expand Up @@ -253,6 +257,37 @@ async def _send_ping(self):
except Exception:
logger.warning("Failed to send ping to a redis connection")

# ReconnectingClientFactory has some logging (if you enable `self.noisy`), but
# it's rubbish. We add our own here.

def startedConnecting(self, connector: IConnector):
logger.info(
"Connecting to redis server %s", format_address(connector.getDestination())
)
super().startedConnecting(connector)

def clientConnectionFailed(self, connector: IConnector, reason: Failure):
logger.info(
"Connection to redis server %s failed: %s",
format_address(connector.getDestination()),
reason.value,
)
super().clientConnectionFailed(connector, reason)

def clientConnectionLost(self, connector: IConnector, reason: Failure):
logger.info(
"Connection to redis server %s lost: %s",
format_address(connector.getDestination()),
reason.value,
)
super().clientConnectionLost(connector, reason)


def format_address(address: IAddress) -> str:
if isinstance(address, (IPv4Address, IPv6Address)):
return "%s:%i" % (address.host, address.port)
return str(address)


class RedisDirectTcpReplicationClientFactory(SynapseRedisFactory):
"""This is a reconnecting factory that connects to redis and immediately
Expand Down

0 comments on commit 464e5da

Please sign in to comment.