mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2025-05-02 19:54:53 -04:00
Respect the @cancellable
flag for ReplicationEndpoint
s (#12700)
While `ReplicationEndpoint`s register themselves via `JsonResource`, they pass a method that calls the handler, instead of the handler itself, to `register_paths`. As a result, `JsonResource` will not correctly pick up the `@cancellable` flag and we have to apply it ourselves. Signed-off-by: Sean Quah <seanq@element.io>
This commit is contained in:
parent
9d8e380d2e
commit
a559c8b0d9
4 changed files with 139 additions and 2 deletions
|
@ -26,7 +26,8 @@ from twisted.web.server import Request
|
|||
|
||||
from synapse.api.errors import HttpResponseException, SynapseError
|
||||
from synapse.http import RequestTimedOutError
|
||||
from synapse.http.server import HttpServer
|
||||
from synapse.http.server import HttpServer, is_method_cancellable
|
||||
from synapse.http.site import SynapseRequest
|
||||
from synapse.logging import opentracing
|
||||
from synapse.logging.opentracing import trace
|
||||
from synapse.types import JsonDict
|
||||
|
@ -310,6 +311,12 @@ class ReplicationEndpoint(metaclass=abc.ABCMeta):
|
|||
url_args = list(self.PATH_ARGS)
|
||||
method = self.METHOD
|
||||
|
||||
if self.CACHE and is_method_cancellable(self._handle_request):
|
||||
raise Exception(
|
||||
f"{self.__class__.__name__} has been marked as cancellable, but CACHE "
|
||||
"is set. The cancellable flag would have no effect."
|
||||
)
|
||||
|
||||
if self.CACHE:
|
||||
url_args.append("txn_id")
|
||||
|
||||
|
@ -324,7 +331,7 @@ class ReplicationEndpoint(metaclass=abc.ABCMeta):
|
|||
)
|
||||
|
||||
async def _check_auth_and_handle(
|
||||
self, request: Request, **kwargs: Any
|
||||
self, request: SynapseRequest, **kwargs: Any
|
||||
) -> Tuple[int, JsonDict]:
|
||||
"""Called on new incoming requests when caching is enabled. Checks
|
||||
if there is a cached response for the request and returns that,
|
||||
|
@ -340,8 +347,18 @@ class ReplicationEndpoint(metaclass=abc.ABCMeta):
|
|||
if self.CACHE:
|
||||
txn_id = kwargs.pop("txn_id")
|
||||
|
||||
# We ignore the `@cancellable` flag, since cancellation wouldn't interupt
|
||||
# `_handle_request` and `ResponseCache` does not handle cancellation
|
||||
# correctly yet. In particular, there may be issues to do with logging
|
||||
# context lifetimes.
|
||||
|
||||
return await self.response_cache.wrap(
|
||||
txn_id, self._handle_request, request, **kwargs
|
||||
)
|
||||
|
||||
# The `@cancellable` decorator may be applied to `_handle_request`. But we
|
||||
# told `HttpServer.register_paths` that our handler is `_check_auth_and_handle`,
|
||||
# so we have to set up the cancellable flag ourselves.
|
||||
request.is_render_cancellable = is_method_cancellable(self._handle_request)
|
||||
|
||||
return await self._handle_request(request, **kwargs)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue