Skip to content

proxystore.endpoint.p2p.addrs

Cache of peer addresses.

The address of a peer (its home relay URL and direct addresses) is saved after each successful connection so the peer can be reached later even if discovery is unavailable, as long as the addresses of the peer have not changed.

ADDR_CACHE_VERSION module-attribute

ADDR_CACHE_VERSION = 1

Format version of the peer address cache file.

PeerAddr

Bases: BaseModel

Cached address of a peer.

Attributes:

  • relay_url (str | None) –

    URL of the home relay of the peer.

  • addresses (list[str]) –

    Direct addresses of the peer.

from_iroh classmethod

from_iroh(addr: EndpointAddr) -> PeerAddr

Create from an iroh address.

Source code in proxystore/endpoint/p2p/addrs.py
@classmethod
def from_iroh(cls, addr: iroh.EndpointAddr) -> PeerAddr:
    """Create from an iroh address."""
    return cls(
        relay_url=addr.relay_url(),
        addresses=addr.direct_addresses(),
    )

to_iroh

to_iroh(peer_id: EndpointId) -> EndpointAddr

Convert to an iroh address of the peer.

Source code in proxystore/endpoint/p2p/addrs.py
def to_iroh(self, peer_id: EndpointId) -> iroh.EndpointAddr:
    """Convert to an iroh address of the peer."""
    return iroh.EndpointAddr(
        iroh.EndpointId.from_string(peer_id),
        self.relay_url,
        self.addresses,
    )

PeerAddrCacheFile

Bases: VersionedFile

Contents of the peer address cache file.

Attributes:

PeerAddrCache

PeerAddrCache(path: str)

Cache of peer addresses stored in a JSON file.

Example
cache = PeerAddrCache(endpoint_dir.peer_addrs_path)
cache.save({peer_id: addr})
assert peer_id in cache.load()

Parameters:

  • path (str) –

    Path to the cache file.

Source code in proxystore/endpoint/p2p/addrs.py
def __init__(self, path: str) -> None:
    self.path = path

load

load() -> dict[EndpointId, EndpointAddr]

Load cached peer addresses.

The cache is only an optimization so errors are logged rather than raised.

Returns:

  • dict[EndpointId, EndpointAddr] –

    Mapping of peer IDs to addresses. The mapping is empty if the file does not exist, is malformed, or has an unsupported format version.

Source code in proxystore/endpoint/p2p/addrs.py
def load(self) -> dict[EndpointId, iroh.EndpointAddr]:
    """Load cached peer addresses.

    The cache is only an optimization so errors are logged rather than
    raised.

    Returns:
        Mapping of peer IDs to addresses. The mapping is empty if the \
        file does not exist, is malformed, or has an unsupported format \
        version.
    """
    try:
        cache = read_model(PeerAddrCacheFile, self.path)
    except FileNotFoundError:
        return {}
    except (OSError, EndpointConfigError) as e:
        logger.warning('Ignoring peer address cache: %s', e)
        return {}

    return {
        peer_id: addr.to_iroh(peer_id)
        for peer_id, addr in cache.peers.items()
    }

save

save(addrs: Mapping[EndpointId, EndpointAddr]) -> None

Atomically save peer addresses, replacing the cached addresses.

Parameters:

Source code in proxystore/endpoint/p2p/addrs.py
def save(self, addrs: Mapping[EndpointId, iroh.EndpointAddr]) -> None:
    """Atomically save peer addresses, replacing the cached addresses.

    Args:
        addrs: Mapping of peer IDs to addresses.
    """
    peers = {
        peer_id: PeerAddr.from_iroh(addr)
        for peer_id, addr in sorted(addrs.items())
    }
    write_model(self.path, PeerAddrCacheFile(peers=peers))