-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
134 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict'; | ||
const dgram = require('dgram'); | ||
|
||
const options = { type: 'udp4', reuseAddr: true }; | ||
|
||
function checkSupportReusePort() { | ||
return new Promise((resolve, reject) => { | ||
const socket = dgram.createSocket(options); | ||
socket.bind(0); | ||
socket.on('listening', () => { | ||
socket.close(); | ||
resolve(); | ||
}); | ||
socket.on('error', (err) => { | ||
console.log('don not support reusePort flag', err); | ||
socket.close(); | ||
reject(); | ||
}); | ||
}); | ||
} | ||
|
||
module.exports = { | ||
checkSupportReusePort, | ||
options, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const { checkSupportReusePort, options } = require('../common/udp'); | ||
const assert = require('assert'); | ||
const child_process = require('child_process'); | ||
const dgram = require('dgram'); | ||
|
||
if (!process.env.isWorker) { | ||
checkSupportReusePort().then(() => { | ||
const socket = dgram.createSocket(options); | ||
socket.bind(0, common.mustCall(() => { | ||
const port = socket.address().port; | ||
const workerOptions = { env: { 'isWorker': 1, port } }; | ||
let count = 2; | ||
for (let i = 0; i < 2; i++) { | ||
const worker = child_process.fork(__filename, workerOptions); | ||
worker.on('exit', common.mustCall((code) => { | ||
assert.strictEqual(code, 0); | ||
if (--count === 0) { | ||
socket.close(); | ||
} | ||
})); | ||
} | ||
})); | ||
}, process.exit); | ||
return; | ||
} | ||
|
||
const socket = dgram.createSocket(options); | ||
|
||
socket.bind(+process.env.port, common.mustCall(() => { | ||
socket.close(common.mustCall(() => { | ||
process.exit(0); | ||
})); | ||
})).on('error', common.mustNotCall()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
if (common.isWindows) | ||
common.skip('dgram clustering is currently not supported on windows.'); | ||
|
||
const { checkSupportReusePort, options } = require('../common/udp'); | ||
const assert = require('assert'); | ||
const cluster = require('cluster'); | ||
const dgram = require('dgram'); | ||
|
||
if (cluster.isPrimary) { | ||
checkSupportReusePort().then(() => { | ||
cluster.fork().on('exit', common.mustCall((code) => { | ||
assert.strictEqual(code, 0); | ||
})); | ||
}, process.exit); | ||
return; | ||
} | ||
|
||
let waiting = 2; | ||
function close() { | ||
if (--waiting === 0) | ||
cluster.worker.disconnect(); | ||
} | ||
|
||
const socket1 = dgram.createSocket(options); | ||
const socket2 = dgram.createSocket(options); | ||
|
||
socket1.bind(0, () => { | ||
socket2.bind(socket1.address().port, () => { | ||
socket1.close(close); | ||
socket2.close(close); | ||
}).on('error', common.mustNotCall()); | ||
}).on('error', common.mustNotCall()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const { checkSupportReusePort, options } = require('../common/udp'); | ||
const dgram = require('dgram'); | ||
|
||
function test() { | ||
const socket1 = dgram.createSocket(options); | ||
const socket2 = dgram.createSocket(options); | ||
socket1.bind(0, common.mustCall(() => { | ||
socket2.bind(socket1.address().port, common.mustCall(() => { | ||
socket1.close(); | ||
socket2.close(); | ||
})); | ||
})); | ||
socket1.on('error', common.mustNotCall()); | ||
socket2.on('error', common.mustNotCall()); | ||
} | ||
|
||
checkSupportReusePort().then(test, process.exit); |