Skip to content

Commit

Permalink
fix: try to use next available port
Browse files Browse the repository at this point in the history
closes #15
  • Loading branch information
fengmk2 committed Mar 17, 2017
1 parent de63f55 commit 796f675
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
23 changes: 17 additions & 6 deletions lib/detect-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,37 @@ module.exports = (port, callback) => {
callback = port;
port = null;
}
port = parseInt(port) || 0;
const maxPort = port + 10;
debug('detect free port between [%s, %s)', port, maxPort);
if (typeof callback === 'function') {
return tryListen(port, callback);
return tryListen(port, maxPort, callback);
}
// promise
return new Promise(resolve => {
tryListen(port, (_, realPort) => {
tryListen(port, maxPort, (_, realPort) => {
resolve(realPort);
});
});
};

function tryListen(port, callback) {
port = parseInt(port) || 0;
function tryListen(port, maxPort, callback) {
const server = new net.Server();

server.on('error', err => {
debug('listen %s error: %s', port, err);
port = 0;
if (port === 0) {
return callback(err);
}

port++;
if (port >= maxPort) {
debug('port: %s >= maxPort: %s, give up and use random port', port, maxPort);
port = 0;
maxPort = 0;
}
server.close();
return tryListen(port, callback);
return tryListen(port, maxPort, callback);
});

server.listen({ port }, () => {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"egg-bin": "^2.0.0",
"egg-ci": "^1.1.0",
"eslint": "^3.13.1",
"eslint-config-egg": "^3.1.0"
"eslint-config-egg": "^3.1.0",
"pedding": "^1.1.0"
},
"scripts": {
"test": "egg-bin test",
Expand Down
31 changes: 23 additions & 8 deletions test/detect-port.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@

const assert = require('assert');
const net = require('net');
const pedding = require('pedding');
const detectPort = require('..');

describe('detect port test', () => {
let server;
const servers = [];
before(done => {
server = new net.Server();
server.listen(7001, done);
done = pedding(11, done);
const server = new net.Server();
server.listen(3000, done);
servers.push(server);
for (let port = 7000; port < 7010; port++) {
const server = new net.Server();
server.listen(port, done);
servers.push(server);
}
});
after(() => {
server.close();
servers.forEach(server => server.close());
});

it('get random port', done => {
Expand All @@ -29,11 +37,18 @@ describe('detect port test', () => {
});
});

it('work with listening port', done => {
const port = 7001;
it('work with listening next port 3001', done => {
const port = 3000;
detectPort(port, (_, realPort) => {
assert(realPort !== 7001);
assert(realPort > 0);
assert(realPort === 3001);
done();
});
});

it('work with listening random port when try port hit maxPort', done => {
const port = 7000;
detectPort(port, (_, realPort) => {
assert(realPort < 7000 || realPort > 7009);
done();
});
});
Expand Down

0 comments on commit 796f675

Please sign in to comment.