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

Commit

Permalink
Clean up some code in the retry logic (#6017)
Browse files Browse the repository at this point in the history
  • Loading branch information
anoadragon453 committed Feb 25, 2020
2 parents 16bf01b + 7902bf1 commit df6b120
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 36 deletions.
1 change: 1 addition & 0 deletions changelog.d/6017.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Clean up some code in the retry logic.
20 changes: 0 additions & 20 deletions synapse/storage/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,26 +250,6 @@ def _set_destination_retry_timings(
},
)

def get_destinations_needing_retry(self):
"""Get all destinations which are due a retry for sending a transaction.
Returns:
list: A list of dicts
"""

return self.runInteraction(
"get_destinations_needing_retry", self._get_destinations_needing_retry
)

def _get_destinations_needing_retry(self, txn):
query = (
"SELECT * FROM destinations"
" WHERE retry_last_ts > 0 and retry_next_ts < ?"
)

txn.execute(query, (self._clock.time_msec(),))
return self.cursor_to_dict(txn)

def _start_cleanup_transactions(self):
return run_as_background_process(
"cleanup_transactions", self._cleanup_transactions
Expand Down
29 changes: 13 additions & 16 deletions synapse/util/retryutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@

logger = logging.getLogger(__name__)

# the intial backoff, after the first transaction fails
MIN_RETRY_INTERVAL = 10 * 60 * 1000

# how much we multiply the backoff by after each subsequent fail
RETRY_MULTIPLIER = 5

# a cap on the backoff
MAX_RETRY_INTERVAL = 24 * 60 * 60 * 1000


class NotRetryingDestination(Exception):
def __init__(self, retry_last_ts, retry_interval, destination):
Expand Down Expand Up @@ -112,9 +121,6 @@ def __init__(
clock,
store,
retry_interval,
min_retry_interval=10 * 60 * 1000,
max_retry_interval=24 * 60 * 60 * 1000,
multiplier_retry_interval=5,
backoff_on_404=False,
backoff_on_failure=True,
):
Expand All @@ -130,12 +136,6 @@ def __init__(
retry_interval (int): The next retry interval taken from the
database in milliseconds, or zero if the last request was
successful.
min_retry_interval (int): The minimum retry interval to use after
a failed request, in milliseconds.
max_retry_interval (int): The maximum retry interval to use after
a failed request, in milliseconds.
multiplier_retry_interval (int): The multiplier to use to increase
the retry interval after a failed request.
backoff_on_404 (bool): Back off if we get a 404
backoff_on_failure (bool): set to False if we should not increase the
Expand All @@ -146,9 +146,6 @@ def __init__(
self.destination = destination

self.retry_interval = retry_interval
self.min_retry_interval = min_retry_interval
self.max_retry_interval = max_retry_interval
self.multiplier_retry_interval = multiplier_retry_interval
self.backoff_on_404 = backoff_on_404
self.backoff_on_failure = backoff_on_failure

Expand Down Expand Up @@ -196,13 +193,13 @@ def __exit__(self, exc_type, exc_val, exc_tb):
else:
# We couldn't connect.
if self.retry_interval:
self.retry_interval *= self.multiplier_retry_interval
self.retry_interval *= RETRY_MULTIPLIER
self.retry_interval *= int(random.uniform(0.8, 1.4))

if self.retry_interval >= self.max_retry_interval:
self.retry_interval = self.max_retry_interval
if self.retry_interval >= MAX_RETRY_INTERVAL:
self.retry_interval = MAX_RETRY_INTERVAL
else:
self.retry_interval = self.min_retry_interval
self.retry_interval = MIN_RETRY_INTERVAL

logger.info(
"Connection to %s was unsuccessful (%s(%s)); backoff now %i",
Expand Down

0 comments on commit df6b120

Please sign in to comment.