nim-libp2p icon indicating copy to clipboard operation
nim-libp2p copied to clipboard

RFC: switch api overhaul

Open Menduist opened this issue 3 years ago • 0 comments

background: https://github.com/status-im/nim-libp2p/pull/668, https://github.com/status-im/nim-libp2p/pull/662

Right now, the switch is setup like so (pseudocode):

let switch = createSwitch(transports, addresses, etc)
await switch.start()
# rest of the owl
await switch.stop()

This has a few issues:

  • if one of the address fail to start (but eg, other succeeds), it's hard to convey the error to the caller
  • you can't add or remove addresses on the fly

Here is a proposal to improve this situation:

proc start(s: Switch, ma: MultiAddress) # Starts a single address
proc start(s: Switch, multipleMa = s.peerInfo.addrs) # Convenience helper around the single start
proc stop(s: Switch, ma: MultiAddress) # Stop a single address
proc stop(s: Switch, multipleMa = s.peerInfo.addrs) # Again, helper which stops everything by default

And because it's starting to get a bit weird, we would probably rename start/stop to something like listen/close, or similar

This has a few implications: The switch no longer has a single start / stop point, so it can't initialize stuff. Right now, all start does is start every address. But it could have, in the future, started the discovery process for instance. I think blocking this is good thing, because it forces us to keep the switch as slim as possible, instead of trying to stick everything in it, but open to discussion on that front.

We would need to rework the transport interface, to allow adding and removing addresses on the fly. I think that something we would have to do at some point anyway, so not too bad.

This new api would allow some nice stuff:

try: await switch.listen(someIPV6)
except SwitchError:
  try: await switch.listen(someIPV4)
  except SwitchError: quit(0) # noo

While preserving the current behavior (fail early) by default.

wdyt?

Menduist avatar Feb 02 '22 10:02 Menduist