Skip to content
Navigation Menu
{{ message }}
-
-
Notifications
You must be signed in to change notification settings - Fork 35.8k
dgram: skip dns.lookup() for literal IP addresses #64133
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
Open
BridgeAR
wants to merge
1
commit into
nodejs:main
Choose a base branch
from
BridgeAR:BridgeAR/2026-06-25-dgram-skip-ip-default
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,43 @@ | ||
| // Measure the send rate to a literal IP destination. The destination needs no | ||
| // name resolution, so this isolates the per-send overhead the default lookup | ||
| // pays before the packet reaches the socket. | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common.js'); | ||
| const dgram = require('dgram'); | ||
| const PORT = common.PORT; | ||
|
|
||
| // `n` is the number of send requests queued each round. Keep it high (>10) so | ||
| // the measurement reflects send overhead rather than event loop cycles. | ||
| const bench = common.createBenchmark(main, { | ||
| n: [100], | ||
| dur: [5], | ||
| }); | ||
|
|
||
| function main({ dur, n }) { | ||
| const chunk = Buffer.allocUnsafe(1); | ||
| let sent = 0; | ||
| const socket = dgram.createSocket('udp4'); | ||
|
|
||
| function onsend() { | ||
| if (sent++ % n === 0) { | ||
| setImmediate(() => { | ||
| for (let i = 0; i < n; i++) { | ||
| socket.send(chunk, PORT, '127.0.0.1', onsend); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| socket.on('listening', () => { | ||
| bench.start(); | ||
| onsend(); | ||
|
|
||
| setTimeout(() => { | ||
| bench.end(sent); | ||
| process.exit(0); | ||
| }, dur * 1000); | ||
| }); | ||
|
|
||
| socket.bind(PORT); | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,80 @@ | ||
| 'use strict'; | ||
|
|
||
| // The default dgram lookup resolves a literal IP address of the socket's own | ||
| // family to itself, without calling dns.lookup(). Each case below stubs the | ||
| // process-global dns.lookup(), so they run sequentially to keep one case from | ||
| // observing another's stub. | ||
|
|
||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const dgram = require('dgram'); | ||
| const dns = require('dns'); | ||
|
|
||
| const originalLookup = dns.lookup; | ||
|
|
||
| function ipv4SendSkipsLookup(next) { | ||
| dns.lookup = common.mustNotCall('dns.lookup() ran for an IPv4 literal'); | ||
|
|
||
| const receiver = dgram.createSocket('udp4'); | ||
| const sender = dgram.createSocket('udp4'); | ||
|
|
||
| receiver.on('message', common.mustCall((msg) => { | ||
| assert.strictEqual(msg.toString(), 'payload'); | ||
| dns.lookup = originalLookup; | ||
| receiver.close(); | ||
| sender.close(); | ||
| next(); | ||
| })); | ||
|
|
||
| receiver.bind(0, '127.0.0.1', common.mustCall(() => { | ||
| sender.send('payload', receiver.address().port, '127.0.0.1', common.mustCall()); | ||
| })); | ||
| } | ||
|
|
||
| function ipv6BindSkipsLookup(next) { | ||
| if (!common.hasIPv6) { | ||
| next(); | ||
| return; | ||
| } | ||
|
|
||
| dns.lookup = common.mustNotCall('dns.lookup() ran for an IPv6 literal'); | ||
|
|
||
| const socket = dgram.createSocket('udp6'); | ||
|
|
||
| socket.bind(0, '::1', common.mustCall(() => { | ||
| dns.lookup = originalLookup; | ||
| socket.close(); | ||
| next(); | ||
| })); | ||
| } | ||
|
|
||
| function mismatchedFamilyFallsThrough(next) { | ||
| // '::1' is not an IPv4 literal, so a udp4 socket still resolves it via | ||
| // dns.lookup() rather than short-circuiting. | ||
| dns.lookup = common.mustCall((host, family, callback) => { | ||
| dns.lookup = originalLookup; | ||
| assert.strictEqual(host, '::1'); | ||
| assert.strictEqual(family, 4); | ||
| callback(null, '127.0.0.1', 4); | ||
| }); | ||
|
|
||
| const socket = dgram.createSocket('udp4'); | ||
|
|
||
| socket.bind(0, '::1', common.mustCall(() => { | ||
| socket.close(); | ||
| next(); | ||
| })); | ||
| } | ||
|
|
||
| const cases = [ | ||
| ipv4SendSkipsLookup, | ||
| ipv6BindSkipsLookup, | ||
| mismatchedFamilyFallsThrough, | ||
| ]; | ||
|
|
||
| (function runNext() { | ||
| const testCase = cases.shift(); | ||
| if (testCase !== undefined) { | ||
| testCase(runNext); | ||
| } | ||
| })(); |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
You can’t perform that action at this time.

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why only when the default is used? Is it because the previous behaviour would always call custom function, and not doing so would be breaking?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a stacked PR that does it in general, I was just concerned about it being major if we do it for custom lookup methods, so I decided to split it. I am fine to have it as one PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's leave it as is to keep it non-major for this PR 👍 .