holepunchto / hyperswarm

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

swarm.leave(topic) doesn't drop existing peer connections on a topic #50

Open m4gpi opened 4 years ago

m4gpi commented 4 years ago

I'm attempting to use a single hyperswarm instance for multiple connections on different topics and want to be able to drop and re-start connections in a granular fashion so I can schedule replication.

@KrishnaPG mentioned a similar question in a previous issue, so forgive me if I've missed the obvious answer there:

Can the swarm.join be called repeatedly multiple times to join different topics, or should different swarm object be created once for each topic? Will duplicate connections to same peer be automatically removed in both cases?

Is it possible for me to use hyperswarm in such a manner at the moment? It seems I can create multiple open connections, but I'm unable to drop a connection once its been opened, which is the behaviour I'd expect after calling swarm.leave(topic). At the moment, when I call swarm.leave(topic) it appears not to drop existing peer connections. Do I need to manually force disconnecting / cancel auto-reconnect with each peer as you mentioned, doing something like...

swarm.on('connection', (socket, details) => {
  details.backoff()
  socket.destroy()
  return
})

my code: https://ledger-git.dyne.org/CoBox/cobox-group-base/src/commit/e6fc3e4ab8668c151cc155d9c43b46485ca3293a/index.js#L79

Thanks!

mafintosh commented 4 years ago

The swarm internally doesn’t know which incoming connections map to which topic, so unfortunately it’s up for the user to close existing ones when you leave a topic.

Leave stops announcing to the dht and stops connecting to people found on the corresponding topic

m4gpi commented 4 years ago

Great okay, so I should just be able to close the stream and it'll drop the peer connection. Thanks!