Skip to content

Instantly share code, notes, and snippets.

@igor-kupczynski
Last active August 29, 2015 14:13
Show Gist options
  • Save igor-kupczynski/e900cfdeeac5019ecdea to your computer and use it in GitHub Desktop.
Save igor-kupczynski/e900cfdeeac5019ecdea to your computer and use it in GitHub Desktop.
Latency proxy
$ time curl -XGET localhost:8008
{
"status" : 200,
"name" : "X-Ray",
"version" : {
"number" : "1.3.5",
"build_hash" : "4a50e7df768fddd572f48830ae9c35e4ded86ac1",
"build_timestamp" : "2014-11-05T15:21:28Z",
"build_snapshot" : false,
"lucene_version" : "4.9"
},
"tagline" : "You Know, for Search"
}
real 0m10.048s
user 0m0.004s
sys 0m0.004s
{
"name": "latency-proxy",
"version": "0.0.1",
"main": "proxy.js",
"author": "Igor Kupczyński <igor@kupczynski.info>",
"license": "Apache 2.0",
"dependencies": {
"http-proxy": "^1.8.1",
"optimist": "^0.6.1"
}
}
'use strict';
// Opts parsing
const argv = require('optimist')
.demand(['server','latency'])
.usage('Usage: $0 --server [address] --latency [num]')
.describe('server', 'Server to proxy to, e.g. http://localhost:9200')
.describe('latency', 'How much time will each request be on hold before proxing to the server (in seconds)')
.describe('proxy_port', 'Port that the proxy will listen on').default('proxy_port', 8008)
.argv,
proxy_host = 'localhost',
proxy_port = argv.proxy_port,
proxy_to = argv.server,
latency = argv.latency;
// Modules
const http = require('http'),
httpProxy = require('http-proxy'),
sec = 1000;
// Proxy definition
const proxy = httpProxy.createProxyServer({}),
proxy_func = function(req, res, target) {
return function() {
proxy.web(req, res, {target: target});
};
};
// Listen to requests
http.createServer(function (req, res) {
// Run the proxy logic on each request after a timeoue
setTimeout(proxy_func(req, res, proxy_to), latency * sec);
}).listen(proxy_port, function(e) {
console.log("Listening at 'localhost:" + proxy_port + "', proxying to '" + proxy_to +
"', each request is on hold for " + latency + "s");
});
$ node --harmony proxy.js --server http://localhost:9200 --latency 10
Listening at 'localhost:8008', proxying to 'http://localhost:9200', each request is on hold for 10s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment