diff --git a/workspace/a2a_client.py b/workspace/a2a_client.py index 7cc79b5f..ecf60f27 100644 --- a/workspace/a2a_client.py +++ b/workspace/a2a_client.py @@ -187,12 +187,21 @@ def enrich_peer_metadata_nonblocking( canon = _validate_peer_id(peer_id) if canon is None: return None + current = time.monotonic() # Schedule background fetch unless one is already in flight for this # peer. The synchronous version atomically reads-then-writes; the # async version splits that into "schedule fetch" + "fetch fills # cache later." The in-flight set keeps a flurry of pushes from # one peer (e.g., a chatty agent) from spawning N parallel GETs. + # The cache check lives inside the lock so a concurrent cache-hit + # thread and a cache-miss thread don't both pass the in-flight gate + # before either has populated the cache. with _enrich_in_flight_lock: + cached = _peer_metadata_get(canon) + if cached is not None: + fetched_at, record = cached + if current - fetched_at < _PEER_METADATA_TTL_SECONDS: + return record if canon in _enrich_in_flight: return None _enrich_in_flight.add(canon)