Skip to content

fix: try to use next available port #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions lib/detect-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,40 @@ module.exports = (port, callback) => {
callback = port;
port = null;
}
port = parseInt(port) || 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parseInt(port, 10)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let maxPort = port + 10;
if (maxPort > 65535) {
maxPort = 65535;
}
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not try another random port?

Copy link
Member Author

@fengmk2 fengmk2 Mar 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

random port won't error on normal case. If listen random port error happen, it's hard to try again, maybe "open file handles" is full on that time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is not covered

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's hard to mock this error, ignore it for now.

}

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
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
},
"devDependencies": {
"command-line-test": "^1.0.8",
"egg-bin": "^2.0.0",
"egg-bin": "^1.10.3",
"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