Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Allow for the configuration of max request retries and min/max retry delays in the matrix federation client #12504

Merged
merged 10 commits into from
Jun 9, 2023
1 change: 1 addition & 0 deletions changelog.d/12504.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow for the configuration of max request retries and min/max retry delays in the matrix federation client.
6 changes: 6 additions & 0 deletions synapse/config/experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,9 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:

# MSC2654: Unread counts
self.msc2654_enabled: bool = experimental.get("msc2654_enabled", False)

# Allow for the configuration of max request retries and min/max retry delays
# in the matrix federation client
self.max_long_retry_delay = experimental.get("max_long_retry_delay", 60)
self.min_retry_delay = experimental.get("min_retry_delay", 2)
self.max_long_retries = experimental.get("max_long_retries", 10)
12 changes: 7 additions & 5 deletions synapse/http/matrixfederationclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@
# need a generous limit here.
MAX_RESPONSE_SIZE = 100 * 1024 * 1024

MAX_LONG_RETRIES = 10
MAX_SHORT_RETRIES = 3
MAXINT = sys.maxsize

Expand Down Expand Up @@ -347,6 +346,9 @@ def __init__(self, hs: "HomeServer", tls_client_options_factory):
self._store = hs.get_datastores().main
self.version_string_bytes = hs.version_string.encode("ascii")
self.default_timeout = 60
self.max_long_retry_delay = hs.config.experimental.max_long_retry_delay
self.min_retry_delay = hs.config.experimental.min_retry_delay
self.max_long_retries = hs.config.experimental.max_long_retries

def schedule(x):
self.reactor.callLater(_EPSILON, x)
Expand Down Expand Up @@ -502,7 +504,7 @@ async def _send_request(
# XXX: Would be much nicer to retry only at the transaction-layer
# (once we have reliable transactions in place)
if long_retries:
retries_left = MAX_LONG_RETRIES
retries_left = self.max_long_retries
else:
retries_left = MAX_SHORT_RETRIES

Expand Down Expand Up @@ -649,12 +651,12 @@ async def _send_request(

if retries_left and not timeout:
if long_retries:
delay = 4 ** (MAX_LONG_RETRIES + 1 - retries_left)
delay = min(delay, 60)
delay = 4 ** (self.max_long_retries + 1 - retries_left)
delay = min(delay, self.max_long_retry_delay)
delay *= random.uniform(0.8, 1.4)
else:
delay = 0.5 * 2 ** (MAX_SHORT_RETRIES - retries_left)
H-Shay marked this conversation as resolved.
Show resolved Hide resolved
delay = min(delay, 2)
delay = min(delay, self.min_retry_delay)
H-Shay marked this conversation as resolved.
Show resolved Hide resolved
delay *= random.uniform(0.8, 1.4)

logger.debug(
Expand Down