From fea9507fe85f2b6ca6754fcc559a65fb1007190f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20P=C3=A9rez?= Date: Mon, 21 Sep 2026 22:45:20 -0300 Subject: [PATCH] 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. --- src/adn_server/domain/mesh_session.py | 13 ++++++++++--- tests/domain/test_mesh_session.py | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/adn_server/domain/mesh_session.py b/src/adn_server/domain/mesh_session.py index 0a5acd0..b6cca17 100644 --- a/src/adn_server/domain/mesh_session.py +++ b/src/adn_server/domain/mesh_session.py @@ -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() diff --git a/tests/domain/test_mesh_session.py b/tests/domain/test_mesh_session.py index 8949c4b..a650d7e 100644 --- a/tests/domain/test_mesh_session.py +++ b/tests/domain/test_mesh_session.py @@ -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()