Repository: LMCache/LMCache
File: lmcache/v1/storage_backend/p2p_backend.py (Python)

Code before:
            )
            # Remove the failed socket mapping to avoid using broken connection
            self.lookup_url_to_socket_mapping.pop(peer_lookup_url, None)
            raise
        except Exception as e:
            logger.error(
                "Unexpected error recreating socket for peer_lookup_url %s "
                "(peer_init_url: %s): %s",
                peer_lookup_url,
                peer_init_url,
                e,
                exc_info=True,
            )
            self.lookup_url_to_socket_mapping.pop(peer_lookup_url, None)
            raise

    async def _ensure_peer_connection(
        self,
        peer_init_url: str,
        registration_time: float = 0.0,
    ) -> None:
        if peer_init_url in self.peer_id_to_lookup_url_mapping:
            if (
                registration_time > 0.0
                and registration_time
                != self.peer_id_to_lookup_url_mapping[peer_init_url][1]
            ):
                self._clear_peer_connection_cache(peer_init_url, registration_time)
            else:
                return
        init_side_msg = P2PInitSideMsg()
        init_ret_msg = await self.transfer_channel.async_lazy_init_peer_connection(
            local_id=self.peer_init_url,
            peer_id=peer_init_url,
            peer_init_url=peer_init_url,
            init_side_msg=init_side_msg,
        )
        assert isinstance(init_ret_msg, P2PInitSideRetMsg)
        peer_lookup_url = init_ret_msg.peer_lookup_url
        self.peer_id_to_lookup_url_mapping[peer_init_url] = (
            peer_lookup_url,
            registration_time,
        )

        lookup_socket = self._create_socket_with_timeout(
            peer_lookup_url,
            zmq.REQ,
            "connect",
        )
        self.lookup_url_to_socket_mapping[peer_lookup_url] = lookup_socket
        self.lookup_url_to_lock_mapping[peer_lookup_url] = asyncio.Lock()

Review comment (performance):
Could you add a comment here to explain [1] is the registration_time in `self.peer_id_to_lookup_url_mapping: dict[str, tuple[str, float]]`

BTW, it's good for performance to use tuple, but hard to maintain and hard for readability. Whatever, let's keep to use tuple here and discuss whether to use `NamedTuple` or any other data structure in future.

Code after:
            return BatchedLookupAndPutRetMsg(num_read_chunks=len(local_mem_objs))
        except Exception as e:
            logger.error(
                "Error during P2P batched lookup and put operation: %s",
                e,
                exc_info=True,
            )
            return BatchedLookupAndPutRetMsg(num_read_chunks=0)

    async def _ensure_peer_connection(
        self,
        peer_init_url: str,
        force_update: bool = False,
    ) -> None:
        if not force_update and peer_init_url in self.peer_id_to_lookup_url_mapping:
            return
        init_side_msg = P2PInitSideMsg()
        init_ret_msg = await self.transfer_channel.async_lazy_init_peer_connection(
            local_id=self.peer_init_url,
            peer_id=peer_init_url,
            peer_init_url=peer_init_url,
            init_side_msg=init_side_msg,
        )
        assert isinstance(init_ret_msg, P2PInitSideRetMsg)
        # update peer_lookup_url
        new_peer_lookup_url = init_ret_msg.peer_lookup_url
        old_peer_lookup_url = self.peer_id_to_lookup_url_mapping.pop(
            peer_init_url, None
        )
        self.peer_id_to_lookup_url_mapping[peer_init_url] = new_peer_lookup_url
        if old_peer_lookup_url is not None:
            if old_peer_lookup_url != new_peer_lookup_url:
                logger.info(
                    "Peer %s lookup URL changed from %s to %s",
                    peer_init_url,
                    old_peer_lookup_url,
                    new_peer_lookup_url,
                )

            # close old lookup socket
            old_lookup_socket = self.lookup_url_to_socket_mapping.pop(
                old_peer_lookup_url, None
            )
            if old_lookup_socket is not None:
                try:
                    old_lookup_socket.close(linger=0)
                except Exception as e:
                    logger.error("Error closing old lookup socket", e)

            # get lookup lock
            lookup_lock = self.lookup_url_to_lock_mapping.pop(