Skip to content

Instantly share code, notes, and snippets.

@owenallenaz
Created January 13, 2017 17:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save owenallenaz/fc3d8b24cdaa4ec04cc9dd0bf8ab485f to your computer and use it in GitHub Desktop.
Save owenallenaz/fc3d8b24cdaa4ec04cc9dd0bf8ab485f to your computer and use it in GitHub Desktop.
Showing stalled file reads because of `dns.lookup`
var dns = require("dns");
var http = require("http");
var fs = require("fs");
var net = require("net");
dns.setServers(["127.0.0.1"]);
var makeCallLookup = function(cb) {
dns.lookup("testSlow.com", cb);
}
var makeCallResolve = function(cb) {
dns.resolve("testSlow.com", cb);
}
fs.writeFileSync("/tmp/testFile.txt", "Here is my content");
setInterval(function() {
fs.readFile("/tmp/testFile.txt", function(err, data) {
if (err) { throw err; }
console.log("file read", new Date());
});
}, 100);
// pick a method to utilize
// var method = makeCallLookup;
var method = makeCallResolve;
setTimeout(function() {
for(var i = 0; i < 4; i++) {
(function() {
method(function(err) {
if (err) { throw err; }
console.log("call done", i);
});
})(i);
}
}, 1000);
var dns = require("dns");
var proxyDns = require("proxy-dns");
var server = new proxyDns.default({
ttl : 600
});
server.use(function* (next) {
console.log("got request", this.domain);
if (this.domain === "testSlow.com") {
// stall lookups to testSlow.com by 4s
console.log("got slow request");
yield new Promise((resolve, reject) => {
setTimeout(() => {
this.answers = ["127.0.0.1"];
resolve();
}, 4000);
});
yield next;
} else {
// resolve other calls using native behaviors
yield new Promise((resolve, reject) => {
dns.lookup(this.domain, function(err, ip) {
if (err) {
this.answers = [];
return resolve();
}
this.answers = [ip];
resolve();
});
});
yield next;
}
});
server.listen(53);
When using method === makeCallLookup: The file reads which run every 100ms stall entirely for 4 seconds from :22 to :26.
file read 2017-01-13T17:28:21.222Z
file read 2017-01-13T17:28:21.301Z
file read 2017-01-13T17:28:21.401Z
file read 2017-01-13T17:28:21.502Z
file read 2017-01-13T17:28:21.603Z
file read 2017-01-13T17:28:21.705Z
file read 2017-01-13T17:28:21.805Z
file read 2017-01-13T17:28:21.906Z
file read 2017-01-13T17:28:22.006Z
call done 4
call done 4
call done 4
call done 4
file read 2017-01-13T17:28:26.158Z
file read 2017-01-13T17:28:26.158Z
file read 2017-01-13T17:28:26.158Z
file read 2017-01-13T17:28:26.158Z
file read 2017-01-13T17:28:26.158Z
file read 2017-01-13T17:28:26.159Z
file read 2017-01-13T17:28:26.159Z
When using method === makeCallResolve: There is no delay to the fs calls at all.
file read 2017-01-13T17:36:44.093Z
file read 2017-01-13T17:36:44.173Z
file read 2017-01-13T17:36:44.274Z
file read 2017-01-13T17:36:44.375Z
file read 2017-01-13T17:36:44.477Z
file read 2017-01-13T17:36:44.578Z
file read 2017-01-13T17:36:44.679Z
file read 2017-01-13T17:36:44.779Z
file read 2017-01-13T17:36:44.879Z
file read 2017-01-13T17:36:44.980Z
file read 2017-01-13T17:36:45.081Z
file read 2017-01-13T17:36:45.182Z
file read 2017-01-13T17:36:45.283Z
file read 2017-01-13T17:36:45.385Z
file read 2017-01-13T17:36:45.485Z
file read 2017-01-13T17:36:45.586Z
file read 2017-01-13T17:36:45.686Z
file read 2017-01-13T17:36:45.786Z
file read 2017-01-13T17:36:45.887Z
file read 2017-01-13T17:36:45.987Z
file read 2017-01-13T17:36:46.087Z
file read 2017-01-13T17:36:46.188Z
file read 2017-01-13T17:36:46.289Z
file read 2017-01-13T17:36:46.390Z
file read 2017-01-13T17:36:46.491Z
file read 2017-01-13T17:36:46.592Z
file read 2017-01-13T17:36:46.693Z
file read 2017-01-13T17:36:46.794Z
file read 2017-01-13T17:36:46.895Z
file read 2017-01-13T17:36:46.996Z
file read 2017-01-13T17:36:47.097Z
file read 2017-01-13T17:36:47.198Z
file read 2017-01-13T17:36:47.299Z
file read 2017-01-13T17:36:47.400Z
file read 2017-01-13T17:36:47.501Z
file read 2017-01-13T17:36:47.602Z
file read 2017-01-13T17:36:47.702Z
file read 2017-01-13T17:36:47.802Z
file read 2017-01-13T17:36:47.903Z
file read 2017-01-13T17:36:48.004Z
file read 2017-01-13T17:36:48.105Z
file read 2017-01-13T17:36:48.206Z
file read 2017-01-13T17:36:48.308Z
file read 2017-01-13T17:36:48.408Z
file read 2017-01-13T17:36:48.509Z
file read 2017-01-13T17:36:48.610Z
file read 2017-01-13T17:36:48.711Z
file read 2017-01-13T17:36:48.812Z
file read 2017-01-13T17:36:48.914Z
call done 4
call done 4
call done 4
call done 4
file read 2017-01-13T17:36:49.015Z
file read 2017-01-13T17:36:49.115Z
file read 2017-01-13T17:36:49.216Z
file read 2017-01-13T17:36:49.318Z
file read 2017-01-13T17:36:49.418Z
file read 2017-01-13T17:36:49.518Z
file read 2017-01-13T17:36:49.619Z
file read 2017-01-13T17:36:49.720Z
file read 2017-01-13T17:36:49.821Z
file read 2017-01-13T17:36:49.922Z
file read 2017-01-13T17:36:50.023Z
file read 2017-01-13T17:36:50.124Z
file read 2017-01-13T17:36:50.225Z
file read 2017-01-13T17:36:50.326Z
file read 2017-01-13T17:36:50.426Z
file read 2017-01-13T17:36:50.526Z
file read 2017-01-13T17:36:50.627Z
file read 2017-01-13T17:36:50.728Z
file read 2017-01-13T17:36:50.829Z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment