1.8 KiB
Add a prefer_addr() helper
Added transport preference selection to tractor/discovery/_api.py with two new functions:
_is_local_addr(addr: Address) -> bool
Determines whether an Address is reachable on the local host:
UDSAddress: always returnsTrue(filesystem-bound, inherently local)TCPAddress: checks if._hostis a loopback IP viaipaddress.ip_address().is_loopback, then falls back to comparing against the machine’s own interface IPs viasocket.getaddrinfo(socket.gethostname(), None)
prefer_addr(addrs: list[UnwrappedAddress]) -> UnwrappedAddress
Selects the “best” transport address from a multihomed actor’s address list. Wraps each candidate via wrap_address() to get typed Address objects, then classifies into three tiers:
- UDS (same-host guaranteed, lowest overhead)
- TCP loopback / same-host IP (local network)
- TCP remote (only option for distributed)
Within each tier, the last-registered (latest) entry is preferred. Falls back to addrs[-1] if no heuristic matches.
Integration
Registrar.find_actor()in_registry.py: changed return type fromUnwrappedAddress|Nonetolist[UnwrappedAddress]|None— returns the full addr list so callers can apply transport preference.query_actor()in_api.py: now callsprefer_addr(addrs)on the list returned byRegistrar.find_actor()instead of receiving a single pre-selected addr.wait_for_actor()in_api.py: replacedaddrs[-1]withprefer_addr(addrs)for consistent transport selection.
Verification
All discovery tests pass (13/13 non-daemon). test_local.py and test_multi_program.py also pass (daemon fixture teardown failures are pre-existing and unrelated).