Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
add srv_resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
dklimpel committed Aug 15, 2021
1 parent 0a5b8e3 commit 8232835
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
2 changes: 1 addition & 1 deletion changelog.d/10608.misc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Type hints for proxyagent.
Type hints for proxyagent and srv_resolver.
1 change: 1 addition & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ files =
synapse/http/additional_resource.py,
synapse/http/client.py,
synapse/http/federation/matrix_federation_agent.py,
synapse/http/federation/srv_resolver.py,
synapse/http/federation/well_known_resolver.py,
synapse/http/matrixfederationclient.py,
synapse/http/proxyagent.py,
Expand Down
41 changes: 23 additions & 18 deletions synapse/http/federation/srv_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import logging
import random
import time
from typing import List
from typing import Callable, Dict, List

import attr

Expand All @@ -28,7 +28,7 @@

logger = logging.getLogger(__name__)

SERVER_CACHE = {}
SERVER_CACHE: Dict = {}


@attr.s(slots=True, frozen=True)
Expand All @@ -37,26 +37,26 @@ class Server:
Our record of an individual server which can be tried to reach a destination.
Attributes:
host (bytes): target hostname
port (int):
priority (int):
weight (int):
expires (int): when the cache should expire this record - in *seconds* since
host: target hostname
port:
priority:
weight:
expires: when the cache should expire this record - in *seconds* since
the epoch
"""

host = attr.ib()
port = attr.ib()
priority = attr.ib(default=0)
weight = attr.ib(default=0)
expires = attr.ib(default=0)
host: bytes = attr.ib()
port: int = attr.ib()
priority: int = attr.ib(default=0)
weight: int = attr.ib(default=0)
expires: int = attr.ib(default=0)


def _sort_server_list(server_list):
"""Given a list of SRV records sort them into priority order and shuffle
each priority with the given weight.
"""
priority_map = {}
priority_map: Dict[int, List[Server]] = {}

for server in server_list:
priority_map.setdefault(server.priority, []).append(server)
Expand Down Expand Up @@ -103,11 +103,16 @@ class SrvResolver:
Args:
dns_client (twisted.internet.interfaces.IResolver): twisted resolver impl
cache (dict): cache object
get_time (callable): clock implementation. Should return seconds since the epoch
cache: cache object
get_time: clock implementation. Should return seconds since the epoch
"""

def __init__(self, dns_client=client, cache=SERVER_CACHE, get_time=time.time):
def __init__(
self,
dns_client=client,
cache: Dict = SERVER_CACHE,
get_time: Callable = time.time,
):
self._dns_client = dns_client
self._cache = cache
self._get_time = get_time
Expand All @@ -116,7 +121,7 @@ async def resolve_service(self, service_name: bytes) -> List[Server]:
"""Look up a SRV record
Args:
service_name (bytes): record to look up
service_name: record to look up
Returns:
a list of the SRV records, or an empty list if none found
Expand Down Expand Up @@ -158,7 +163,7 @@ async def resolve_service(self, service_name: bytes) -> List[Server]:
and answers[0].payload
and answers[0].payload.target == dns.Name(b".")
):
raise ConnectError("Service %s unavailable" % service_name)
raise ConnectError(f"Service {service_name!r} unavailable")

servers = []

Expand Down

0 comments on commit 8232835

Please sign in to comment.