Big services publish several A records for one name so load spreads across servers. A resolver hands them out round-robin.
resolve(name) to rotate through the A values for a name on successive calls (keep a per-name index)So a name with A records [1.1.1.1, 2.2.2.2] returns 1.1.1.1, then 2.2.2.2, then 1.1.1.1, β¦
import time
class DNSResolver:
def __init__(self, zone=None, clock=time.time):
self.zone = zone if zone is not None else {}
self.clock = clock
self._rr = {} # name -> next index
def _lookup(self, name, seen=None):
seen = seen or set()
if name in seen:
return []
seen.add(name)
records = self.zone.get(name, [])
a_values = [r["value"] for r in records if r["type"] == "A"]
if a_values:
return a_values
for r in records:
if r["type"] == "CNAME":
return self._lookup(r["value"], seen)
return []
def resolve(self, name):
"""Return the next A value round-robin, or None if there are none."""
# TODO: ask self._lookup for this name's A values and answer None when
# that list comes back empty. Otherwise pick the value sitting at the
# counter self._rr holds for this name, wrapped by modulo so a short
# list cycles, and bump that per-name counter so the next call moves on.
pass
The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.
Big services publish several A records for one name so load spreads across servers. A resolver hands them out round-robin.
resolve(name) to rotate through the A values for a name on successive calls (keep a per-name index)So a name with A records [1.1.1.1, 2.2.2.2] returns 1.1.1.1, then 2.2.2.2, then 1.1.1.1, β¦
Press Run Tests or ββ© to check your solution.