This repository was archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
HTTP Replication Client #15470
Merged
clokep
merged 17 commits into
matrix-org:develop
from
realtyem:exp/unix-socket-repl-agent-alt
May 9, 2023
Merged
HTTP Replication Client #15470
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
4a1f2a6
Create drop-in replacement for SimpleHttpClient for use only with HTT…
realtyem 3d2f387
Changelog
realtyem a21973f
Fixup: Remove unnecessary import of HomeServer.
realtyem ccb873e
Fixup: Fix unnecessary recasting of uri.port into an int, which it al…
realtyem 13c1654
Fixup: I think this is the right way to hook up the context factory. …
realtyem a9a67ee
Fixup: Missed an argument
realtyem 96e3bfe
review: remove orphan 'endpoints' from docstring.
realtyem fedd7db
Remove redact_uri from logging during replication requests, as access…
realtyem cbafb6e
Correct some more docstring
realtyem 7e8f0f0
Apply suggestions from code review
realtyem c0096a3
Rename SimpleReplicationClient to ReplicationClient
realtyem ed42554
Restore usage of run_in_background() and update comments involved.
realtyem 3662f61
Move declaration in effort to avoid a rebase
realtyem 052abf5
Apply suggestions from code review
realtyem 144474d
Several things:
realtyem 85ef55a
Someday, lint will be the end of me
realtyem befc1c9
Clarify docstring.
clokep File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Create new `Client` for use with HTTP Replication between workers. Contributed by Jason Little. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| # Copyright 2023 The Matrix.org Foundation C.I.C. | ||
realtyem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import logging | ||
| from typing import Optional | ||
|
|
||
| from zope.interface import implementer | ||
|
|
||
| from twisted.internet import defer | ||
| from twisted.internet.endpoints import HostnameEndpoint, wrapClientTLS | ||
| from twisted.internet.interfaces import IStreamClientEndpoint | ||
| from twisted.python.failure import Failure | ||
| from twisted.web.client import URI, HTTPConnectionPool, _AgentBase | ||
| from twisted.web.error import SchemeNotSupported | ||
| from twisted.web.http_headers import Headers | ||
| from twisted.web.iweb import ( | ||
| IAgent, | ||
| IAgentEndpointFactory, | ||
| IBodyProducer, | ||
| IPolicyForHTTPS, | ||
| IResponse, | ||
| ) | ||
|
|
||
| from synapse.types import ISynapseReactor | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @implementer(IAgentEndpointFactory) | ||
| class ReplicationEndpointFactory: | ||
| """Connect to a given TCP socket""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| reactor: ISynapseReactor, | ||
| context_factory: IPolicyForHTTPS, | ||
| ) -> None: | ||
| self.reactor = reactor | ||
| self.context_factory = context_factory | ||
|
|
||
| def endpointForURI(self, uri: URI) -> IStreamClientEndpoint: | ||
| """ | ||
| This part of the factory decides what kind of endpoint is being connected to. | ||
|
|
||
| Args: | ||
| uri: The pre-parsed URI object containing all the uri data | ||
|
|
||
| Returns: The correct client endpoint object | ||
| """ | ||
| if uri.scheme in (b"http", b"https"): | ||
| endpoint = HostnameEndpoint(self.reactor, uri.host, uri.port) | ||
| if uri.scheme == b"https": | ||
| endpoint = wrapClientTLS( | ||
| self.context_factory.creatorForNetloc(uri.host, uri.port), endpoint | ||
| ) | ||
| return endpoint | ||
| else: | ||
| raise SchemeNotSupported(f"Unsupported scheme: {uri.scheme!r}") | ||
|
|
||
|
|
||
| @implementer(IAgent) | ||
| class ReplicationAgent(_AgentBase): | ||
| """ | ||
| Client for connecting to replication endpoints via HTTP and HTTPS. | ||
|
|
||
| Much of this code is copied from Twisted's twisted.web.client.Agent. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| reactor: ISynapseReactor, | ||
| contextFactory: IPolicyForHTTPS, | ||
| connectTimeout: Optional[float] = None, | ||
| bindAddress: Optional[bytes] = None, | ||
| pool: Optional[HTTPConnectionPool] = None, | ||
| ): | ||
| """ | ||
| Create a ReplicationAgent. | ||
|
|
||
| Args: | ||
| reactor: A reactor for this Agent to place outgoing connections. | ||
| contextFactory: A factory for TLS contexts, to control the | ||
| verification parameters of OpenSSL. The default is to use a | ||
| BrowserLikePolicyForHTTPS, so unless you have special | ||
| requirements you can leave this as-is. | ||
| connectTimeout: The amount of time that this Agent will wait | ||
| for the peer to accept a connection. | ||
| bindAddress: The local address for client sockets to bind to. | ||
| pool: An HTTPConnectionPool instance, or None, in which | ||
| case a non-persistent HTTPConnectionPool instance will be | ||
| created. | ||
| """ | ||
| _AgentBase.__init__(self, reactor, pool) | ||
| endpoint_factory = ReplicationEndpointFactory(reactor, contextFactory) | ||
| self._endpointFactory = endpoint_factory | ||
|
|
||
| def request( | ||
| self, | ||
| method: bytes, | ||
| uri: bytes, | ||
| headers: Optional[Headers] = None, | ||
| bodyProducer: Optional[IBodyProducer] = None, | ||
| ) -> "defer.Deferred[IResponse]": | ||
| """ | ||
| Issue a request to the server indicated by the given uri. | ||
|
|
||
| An existing connection from the connection pool may be used or a new | ||
| one may be created. | ||
|
|
||
| Currently, HTTP and HTTPS schemes are supported in uri. | ||
|
|
||
| This is copied from twisted.web.client.Agent, except: | ||
|
|
||
| * It uses a different pool key (combining the host & port). | ||
| * It does not call _ensureValidURI(...) since it breaks on some | ||
| UNIX paths. | ||
|
|
||
| See: twisted.web.iweb.IAgent.request | ||
| """ | ||
| parsedURI = URI.fromBytes(uri) | ||
| try: | ||
| endpoint = self._endpointFactory.endpointForURI(parsedURI) | ||
| except SchemeNotSupported: | ||
| return defer.fail(Failure()) | ||
|
|
||
| # This sets the Pool key to be: | ||
| # (http(s), <host:ip>) | ||
| key = (parsedURI.scheme, parsedURI.netloc) | ||
|
|
||
| # _requestWithEndpoint comes from _AgentBase class | ||
| return self._requestWithEndpoint( | ||
| key, | ||
| endpoint, | ||
| method, | ||
| parsedURI, | ||
| headers, | ||
| bodyProducer, | ||
| parsedURI.originForm, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.