13
13
# limitations under the License.
14
14
15
15
import logging
16
- from typing import Optional
16
+ from typing import Dict , Optional
17
17
18
18
from zope .interface import implementer
19
19
32
32
IResponse ,
33
33
)
34
34
35
+ from synapse .config .workers import InstanceLocationConfig
35
36
from synapse .types import ISynapseReactor
36
37
37
38
logger = logging .getLogger (__name__ )
@@ -44,9 +45,11 @@ class ReplicationEndpointFactory:
44
45
def __init__ (
45
46
self ,
46
47
reactor : ISynapseReactor ,
48
+ instance_map : Dict [str , InstanceLocationConfig ],
47
49
context_factory : IPolicyForHTTPS ,
48
50
) -> None :
49
51
self .reactor = reactor
52
+ self .instance_map = instance_map
50
53
self .context_factory = context_factory
51
54
52
55
def endpointForURI (self , uri : URI ) -> IStreamClientEndpoint :
@@ -58,15 +61,29 @@ def endpointForURI(self, uri: URI) -> IStreamClientEndpoint:
58
61
59
62
Returns: The correct client endpoint object
60
63
"""
61
- if uri .scheme in (b"http" , b"https" ):
62
- endpoint = HostnameEndpoint (self .reactor , uri .host , uri .port )
63
- if uri .scheme == b"https" :
64
+ # The given URI has a special scheme and includes the worker name. The
65
+ # actual connection details are pulled from the instance map.
66
+ worker_name = uri .netloc .decode ("utf-8" )
67
+ scheme = self .instance_map [worker_name ].scheme ()
68
+
69
+ if scheme in ("http" , "https" ):
70
+ endpoint = HostnameEndpoint (
71
+ self .reactor ,
72
+ self .instance_map [worker_name ].host ,
73
+ self .instance_map [worker_name ].port ,
74
+ )
75
+ if scheme == "https" :
64
76
endpoint = wrapClientTLS (
65
- self .context_factory .creatorForNetloc (uri .host , uri .port ), endpoint
77
+ # The 'port' argument below isn't actually used by the function
78
+ self .context_factory .creatorForNetloc (
79
+ self .instance_map [worker_name ].host ,
80
+ self .instance_map [worker_name ].port ,
81
+ ),
82
+ endpoint ,
66
83
)
67
84
return endpoint
68
85
else :
69
- raise SchemeNotSupported (f"Unsupported scheme: { uri . scheme !r } " )
86
+ raise SchemeNotSupported (f"Unsupported scheme: { scheme } " )
70
87
71
88
72
89
@implementer (IAgent )
@@ -80,6 +97,7 @@ class ReplicationAgent(_AgentBase):
80
97
def __init__ (
81
98
self ,
82
99
reactor : ISynapseReactor ,
100
+ instance_map : Dict [str , InstanceLocationConfig ],
83
101
contextFactory : IPolicyForHTTPS ,
84
102
connectTimeout : Optional [float ] = None ,
85
103
bindAddress : Optional [bytes ] = None ,
@@ -102,7 +120,9 @@ def __init__(
102
120
created.
103
121
"""
104
122
_AgentBase .__init__ (self , reactor , pool )
105
- endpoint_factory = ReplicationEndpointFactory (reactor , contextFactory )
123
+ endpoint_factory = ReplicationEndpointFactory (
124
+ reactor , instance_map , contextFactory
125
+ )
106
126
self ._endpointFactory = endpoint_factory
107
127
108
128
def request (
0 commit comments