Skip to content

Commit 986e15c

Browse files
committed
Wrap the Rust HTTP client with make_deferred_yieldable
So downstream usage doesn't need to use `PreserveLoggingContext()` or `make_deferred_yieldable` Spawning from #18870 and #18357 (comment)
1 parent 4b43e6f commit 986e15c

File tree

5 files changed

+71
-18
lines changed

5 files changed

+71
-18
lines changed

synapse/api/auth/mas.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,14 @@
3333
UnrecognizedRequestError,
3434
)
3535
from synapse.http.site import SynapseRequest
36-
from synapse.logging.context import PreserveLoggingContext
3736
from synapse.logging.opentracing import (
3837
active_span,
3938
force_tracing,
4039
inject_request_headers,
4140
start_active_span,
4241
)
4342
from synapse.metrics import SERVER_NAME_LABEL
44-
from synapse.synapse_rust.http_client import HttpClient
43+
from synapse.synapse_rust_wrapper.http_client import HttpClient
4544
from synapse.types import JsonDict, Requester, UserID, create_requester
4645
from synapse.util import json_decoder
4746
from synapse.util.caches.cached_call import RetryOnExceptionCachedCall
@@ -229,13 +228,12 @@ async def _introspect_token(
229228
try:
230229
with start_active_span("mas-introspect-token"):
231230
inject_request_headers(raw_headers)
232-
with PreserveLoggingContext():
233-
resp_body = await self._rust_http_client.post(
234-
url=self._introspection_endpoint,
235-
response_limit=1 * 1024 * 1024,
236-
headers=raw_headers,
237-
request_body=body,
238-
)
231+
resp_body = await self._rust_http_client.post(
232+
url=self._introspection_endpoint,
233+
response_limit=1 * 1024 * 1024,
234+
headers=raw_headers,
235+
request_body=body,
236+
)
239237
except HttpResponseException as e:
240238
end_time = self._clock.time()
241239
introspection_response_timer.labels(

synapse/api/auth/msc3861_delegated.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,14 @@
3838
UnrecognizedRequestError,
3939
)
4040
from synapse.http.site import SynapseRequest
41-
from synapse.logging.context import PreserveLoggingContext
4241
from synapse.logging.opentracing import (
4342
active_span,
4443
force_tracing,
4544
inject_request_headers,
4645
start_active_span,
4746
)
4847
from synapse.metrics import SERVER_NAME_LABEL
49-
from synapse.synapse_rust.http_client import HttpClient
48+
from synapse.synapse_rust_wrapper.http_client import HttpClient
5049
from synapse.types import Requester, UserID, create_requester
5150
from synapse.util import json_decoder
5251
from synapse.util.caches.cached_call import RetryOnExceptionCachedCall
@@ -327,13 +326,12 @@ async def _introspect_token(
327326
try:
328327
with start_active_span("mas-introspect-token"):
329328
inject_request_headers(raw_headers)
330-
with PreserveLoggingContext():
331-
resp_body = await self._rust_http_client.post(
332-
url=uri,
333-
response_limit=1 * 1024 * 1024,
334-
headers=raw_headers,
335-
request_body=body,
336-
)
329+
resp_body = await self._rust_http_client.post(
330+
url=uri,
331+
response_limit=1 * 1024 * 1024,
332+
headers=raw_headers,
333+
request_body=body,
334+
)
337335
except HttpResponseException as e:
338336
end_time = self._clock.time()
339337
introspection_response_timer.labels(

synapse/synapse_rust/http_client.pyi

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ from twisted.internet.defer import Deferred
1717
from synapse.types import ISynapseReactor
1818

1919
class HttpClient:
20+
"""
21+
Since the returned deferreds don't follow Synapse logcontext rules,
22+
this is not meant to be used by Synapse code directly.
23+
24+
Use `synapse.synapse_rust_wrapper.http_client.HttpClient` instead.
25+
"""
26+
2027
def __init__(self, reactor: ISynapseReactor, user_agent: str) -> None: ...
2128
def get(self, url: str, response_limit: int) -> Deferred[bytes]: ...
2229
def post(
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# This file is licensed under the Affero General Public License (AGPL) version 3.
2+
#
3+
# Copyright (C) 2025 New Vector, Ltd
4+
#
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU Affero General Public License as
7+
# published by the Free Software Foundation, either version 3 of the
8+
# License, or (at your option) any later version.
9+
#
10+
# See the GNU Affero General Public License for more details:
11+
# <https://www.gnu.org/licenses/agpl-3.0.html>.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# This file is licensed under the Affero General Public License (AGPL) version 3.
2+
#
3+
# Copyright (C) 2025 New Vector, Ltd
4+
#
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU Affero General Public License as
7+
# published by the Free Software Foundation, either version 3 of the
8+
# License, or (at your option) any later version.
9+
#
10+
# See the GNU Affero General Public License for more details:
11+
# <https://www.gnu.org/licenses/agpl-3.0.html>.
12+
13+
14+
from typing import Mapping
15+
16+
from twisted.internet.defer import Deferred
17+
18+
from synapse.logging.context import make_deferred_yieldable
19+
from synapse.synapse_rust.http_client import HttpClient as RustHttpClient
20+
from synapse.types import ISynapseReactor
21+
22+
23+
class HttpClient:
24+
def __init__(self, reactor: ISynapseReactor, user_agent: str) -> None:
25+
self._http_client = RustHttpClient(reactor, user_agent)
26+
27+
def get(self, url: str, response_limit: int) -> Deferred[bytes]:
28+
deferred = self._http_client.get(url, response_limit)
29+
return make_deferred_yieldable(deferred)
30+
31+
def post(
32+
self,
33+
url: str,
34+
response_limit: int,
35+
headers: Mapping[str, str],
36+
request_body: str,
37+
) -> Deferred[bytes]:
38+
deferred = self._http_client.post(url, response_limit, headers, request_body)
39+
return make_deferred_yieldable(deferred)

0 commit comments

Comments
 (0)