I use the following API in my program to determine the free port and provide it to the application to run
portscanner.findAPortNotInUse(3000, 65000, '127.0.0.1', function(error, port) { console.log('AVAILABLE PORT AT: ' + port) })
https://github.com/baalexander/node-portscanner
This free port is provided for use by the application and works fine. The problem is that if I provide a free port for application A and the application is not already busy (sometimes it takes some time ...), another application B comes in and asks for a free port to give application A port B to APP B what is the cause of the problem ... is there an elegant way to solve it?
My application has no state, so it cannot save which application the port is connecting to ...
There is a solution that we can randomize the range, but this is not reliable ...
In my application Im getting the URL of the application, I have to provide a free port to run.
Update
I cannot use any broker or any other that will control this appearance. I need to find an algorithm (maybe with some smart random ones) that can help me do this internally, i.e. my program is similar to singleton, and I need some trick how to specify a port between 50000 to 65000 , which will reduce the number of port conflicts that was provided to applications
update 2
I decided to try something like the following, what do you think?
using lodash https://lodash.com/docs/4.17.2#random to define ports between loops that provide 3 (or more, if that makes sense) numbers for ranges like the following
portscanner.findAPortNotInUse([50001, 60000, 600010], '127.0.0.1', function(err, port) { if(err) { console.log("error!!!-> " +err); }else { console.log('Port Not in Use ' + port); }
Then, if I got a lie in the port, that is, all 3 ports are busy, run this process again for 3 other random offers .com numbers are welcome !!! I am trying to find a way to avoid a collision as much as possible ...