summary refs log tree commit diff
path: root/synapse/http/federation/srv_resolver.py
diff options
context:
space:
mode:
authorRichard van der Hoff <richard@matrix.org>2019-01-21 23:27:57 +0000
committerRichard van der Hoff <richard@matrix.org>2019-01-22 20:34:35 +0000
commit44be7513bff501055cc2e667a5cca3fb87c23f70 (patch)
tree35cf83792166a999cd68d531ded4178649caebd9 /synapse/http/federation/srv_resolver.py
parentMake key fetches use regular federation client (#4426) (diff)
downloadsynapse-44be7513bff501055cc2e667a5cca3fb87c23f70.tar.xz
MatrixFederationAgent
Pull the magic that is currently in matrix_federation_endpoint and friends into
an agent-like thing
Diffstat (limited to 'synapse/http/federation/srv_resolver.py')
-rw-r--r--synapse/http/federation/srv_resolver.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/synapse/http/federation/srv_resolver.py b/synapse/http/federation/srv_resolver.py
index c49b82c394..ded0b32832 100644
--- a/synapse/http/federation/srv_resolver.py
+++ b/synapse/http/federation/srv_resolver.py
@@ -15,6 +15,7 @@
 # limitations under the License.
 
 import logging
+import random
 import time
 
 import attr
@@ -51,6 +52,38 @@ class Server(object):
     expires = attr.ib(default=0)
 
 
+def pick_server_from_list(server_list):
+    """Randomly choose a server from the server list
+
+    Args:
+        server_list (list[Server]): list of candidate servers
+
+    Returns:
+        Tuple[bytes, int]: (host, port) pair for the chosen server
+    """
+    if not server_list:
+        raise RuntimeError("pick_server_from_list called with empty list")
+
+    # TODO: currently we only use the lowest-priority servers. We should maintain a
+    # cache of servers known to be "down" and filter them out
+
+    min_priority = min(s.priority for s in server_list)
+    eligible_servers = list(s for s in server_list if s.priority == min_priority)
+    total_weight = sum(s.weight for s in eligible_servers)
+    target_weight = random.randint(0, total_weight)
+
+    for s in eligible_servers:
+        target_weight -= s.weight
+
+        if target_weight <= 0:
+            return s.host, s.port
+
+    # this should be impossible.
+    raise RuntimeError(
+        "pick_server_from_list got to end of eligible server list.",
+    )
+
+
 @defer.inlineCallbacks
 def resolve_service(service_name, dns_client=client, cache=SERVER_CACHE, clock=time):
     """Look up a SRV record, with caching