Skip to content

Instantly share code, notes, and snippets.

@owenallenaz
Created December 20, 2016 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save owenallenaz/b98ce2b9b491243e76dd517e0fdc46ca to your computer and use it in GitHub Desktop.
Save owenallenaz/b98ce2b9b491243e76dd517e0fdc46ca to your computer and use it in GitHub Desktop.
Faking a slow DNS server to show setTimeout not affected by dns resolution timing

node dnsLagDnsServer.js

node dnsLagServer.js

node dnsLag.js

--- dnsLag.js should timeout if the timeout was affected

var dns = require("dns");
var http = require("http");
dns.setServers(["127.0.0.1"]);
var req = http.request({
host : "www.foo.com", // routed through local so it doesn't matter
port : 8080
}, function(res) {
var data = "";
res.on("data", (chunk) => data += chunk);
res.on("end", () => {
console.log("request complete", data);
});
});
req.setTimeout(500); // this timeout should halt the request becuase DNS will take 1000ms, but it doesn't
req.on("error", (e) => {
console.log("error", e);
});
req.end();
var proxyDns = require("proxy-dns");
var server = new proxyDns.default({
ttl : 600
});
server.use(function* (next) {
console.log("got request", this.domain);
yield new Promise((resolve, reject) => {
setTimeout(() => {
this.answers = ["127.0.0.1"];
resolve();
}, 1000);
});
yield next;
});
server.listen(53);
var http = require("http");
var server = http.createServer((req, res) => {
console.log("connection received");
setTimeout(function() {
res.end("content");
}, 100);
});
server.listen(8080, () => {
console.log("server booted");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment