Skip to content

Enable keep-alive, --keep-alive-timeout flag added (default: 5000ms) #492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bin/configurable-http-proxy
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ cli
.option(
"--storage-backend <storage-class>",
"Define an external storage class. Defaults to in-MemoryStore."
)
.option(
"--keep-alive-timeout <timeout>",
"Set timeout (in milliseconds) for Keep-Alive connections",
parseInt
);

// collects multiple flags to an object
Expand Down Expand Up @@ -269,6 +274,7 @@ options.redirectTo = args.redirectTo;
options.headers = args.customHeader;
options.timeout = args.timeout;
options.proxyTimeout = args.proxyTimeout;
options.keepAliveTimeout = args.keepAliveTimeout;

// metrics options
options.enableMetrics = !!args.metricsPort;
Expand Down
23 changes: 20 additions & 3 deletions lib/configproxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,23 @@ class ConfigurableProxy extends EventEmitter {
this.metricsServer = http.createServer(metricsCallback);
}

// need separate agents for http and https requests
// these agents allow our _upstream_ sockets to be kept alive
this.httpAgent = http.globalAgent = new http.Agent({ keepAlive: true });
this.httpsAgent = https.globalAgent = new https.Agent({ keepAlive: true });

// these settings configure requests to the proxy itself to accept keep-alive
var httpOptions = {
keepAlive: true,
keepAliveTimeout: this.options.keepAliveTimeout || 5000,
};

// proxy requests separately
var proxyCallback = logErrors(this.handleProxyWeb);
if (this.options.ssl) {
this.proxyServer = https.createServer(this.options.ssl, proxyCallback);
this.proxyServer = https.createServer({ ...this.options.ssl, ...httpOptions }, proxyCallback);
} else {
this.proxyServer = http.createServer(proxyCallback);
this.proxyServer = http.createServer(httpOptions, proxyCallback);
}
// proxy websockets
this.proxyServer.on("upgrade", bound(this, this.handleProxyWs));
Expand Down Expand Up @@ -553,7 +564,13 @@ class ConfigurableProxy extends EventEmitter {
}

// add config argument
args.push({ target: target });
var proxyOptions = { target: target };
if (target.protocol.slice(-2) === "s:") {
proxyOptions.agent = that.httpsAgent;
} else {
proxyOptions.agent = that.httpAgent;
}
args.push(proxyOptions);

// add error handling
args.push(function (e) {
Expand Down
16 changes: 16 additions & 0 deletions test/proxy_spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// jshint jasmine: true
"use strict";

var http = require("http");
var path = require("path");
var util = require("../lib/testutil");
var request = require("request-promise-native");
Expand Down Expand Up @@ -86,6 +87,21 @@ describe("Proxy Tests", function () {
});
});

it("keep-alive proxy request", function (done) {
var agent = new http.Agent({ keepAlive: true });
r(proxyUrl, { agent: agent, resolveWithFullResponse: true }).then((res) => {
agent.destroy();
var body = JSON.parse(res.body);
expect(body).toEqual(
jasmine.objectContaining({
path: "/",
})
);
expect(res.headers["connection"]).toEqual("keep-alive");
done();
});
});

it("proxyRequest event can modify headers", function (done) {
var called = {};
proxy.on("proxyRequest", function (req, res) {
Expand Down