Skip to content

Commit

Permalink
appservice: reset recoverer backoff on login
Browse files Browse the repository at this point in the history
  • Loading branch information
debdutdeb committed Oct 23, 2024
1 parent a5e16a4 commit 9bdf270
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@ book/

# Don't include users' poetry configs
/poetry.toml

data/
registration.yaml
registration.yml
13 changes: 12 additions & 1 deletion synapse/appservice/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import logging
from typing import (
TYPE_CHECKING,
Any,
Awaitable,
Callable,
Collection,
Expand Down Expand Up @@ -482,11 +483,21 @@ def __init__(
self.service = service
self.callback = callback
self.backoff_counter = 1
self._delayed_call_handler = None

def reset(self) -> None:
if self._delayed_call_handler is not None and self._delayed_call_handler.active():
self._delayed_call_handler.cancel()
self._delayed_call_handler = None

self.backoff_counter = 1

self.recover()

def recover(self) -> None:
delay = 2**self.backoff_counter
logger.info("Scheduling retries on %s in %fs", self.service.id, delay)
self.clock.call_later(
self._delayed_call_handler = self.clock.call_later(
delay, run_as_background_process, "as-recoverer", self.retry
)

Expand Down
3 changes: 3 additions & 0 deletions synapse/module_api/callbacks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

from typing import TYPE_CHECKING

from synapse.module_api.callbacks.appservice_login_callbacks import AppserviceLoginModuleApiCallbacks

if TYPE_CHECKING:
from synapse.server import HomeServer

Expand All @@ -40,3 +42,4 @@ def __init__(self, hs: "HomeServer") -> None:
self.account_validity = AccountValidityModuleApiCallbacks()
self.spam_checker = SpamCheckerModuleApiCallbacks(hs)
self.third_party_event_rules = ThirdPartyEventRulesModuleApiCallbacks(hs)
self.appservice_login = AppserviceLoginModuleApiCallbacks(hs)
21 changes: 21 additions & 0 deletions synapse/module_api/callbacks/appservice_login_callbacks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import TYPE_CHECKING, Union


if TYPE_CHECKING:
from synapse.server import HomeServer
from synapse.appservice import ApplicationService

class AppserviceLoginModuleApiCallbacks:
def __init__(self, hs: "HomeServer") -> None:
self.scheduler = hs.get_application_service_scheduler()

async def reset_recoverer_backoff(self, appservice: "ApplicationService"):
print(appservice, "resetting backoff, retrying transactions immediately")

appservice_id = appservice.id

if appservice_id not in self.scheduler.txn_ctrl.recoverers:
print(appservice, "no recoverer found to reset")
return

self.scheduler.txn_ctrl.recoverers[appservice_id].reset()
10 changes: 10 additions & 0 deletions synapse/rest/client/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#
#

from copy import copy
import logging
import re
from typing import (
Expand Down Expand Up @@ -307,6 +308,8 @@ async def _do_appservice_login(
if not appservice.is_interested_in_user(qualified_user_id):
raise LoginError(403, "Invalid access_token", errcode=Codes.FORBIDDEN)

print("logging in appservice")

return await self._complete_login(
qualified_user_id,
login_submission,
Expand All @@ -316,8 +319,15 @@ async def _do_appservice_login(
# is not actually created in Synapse.
should_check_deactivated=qualified_user_id != appservice.sender,
request_info=request_info,
callback=self._appservice_login_callback(appservice),
)

def _appservice_login_callback(self, appservice: "ApplicationService") -> Optional[Callable[[LoginResponse], Awaitable[None]]]:
async def __handle_appservice_login(_: "LoginResponse"):
await self.hs.get_module_api_callbacks().appservice_login.reset_recoverer_backoff(appservice)

return __handle_appservice_login

async def _do_other_login(
self,
login_submission: JsonDict,
Expand Down

0 comments on commit 9bdf270

Please sign in to comment.