Skip to content

Commit de29c13

Browse files
authored
Fix backwards compat for DirectServeJsonResource (#18600)
As that appears in the module API. Broke in #18595.
1 parent 434e389 commit de29c13

19 files changed

+56
-29
lines changed

Cargo.lock

Lines changed: 10 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

changelog.d/18600.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Better handling of ratelimited requests.

synapse/http/additional_resource.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def __init__(
5353
hs: homeserver
5454
handler: function to be called to handle the request.
5555
"""
56-
super().__init__(hs.get_clock())
56+
super().__init__(clock=hs.get_clock())
5757
self._handler = handler
5858

5959
async def _async_render(self, request: Request) -> Optional[Tuple[int, Any]]:

synapse/http/server.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,17 @@
4242
Protocol,
4343
Tuple,
4444
Union,
45+
cast,
4546
)
4647

4748
import attr
4849
import jinja2
4950
from canonicaljson import encode_canonical_json
5051
from zope.interface import implementer
5152

52-
from twisted.internet import defer, interfaces
53+
from twisted.internet import defer, interfaces, reactor
5354
from twisted.internet.defer import CancelledError
55+
from twisted.internet.interfaces import IReactorTime
5456
from twisted.python import failure
5557
from twisted.web import resource
5658

@@ -401,8 +403,15 @@ class DirectServeJsonResource(_AsyncResource):
401403
"""
402404

403405
def __init__(
404-
self, clock: Clock, canonical_json: bool = False, extract_context: bool = False
406+
self,
407+
canonical_json: bool = False,
408+
extract_context: bool = False,
409+
# Clock is optional as this class is exposed to the module API.
410+
clock: Optional[Clock] = None,
405411
):
412+
if clock is None:
413+
clock = Clock(cast(IReactorTime, reactor))
414+
406415
super().__init__(clock, extract_context)
407416
self.canonical_json = canonical_json
408417

@@ -460,7 +469,7 @@ def __init__(
460469
extract_context: bool = False,
461470
):
462471
self.clock = hs.get_clock()
463-
super().__init__(self.clock, canonical_json, extract_context)
472+
super().__init__(canonical_json, extract_context, clock=self.clock)
464473
# Map of path regex -> method -> callback.
465474
self._routes: Dict[Pattern[str], Dict[bytes, _PathEntry]] = {}
466475
self.hs = hs
@@ -573,6 +582,17 @@ class DirectServeHtmlResource(_AsyncResource):
573582
# The error template to use for this resource
574583
ERROR_TEMPLATE = HTML_ERROR_TEMPLATE
575584

585+
def __init__(
586+
self,
587+
extract_context: bool = False,
588+
# Clock is optional as this class is exposed to the module API.
589+
clock: Optional[Clock] = None,
590+
):
591+
if clock is None:
592+
clock = Clock(cast(IReactorTime, reactor))
593+
594+
super().__init__(clock, extract_context)
595+
576596
def _send_response(
577597
self,
578598
request: "SynapseRequest",

synapse/rest/consent/consent_resource.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class ConsentResource(DirectServeHtmlResource):
8181
"""
8282

8383
def __init__(self, hs: "HomeServer"):
84-
super().__init__(hs.get_clock())
84+
super().__init__(clock=hs.get_clock())
8585

8686
self.hs = hs
8787
self.store = hs.get_datastores().main

synapse/rest/synapse/client/federation_whitelist.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class FederationWhitelistResource(DirectServeJsonResource):
4444
PATH = "/_synapse/client/v1/config/federation_whitelist"
4545

4646
def __init__(self, hs: "HomeServer"):
47-
super().__init__(hs.get_clock())
47+
super().__init__(clock=hs.get_clock())
4848

4949
self._federation_whitelist = hs.config.federation.federation_domain_whitelist
5050

synapse/rest/synapse/client/jwks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
class JwksResource(DirectServeJsonResource):
3535
def __init__(self, hs: "HomeServer"):
36-
super().__init__(hs.get_clock(), extract_context=True)
36+
super().__init__(clock=hs.get_clock(), extract_context=True)
3737

3838
# Parameters that are allowed to be exposed in the public key.
3939
# This is done manually, because authlib's private to public key conversion

synapse/rest/synapse/client/new_user_consent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class NewUserConsentResource(DirectServeHtmlResource):
4848
"""
4949

5050
def __init__(self, hs: "HomeServer"):
51-
super().__init__(hs.get_clock())
51+
super().__init__(clock=hs.get_clock())
5252
self._sso_handler = hs.get_sso_handler()
5353
self._server_name = hs.hostname
5454
self._consent_version = hs.config.consent.user_consent_version

synapse/rest/synapse/client/oidc/backchannel_logout_resource.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class OIDCBackchannelLogoutResource(DirectServeJsonResource):
3535
isLeaf = 1
3636

3737
def __init__(self, hs: "HomeServer"):
38-
super().__init__(hs.get_clock())
38+
super().__init__(clock=hs.get_clock())
3939
self._oidc_handler = hs.get_oidc_handler()
4040

4141
async def _async_render_POST(self, request: SynapseRequest) -> None:

synapse/rest/synapse/client/oidc/callback_resource.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class OIDCCallbackResource(DirectServeHtmlResource):
3535
isLeaf = 1
3636

3737
def __init__(self, hs: "HomeServer"):
38-
super().__init__(hs.get_clock())
38+
super().__init__(clock=hs.get_clock())
3939
self._oidc_handler = hs.get_oidc_handler()
4040

4141
async def _async_render_GET(self, request: SynapseRequest) -> None:

0 commit comments

Comments
 (0)