Skip to content

Commit

Permalink
Allow it to disable connection keep-alive; mitigates #77
Browse files Browse the repository at this point in the history
  • Loading branch information
nonblocking committed May 6, 2021
1 parent a776f99 commit f004af5
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

## [unreleased]

* Http Proxy: Allow it to disable connection keep-alive; mitigates #77
* Prevented plugins with the same name to silently overwrite each other. If a plugin with the same name already exists
it will not be loaded anymore, and an error in the Admin UI will be shown.
* Made the *name* property required for all plugins and disallowed some characters like '/' and '?' to prevent problems
Expand Down
3 changes: 3 additions & 0 deletions packages/plugin-packages/mashroom-http-proxy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ You can override the default config in your Mashroom config file like this:
"rejectUnauthorized": true,
"poolMaxSockets": 10,
"socketTimeoutMs": 60000,
"keepAlive": true,
"proxyImpl": "default"
}
}
Expand All @@ -62,6 +63,8 @@ You can override the default config in your Mashroom config file like this:
* _rejectUnauthorized_: Reject self-signed certificates (Default: true)
* _poolMaxSockets_: Max pool size for connections (Default: 10)
* _socketTimeoutMs_: Socket timeout, 0 means no timeout (Default: 30000 - 30sec)
* _keepAlive_: Enable/disable connection keep-alive. Set this to *false* if you experience random ECONNRESET with the *nodeHttpProxy* implementation,
see: https://github.com/nonblocking/mashroom/issues/77
* _proxyImpl_: Switch the proxy implementation. Currently available are *request* (based on [request](https://github.com/request/request)),
*nodeHttpProxy* (based on [node-http-proxy](https://github.com/http-party/node-http-proxy)) and *default* (which is *request* at the moment)

Expand Down
1 change: 1 addition & 0 deletions packages/plugin-packages/mashroom-http-proxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"rejectUnauthorized": true,
"poolMaxSockets": 10,
"socketTimeoutMs": 30000,
"keepAlive": true,
"proxyImpl": "default"
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {Agent as HttpsAgent} from 'https';
import type {PoolConfig, PoolStats} from '../type-definitions/internal';

let _config: PoolConfig = {
keepAlive: true,
maxSockets: 10,
rejectUnauthorized: false,
};
Expand All @@ -25,7 +26,7 @@ export const getPoolConfig = (): PoolConfig => {
export const getHttpPool = () => {
if (!_httpPool) {
_httpPool = new http.Agent({
keepAlive: true,
keepAlive: _config.keepAlive,
maxSockets: _config.maxSockets,
});
}
Expand All @@ -35,7 +36,7 @@ export const getHttpPool = () => {
export const getHttpsPool = () => {
if (!_httpsPool) {
_httpsPool = new https.Agent({
keepAlive: true,
keepAlive: _config.keepAlive,
maxSockets: _config.maxSockets,
rejectUnauthorized: _config.rejectUnauthorized,
});
Expand All @@ -48,7 +49,6 @@ const getPoolStats = (agent: HttpAgent | HttpsAgent): PoolStats => {

return {
activeConnections: countArrayEntries(agent.sockets),
// @ts-ignore Not sure why freeSockets is not part of the type definition; see: https://nodejs.org/api/http.html#http_agent_freesockets
idleConnections: countArrayEntries(agent.freeSockets),
waitingRequests: countArrayEntries(agent.requests),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import type {MashroomServicesPluginBootstrapFunction} from '@mashroom/mashroom/t
import type {Proxy} from '../../type-definitions/internal';

const bootstrap: MashroomServicesPluginBootstrapFunction = async (pluginName, pluginConfig, pluginContextHolder) => {
const {proxyImpl, forwardMethods = [], forwardHeaders = [], rejectUnauthorized, poolMaxSockets, socketTimeoutMs} = pluginConfig;
const {proxyImpl, forwardMethods = [], forwardHeaders = [], rejectUnauthorized, poolMaxSockets, socketTimeoutMs, keepAlive} = pluginConfig;
const pluginContext = pluginContextHolder.getPluginContext();
const logger = pluginContext.loggerFactory('mashroom.httpProxy');

setPoolConfig({
keepAlive,
maxSockets: poolMaxSockets,
rejectUnauthorized,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface HttpHeaderFilter {
}

export type PoolConfig = {
keepAlive: boolean;
rejectUnauthorized: boolean;
maxSockets: number;
}
Expand Down

0 comments on commit f004af5

Please sign in to comment.