Skip to content

Instantly share code, notes, and snippets.

@jchip
Last active September 11, 2019 15:46
Show Gist options
  • Save jchip/73c44046408d0e60b96abc3fc0303eb2 to your computer and use it in GitHub Desktop.
Save jchip/73c44046408d0e60b96abc3fc0303eb2 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");
var firstDone = false;
clsAction(httpGetRequest, function () {
firstDone = true;
});
function secondFetch() {
if (firstDone) {
clsAction(httpGetRequest, function () {
console.log("test done");
});
} else {
setTimeout( secondFetch, 50 );
}
}
secondFetch();
}
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");
var firstDone = false;
clsAction(httpGetRequestFetch, function () {
firstDone = true;
});
function secondFetch() {
if (firstDone) {
clsAction(httpGetRequestFetch, function () {
console.log("test done");
});
} else {
setTimeout( secondFetch, 50 );
}
}
secondFetch();
}
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");
var firstDone = false;
clsAction(httpGetRequestWreck, function () {
firstDone = true;
});
function secondFetch() {
if (firstDone) {
clsAction(httpGetRequestWreck, function () {
console.log("test done");
});
} else {
setTimeout( secondFetch, 50 );
}
}
secondFetch();
}
test();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment