Skip to content
This repository has been archived by the owner on Jan 7, 2022. It is now read-only.

Bind random port if all alternative ports fail #39

Merged
merged 2 commits into from
Sep 26, 2019

Conversation

sammacbeth
Copy link
Collaborator

If binding of all ports in the _portAlts list fails, we get stuck in an infinite loop trying to bind the first port number. This change updates this._port on every retry so at the end of the list we listen with undefined which will give us a random port.

Code to reproduce the original issue:

const Discovery = require('hyperdiscovery');
const swarm = Discovery();

Then run: DEBUG=hyperdiscovery node index.js

Observed output:

 hyperdiscovery Port 3282 in use. Trying 3000.
 hyperdiscovery Port 3282 in use. Trying 3002.
 hyperdiscovery Port 3282 in use. Trying 3004.
 hyperdiscovery Port 3282 in use. Trying 2001.
 hyperdiscovery Port 3282 in use. Trying 2003.
 hyperdiscovery Port 3282 in use. Trying 2005.
 hyperdiscovery Port 3282 in use. Trying undefined.
 hyperdiscovery Port 3282 in use. Trying undefined.
...

After this change:

  hyperdiscovery Port 3282 in use. Trying 3000. +0ms
  hyperdiscovery Port 3000 in use. Trying 3002. +4ms
  hyperdiscovery Port 3002 in use. Trying 3004. +1ms
  hyperdiscovery Port 3004 in use. Trying 2001. +3ms
  hyperdiscovery Port 2001 in use. Trying 2003. +0ms
  hyperdiscovery Port 2003 in use. Trying 2005. +0ms
  hyperdiscovery Port 2005 in use. Trying undefined. +1ms
  hyperdiscovery swarm:listening { port: 62989 } +9ms

@okdistribute
Copy link
Collaborator

oh nice one, thanks for that @sammacbeth. I can add you to the npm & github org as a contributor

@okdistribute okdistribute merged commit 6bfea5e into dat-ecosystem-archive:master Sep 26, 2019
@sammacbeth
Copy link
Collaborator Author

Thanks for the merge! Would it be possible to release a new version to npm with this fix so I can pull it into my project?

@RangerMauve
Copy link
Contributor

On it!

@RangerMauve
Copy link
Contributor

Out in 10.1.0 😁

# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants