Registration and Liveness

Step 1: Registration and Liveness

A service registry maps each service name to the set of running instances, each identified by an id and remembered by the time it was last seen. An instance stays "healthy" only for a time-to-live (TTL) window after its last sighting, if that window lapses without contact, it's presumed dead. Because tests can't rely on wall-clock time, every call takes an explicit now.

Build registration and the health check:

  • register(service, instance_id, now): record the instance under its service with last_seen = now.
  • healthy(service, now): return the sorted ids whose last sighting is within ttl of now (i.e. now - last_seen <= ttl).
Hints
Starter code
class ServiceRegistry:
    def __init__(self, ttl=10):
        self.ttl = ttl
        self.instances = {}   # service -> {instance_id: last_seen}
        self._rr = {}

    def register(self, service, instance_id, now):
        """Record (or refresh) an instance's last-seen time."""
        # TODO: self.instances.setdefault(service, {})[instance_id] = now
        pass

    def healthy(self, service, now):
        """Sorted ids seen within the TTL window."""
        svc = self.instances.get(service, {})
        # TODO: return sorted(i for i, last in svc.items() if now - last <= self.ttl)
        pass
Write it on a laptop

The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.