Files
synapse/synapse/synapse_rust/http_client.pyi
T
Erik JohnstonandClaude Opus 5 9b5697d378 Move per-homeserver Rust state into a RustRuntime object on the HomeServer (#20011)
Previously the tokio runtime was stashed in a hidden attribute on the
reactor object, installed lazily by whichever Rust code first needed it,
and started via `callWhenRunning`.

Instead, we create a `RustRuntime` (accessible via
`HomeServer.get_rust_runtime()`) that holds any per-reactor Rust state,
such as the tokio runtime. It is constructed lazily on use. Rust
consumers (`HttpClient`, `VersionsHandler`, the Python DB pool wrapper)
now receive the runtime or reactor handle explicitly, and the
`reactor.run()` / manual-startup workarounds in tests are no longer
needed.

We also add helper wrappers in Rust for `Reactor` and `HomeServer` that
exposes the needed functionality.

The aim is to allow us to have a Rust-side clock (mainly to get the
current time), that respects the unit test per-reactor time management.

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-21 18:48:37 +01:00

51 lines
1.6 KiB
Python

# This file is licensed under the Affero General Public License (AGPL) version 3.
#
# Copyright (C) 2025 New Vector, Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# See the GNU Affero General Public License for more details:
# <https://www.gnu.org/licenses/agpl-3.0.html>.
from typing import Mapping
from twisted.internet.defer import Deferred
from synapse.synapse_rust.runtime import RustRuntime
class HttpClient:
"""
The returned deferreds follow Synapse logcontext rules.
"""
def __init__(
self,
runtime: RustRuntime,
user_agent: str,
http2_only: bool = False,
) -> None:
"""
Create a new HTTP client backed by reqwest.
Args:
runtime: The per-homeserver Rust state (`hs.get_rust_runtime()`)
user_agent: The user agent to use for requests
http2_only: Whether to use HTTP/2 only, even on unencrypted connections. By
default, it will always use HTTP/1.1 over unencrypted connections, and
rely on TLS ALPN to negotiate HTTP/2.
Ensure the upstream server supports HTTP/2 before enabling this.
"""
def get(self, url: str, response_limit: int) -> Deferred[bytes]: ...
def post(
self,
url: str,
response_limit: int,
headers: Mapping[str, str],
request_body: str,
) -> Deferred[bytes]: ...