Skip to content
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

Make the promise.cancel() method actually work #42

Merged
merged 4 commits into from
Mar 30, 2020
Merged

Make the promise.cancel() method actually work #42

merged 4 commits into from
Mar 30, 2020

Conversation

LuKks
Copy link

@LuKks LuKks commented Mar 23, 2020

This is needed for sindresorhus/is-online#47

@LuKks
Copy link
Author

LuKks commented Mar 23, 2020

Here is more info about the changes: sindresorhus/is-online#47 (comment)

index.js Outdated Show resolved Hide resolved
@@ -83,16 +83,22 @@ const queryDns = (version, options) => {

const socket = dns({
retries: 0,
maxQueries: 1,
Copy link
Owner

Choose a reason for hiding this comment

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

Another problem is the default maxQueries (10000) of dns-socket which is overkill because we just do one query at the same time

If we only do one query at the time, how does setting this change anything?

Copy link
Author

Choose a reason for hiding this comment

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

dns-socket internally creates an empty array with 10000 empty values, later it may or not loop each entry searching for just one query and anyway is a waste of memory.
Check this line: https://github.com/mafintosh/dns-socket/blob/master/index.js#L31

@@ -125,7 +131,7 @@ const queryDns = (version, options) => {
})();

promise.cancel = () => {
socket.cancel();
socket.destroy();
Copy link
Owner

Choose a reason for hiding this comment

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

What's the difference between .cancel() and .destroy()?

Copy link
Author

Choose a reason for hiding this comment

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

Well, socket.cancel() without argument does nothing because it needs the query id: https://github.com/mafintosh/dns-socket/blob/master/index.js#L217
In that case, doing "promise.cancel()" was basically no-op.

Now we are doing socket.destroy() and checking for socket.destroyed so we stop unnecessary queries.

@sindresorhus sindresorhus changed the title Query leak Make the promise.cancel() method actually work Mar 30, 2020
@sindresorhus sindresorhus merged commit c4303d5 into sindresorhus:master Mar 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants