holepunchto / hyperswarm

A distributed networking stack for connecting peers.
https://docs.holepunch.to
MIT License
1.06k stars 85 forks source link

Support independent discovery sessions #119

Closed kasperisager closed 2 years ago

kasperisager commented 2 years ago

The PeerDiscovery instance is now never surfaced to the caller who instead gets back a PeerDiscoverySession instance on swarm.join(). Multiple independant sessions may be created with different options with the underlying PeerDiscovery instance determining based on reference counts whether it should act as server, client, or both. When the last session is destroyed, the PeerDiscovery instance is also destroyed. The PeerDiscovery instance may also be destroyed directly using swarm.leave() per the existing flow.

kasperisager commented 2 years ago

One gotcha is that swarm.join() now always allocates, which wasn't previously the case. If a caller depends on swarm.join() effectively acting as discovery.refresh(), they risk unexpectedly allocating sessions that aren't cleaned up until swarm.leave() is called.

mafintosh commented 2 years ago

Nice, LGTM from me, but will let @andrewosh do a quick pass also as this is quite critical