perf(obp): derive a bridge's dns_host on reload, not on every lookup

MeshSessionStore.session refreshed dns_host from the config on every call,
and deciding whether TARGET_IP is a name or an address costs a thrown and
caught ValueError for every bridge configured by name. The OBP ingress
reads self._session three to four times per datagram, so a mesh of eight
named bridges paid that exception several times per frame, per bridge.

Nothing needed it there: the config is read-only at runtime, and sync
already follows a reload. It refreshed configured_peer but not dns_host,
which is the only reason the per-lookup refresh existed, so sync now owns
both and session derives dns_host only for a session it creates.

Measured on the v5 ingress path with a bridge configured by name:
66.2us to 47.9us per frame.
pull/92/head
Rodrigo Pérez 7 days ago
parent e76d49716d
commit fea9507fe8

@ -239,7 +239,10 @@ class MeshSessionStore:
return self._sessions.get(system_name)
def session(self, system_name: str, sys_cfg: dict[str, Any] | None = None) -> ObpBridgeSession:
"""The session for this system, created from its config on first use."""
"""The session for this system, created from its config on first use.
Runs several times per datagram, so ``dns_host`` is left to ``sync``.
"""
session = self._sessions.get(system_name)
if session is None:
session = ObpBridgeSession(
@ -250,7 +253,6 @@ class MeshSessionStore:
self._sessions[system_name] = session
elif sys_cfg is not None:
session.configured_peer = _peer_from_config(sys_cfg)
session.dns_host = dns_host_from_config(sys_cfg)
return session
def drop(self, system_name: str) -> None:
@ -274,8 +276,13 @@ class MeshSessionStore:
configured = _peer_from_config(sys_cfg)
session = self._sessions.get(name)
if session is None:
self._sessions[name] = ObpBridgeSession(system_name=name, configured_peer=configured)
self._sessions[name] = ObpBridgeSession(
system_name=name,
configured_peer=configured,
dns_host=dns_host_from_config(sys_cfg),
)
continue
session.dns_host = dns_host_from_config(sys_cfg)
if session.configured_peer != configured:
session.configured_peer = configured
session.forget_learned_peer()

@ -277,6 +277,25 @@ def test_sync_registers_enabled_openbridges_only() -> None:
assert len(store) == 1
def test_sync_anchors_a_session_it_creates() -> None:
"""Missing it here loses DNS anchoring silently: nothing else derives it."""
config = _config(_TARGET_IP="peer.example.net")
store = MeshSessionStore()
store.sync(config)
assert store.session("OBP-FR").dns_host == "peer.example.net"
def test_sync_anchors_a_session_that_already_existed() -> None:
config = _config()
store = MeshSessionStore()
store.sync(config)
assert store.session("OBP-FR").dns_host is None
config["SYSTEMS"]["OBP-FR"]["_TARGET_IP"] = "peer.example.net"
store.sync(config)
assert store.session("OBP-FR").dns_host == "peer.example.net"
def test_a_reload_that_moves_the_peer_wins_over_what_was_learned() -> None:
"""Editing TARGET_IP in the YAML is the way out of a bad learned address."""
config = _config()

Loading…
Cancel
Save

Powered by TurnKey Linux.