Skip to content

Instantly share code, notes, and snippets.

@Jeff-Lewis
Forked from jchip/cls-http-agent-break.js
Created July 7, 2016 19:12
Show Gist options
  • Save Jeff-Lewis/c81c22164f390ffd5a75ecfe20757e87 to your computer and use it in GitHub Desktop.
Save Jeff-Lewis/c81c22164f390ffd5a75ecfe20757e87 to your computer and use it in GitHub Desktop.
"use strict";
var clsModule = require("continuation-local-storage");
const superagent = require("superagent");
const assert = require("assert");
var http = require("http");
var keepAlive = process.env.KEEP_ALIVE !== "0";
var httpAgent = new http.Agent({
keepAlive: keepAlive,
maxSockets: 1,
keepAliveMsecs: 30000
});
var namespace;
function httpGetRequest(cb) {
var r = superagent["get"]("http://www.google.com");
if (keepAlive) {
console.log("Keep alive ENABLED, setting http agent");
r.agent(httpAgent);
}
r.end(function (err, res) {
if (err) {
cb(err);
} else {
console.log("http get status", res.status);
cb(null, {status: res.status, statusText: res.text, obj: res.body});
}
});
}
function clsAction(action, cb) {
namespace.run(function () {
var xid = Math.floor(Math.random() * 1000);
namespace.set("xid", xid);
console.log("before calling nestedContext: xid value", namespace.get("xid"));
action(function (e) {
console.log("returned from action xid value", namespace.get("xid"), "expected", xid);
assert.equal(namespace.get("xid"), xid);
cb(e);
})
});
}
function test() {
namespace = clsModule.createNamespace("test");
clsAction(httpGetRequest, function () {
clsAction(httpGetRequest, function () {
console.log("test done");
});
});
}
test();
"use strict";
var clsModule = require("continuation-local-storage");
const assert = require("assert");
const util = require("util");
var http = require("http");
var fetch = require("node-fetch");
var keepAlive = process.env.KEEP_ALIVE !== "0";
var httpAgent = new http.Agent({
keepAlive: keepAlive,
maxSockets: 1,
keepAliveMsecs: 30000
});
var namespace;
function httpGetRequestFetch(cb) {
var options = {};
console.log("HTTP Get with fetch");
if (keepAlive) {
console.log("Keep alive ENABLED, setting http agent");
options.agent = httpAgent;
}
fetch("http://www.google.com", options)
.then((res) => {
console.log("http get status", res.status);
cb(null);
})
.catch((err) => {
console.log(err);
});
}
function clsAction(action, cb) {
namespace.run(function () {
var xid = Math.floor(Math.random() * 1000);
namespace.set("xid", xid);
console.log("before calling nestedContext: xid value", namespace.get("xid"));
action(function (e) {
console.log("returned from action xid value", namespace.get("xid"), "expected", xid);
assert.equal(namespace.get("xid"), xid);
cb(e);
})
});
}
function test() {
namespace = clsModule.createNamespace("test");
clsAction(httpGetRequestFetch, function () {
clsAction(httpGetRequestFetch, function () {
console.log("test done");
});
});
}
test();
"use strict";
var clsModule = require("continuation-local-storage");
const assert = require("assert");
const util = require("util");
var http = require("http");
var Wreck = require("wreck");
var keepAlive = process.env.KEEP_ALIVE !== "0";
var httpAgent = new http.Agent({
keepAlive: keepAlive,
maxSockets: 1,
keepAliveMsecs: 30000
});
var namespace;
function httpGetRequestWreck(cb) {
var options = {};
console.log("HTTP Get with wreck");
if (keepAlive) {
console.log("Keep alive ENABLED, setting http agent");
options.agent = httpAgent;
}
Wreck.get("http://www.google.com", options, function (err, res) {
console.log("http get status", res.statusCode);
cb(err);
});
}
function clsAction(action, cb) {
namespace.run(function () {
var xid = Math.floor(Math.random() * 1000);
namespace.set("xid", xid);
console.log("before calling nestedContext: xid value", namespace.get("xid"));
action(function (e) {
console.log("returned from action xid value", namespace.get("xid"), "expected", xid);
assert.equal(namespace.get("xid"), xid);
cb(e);
})
});
}
function test() {
namespace = clsModule.createNamespace("test");
clsAction(httpGetRequestWreck, function () {
clsAction(httpGetRequestWreck, function () {
console.log("test done");
});
});
}
test();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment