proxystore.endpoint.p2p.manager¶
Manager of peer connections to other endpoints.
RequestHandler
module-attribute
¶
RequestHandler = Callable[
[EndpointId, Message], Awaitable[Message]
]
Handler of requests from peers.
The handler is called with the ID of the peer and the request and returns the response.
PathInfo
dataclass
¶
Network path used by a connection to a peer.
Attributes:
-
relayed(bool) –If traffic is relayed rather than sent directly to the peer.
-
remote_addr(str) –Address of the peer (or relay) on this path.
-
rtt_ms(int) –Round-trip time in milliseconds estimated by QUIC.
from_connection
classmethod
¶
from_connection(connection: Connection) -> Self | None
Get the path selected for sending on a connection.
Returns:
-
Self | None–The selected path or
Noneif the connection has no path.
Source code in proxystore/endpoint/p2p/manager.py
PeerConnection
dataclass
¶
PeerConnection(
peer_id: EndpointId,
connection: Connection,
dialed: bool,
)
Connection to a peer.
Attributes:
-
peer_id(EndpointId) –ID of the peer.
-
connection(Connection) –Underlying iroh connection.
-
dialed(bool) –If this endpoint opened the connection (rather than the peer).
report_path
¶
report_path(peer_name: str) -> None
Log the path of the connection if it changed since last reported.
Changes in only the RTT of the path are not reported.
Parameters:
-
peer_name(str) –Name of the peer for the log.
Source code in proxystore/endpoint/p2p/manager.py
watch_path
async
¶
watch_path(peer_name: str) -> None
Report changes to the path of the connection after connecting.
A new connection often starts relayed and switches to a direct path once hole-punching succeeds, so the path is checked periodically for a short time to report the change even if the connection is idle.
Parameters:
-
peer_name(str) –Name of the peer for the log.
Source code in proxystore/endpoint/p2p/manager.py
PeerPolicy
¶
Bases: Protocol
Policy of which peer endpoints an endpoint communicates with.
The PeerManager checks
the policy on each connection and request, and closes the connections to
peers which are no longer allowed. The
Allowlist is the policy of
endpoints started from an endpoint directory.
allowed
¶
allowed(peer_id: EndpointId) -> bool
PeerOptions
dataclass
¶
PeerOptions(
preset: Preset = preset_n0(),
relay_mode: RelayMode | None = None,
bind_addr: str | None = None,
connect_timeout: float = 30,
online_timeout: float | None = 10,
)
Options of the connections of a peer manager to peers.
Attributes:
-
preset(Preset) –iroh preset used to configure discovery and relays. Defaults to
iroh.preset_n0()which uses n0's public relays and DNS discovery. -
relay_mode(RelayMode | None) –Relay mode which overrides the relays of the preset or
Noneto use the relays of the preset. -
bind_addr(str | None) –Address to bind to (e.g.,
"127.0.0.1:0") orNoneto bind to all interfaces on a random port. -
connect_timeout(float) –Timeout in seconds when connecting to a peer.
-
online_timeout(float | None) –Timeout in seconds to wait for the endpoint to connect to its home relay before logging a warning. If
None, the endpoint does not wait (e.g., because relays are disabled).
from_config
classmethod
¶
from_config(config: EndpointP2PConfig) -> Self
Get the options for a peer-to-peer configuration.
The preset determines the discovery service, and the relay mode overrides the relays of the preset.
Source code in proxystore/endpoint/p2p/manager.py
CloseCode
¶
PeerManager
¶
PeerManager(
secret_key: SecretKey,
policy: PeerPolicy,
*,
options: PeerOptions | None = None,
max_request_size: int | None = None,
addr_cache: PeerAddrCache | None = None,
)
Manager of connections to peer endpoints.
The manager binds an iroh endpoint using the secret key of the ProxyStore endpoint, accepts connections from peers, and sends requests to peers. Each request is sent on its own bidirectional stream. A connection to a peer is used in both directions: requests to the peer are sent on the most recent connection, whichever endpoint opened it, and requests from the peer are accepted on every connection. Two connections to a peer only exist if both endpoints connect to each other at the same time.
The manager only communicates with peers allowed by its
PeerPolicy. Connections
from other endpoints are refused, and requests to other endpoints fail
with
PeerNotAllowedError.
The policy is checked on each connection and request, and connections to
peers which are no longer allowed are closed.
Example
Parameters:
-
secret_key(SecretKey) –Secret key of the endpoint.
-
policy(PeerPolicy) –Policy of which peers are allowed.
-
options(PeerOptions | None, default:None) –Options of connections to peers. Defaults to
PeerOptions(). -
max_request_size(int | None, default:None) –Maximum size in bytes of the data in a request from a peer or
Nonefor no limit. -
addr_cache(PeerAddrCache | None, default:None) –Optional cache where the addresses of peers are saved (see
PeerAddrCache). Cached addresses are used when connecting to peers so peers can be reached even if discovery is unavailable.
Source code in proxystore/endpoint/p2p/manager.py
endpoint
property
¶
path
¶
path(peer_id: EndpointId) -> PathInfo | None
Get the path used by the connection to a peer.
Returns:
-
PathInfo | None–The path of the connection used to send requests to the peer or
Noneif there is no open connection.
Source code in proxystore/endpoint/p2p/manager.py
addr
¶
Get the current address of this endpoint.
The address contains the ID, home relay URL, and direct addresses
of the endpoint. Other endpoints can use the address to connect to
this endpoint without discovery (see
add_peer_addr()).
Source code in proxystore/endpoint/p2p/manager.py
add_peer_addr
¶
Add a known address of a peer to use when connecting to the peer.
This is useful when discovery is unavailable.
Source code in proxystore/endpoint/p2p/manager.py
peer_name
¶
peer_name(peer_id: EndpointId) -> str
Format the ID of a peer with its name for logging.
The name is from the peer policy or unknown if the peer has no
name (see
EndpointId.log_name()).
Source code in proxystore/endpoint/p2p/manager.py
start
async
¶
start(handler: RequestHandler) -> None
Bind the endpoint and start accepting connections from peers.
Note
The iroh bindings run background threads so the manager must be started after the process is daemonized or forked.
Parameters:
-
handler(RequestHandler) –Handler of requests from peers.
Source code in proxystore/endpoint/p2p/manager.py
close
async
¶
Close all peer connections and the endpoint.
This is idempotent so it is safe to call multiple times.
Source code in proxystore/endpoint/p2p/manager.py
request
async
¶
request(peer_id: EndpointId, request: Message) -> Message
Send a request to a peer and wait for the response.
Parameters:
-
peer_id(EndpointId) –ID of the peer.
-
request(Message) –Request message.
Returns:
-
Message–The response message.
Raises:
-
PeerNotAllowedError–If the peer is not in the allowlist or the peer refused the connection.
-
PeerConnectionTimeoutError–If connecting to the peer times out.
-
PeerUnavailableError–If the request fails.