Skip to content Skip to sidebar Skip to footer

Find Free Port Not In Use For Apps - Find Some Algorithm

I use the following API in my program to detrmine free port and provide it to application to run portscanner.findAPortNotInUse(3000, 65000, '127.0.0.1', function(error, port) { c

Solution 1:

I would simply accept the fact that things can go wrong in a distributed system and retry the operation (i.e., getting a free port) if it failed for whatever reason on the first attempt.

Luckily, there are lots of npm modules out there that do that already for you, e.g. retry.

Using this module you can retry an asynchronous operation until it succeeds, and configure waiting strategies, and how many times it should be retried maximally, and so on…

To provide a code example, it basically comes down to something such as:

const operation = retry.operation();

operation.attempt(currentAttempt => {
  findAnUnusedPortAndUseIt(err => {
    if (operation.retry(err)) {
      return;
    }

    callback(err ? operation.mainError() : null);
  });
});

The benefits of this solution are:

  • Works without locking, i.e. it is efficient and makes low usage of resources if everything is fine.
  • Works without a central broker or something like that.
  • Works for distributed systems of any size.
  • Uses a pattern that you can re-use in distributed systems for all kinds of problems.
  • Uses a battle-tested and solid npm module instead of handwriting all these things.
  • Does not require you to change your code in a major way, instead it is just adding a few lines.

Hope this helps :-)

Solution 2:

If your applications can open ports with option like SO_REUSEADDR, but operation system keeps ports in the list in TIME_WAIT state, you can bind/open port you want to return with SO_REUSEADDR, instantly close it and give it back to application. So for TIME_WAIT period (depending on operation system it can be 30 seconds, and actual time should be decided/set up or found by experiment/administration) port list will show this port as occupied.

If your port finder does not give port numbers for ports in TIME_WAIT state, problem solved by relatively expensive open/close socket operation.

Solution 3:

I'd advise you look for a way to retain state. Even temporary state, in memory, is better than nothing at all. This way you could at least avoid giving out ports you've already given out. Because those are very likely not free anymore. (This would be as simple as saving them and regenerating a random port if you notice you found a random port you've already given out). If you don't want collisions, build your module to have state so it can avoid them. If you don't want to do that, you'll have to accept there are going to be collisions sometimes when there don't need to be.

If the URLs you get are random, the best you can do is guess randomly. If you can derive some property in which the URLs uniquely and consistently differ, you could design something around that.

Code example:

functiongetUnusedPort(url) {
  // range is [0, 65001). (inclusive zero, exclusive 65001)constguessPort = () => Math.floor(Math.random() * 15001) + 50000;
  let randomPort = guessPort();
  while (checkPortInUse(randomPort)) {
    randomPort = guessPort();
  }
  return randomPort;
}

Notes:

  • checkPortInUse will probably be asynchronous so you'll have to accommodate for that.
  • You said 'between 50000 and 65000'. This is from 50000 up to and including 65000.

Solution 4:

When managing multiple applications or multiple servers, where one must be right the first time (without retrying), you need a single source of truth. Applications on the same machine can talk to a database, a broker server or even a file, so long as the resource is "lockable". (Servers work in similar ways, though not with local files).

So your flow would be something like:

  1. App A sends request to service to request lock.
  2. When lock is confirmed, start port scanner
  3. When port is used, release lock.

Again, this could be a "PortService" you write that hands out unused ports, or a simple lock in some shared resource so two things are getting the same port at the same time.

Hopefully you can find something suitable to work for your apps.

Solution 5:

have you tried something like this ?

var portscanner = require('portscanner')

module.exports = function (ip, minport) {

  var pendings = [];
  var allocated = [];
  var pendingsearch = false;

  functionstart(){
    if (!pendingsearch && pendings.length) {
      pendingsearch = true;
      var ready = pendings.shift();
      var min = getMax();
      min = min===-1?minport:min+1;
      var max = min + 100;
      portscanner.findAPortNotInUse(min, max, ip, function(err, port) {
        if (err) {
          pendings.unshift(ready);
        } elseif (allocated.indexOf(port)>-1) {
          pendings.unshift(ready);
        } else {
          allocated.push(port);
          ready(port);
        }
        restart();
      })
    }
  }
  functionrestart(){
    pendingsearch = false;
    setTimeout(start, 0);
  }
  functiongetMax(){
    var max = -1;
    allocated.forEach(function(p) {
      if (p>max) {
        max = p
      }
    })
    return max
  }

  functionfindPort(ready) {
    pendings.push(ready);
    start();
  };

  functionupdateAllocated() {
    var tocheck = [].concat(allocated);
    var todo = tocheck.length;
    tocheck.forEach(function(port, index) {
      portscanner.checkPortStatus(port, ip, function(error, status) {
        if (status==='closed') {
          allocated.splice(allocated.indexOf(port), 1);
        }
        if (index===tocheck.length-1) {
          setTimeout(updateAllocated, 1000);
        }
      })
    });
    if (tocheck.length<1) {
      setTimeout(updateAllocated, 1000);
    }
  };
  var update = setTimeout(updateAllocated, 1000);

  return {
    findPort: findPort,
    stop: function(){
      clearTimeout(update);
    },
  }
};

Post a Comment for "Find Free Port Not In Use For Apps - Find Some Algorithm"