Skip to content

Commit 77f8289

Browse files
TylerBrockflovilmart
authored andcommitted
Use http agents for hook requests (parse-community#4791)
1 parent b049d9c commit 77f8289

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

src/Controllers/HooksController.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,26 @@ import * as Parse from "parse/node";
66
// @flow-disable-next
77
import * as request from "request";
88
import { logger } from '../logger';
9+
import http from 'http';
10+
import https from 'https';
911

1012
const DefaultHooksCollectionName = "_Hooks";
13+
const HTTPAgents = {
14+
http: new http.Agent({ keepAlive: true }),
15+
https: new https.Agent({ keepAlive: true }),
16+
}
1117

1218
export class HooksController {
1319
_applicationId:string;
1420
_webhookKey:string;
1521
database: any;
22+
keepAlive: boolean;
1623

17-
constructor(applicationId:string, databaseController, webhookKey) {
24+
constructor(applicationId:string, databaseController, webhookKey, keepAlive) {
1825
this._applicationId = applicationId;
1926
this._webhookKey = webhookKey;
2027
this.database = databaseController;
28+
this.keepAlive = keepAlive;
2129
}
2230

2331
load() {
@@ -85,7 +93,7 @@ export class HooksController {
8593
}
8694

8795
addHookToTriggers(hook) {
88-
var wrappedFunction = wrapToHTTPRequest(hook, this._webhookKey);
96+
var wrappedFunction = wrapToHTTPRequest(hook, this._webhookKey, this.keepAlive);
8997
wrappedFunction.url = hook.url;
9098
if (hook.className) {
9199
triggers.addTrigger(hook.triggerName, hook.className, wrappedFunction, this._applicationId)
@@ -159,7 +167,7 @@ export class HooksController {
159167
}
160168
}
161169

162-
function wrapToHTTPRequest(hook, key) {
170+
function wrapToHTTPRequest(hook, key, keepAlive) {
163171
return (req, res) => {
164172
const jsonBody = {};
165173
for (var i in req) {
@@ -177,9 +185,14 @@ function wrapToHTTPRequest(hook, key) {
177185
headers: {
178186
'Content-Type': 'application/json'
179187
},
180-
body: JSON.stringify(jsonBody)
188+
body: JSON.stringify(jsonBody),
181189
};
182190

191+
if (keepAlive) {
192+
const agent = hook.url.startsWith('https') ? HTTPAgents['https'] : HTTPAgents['http'];
193+
jsonRequest.agent = agent;
194+
}
195+
183196
if (key) {
184197
jsonRequest.headers['X-Parse-Webhook-Key'] = key;
185198
} else {

src/Controllers/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,10 @@ export function getDatabaseController(options: ParseServerOptions, cacheControll
149149
export function getHooksController(options: ParseServerOptions, databaseController: DatabaseController): HooksController {
150150
const {
151151
appId,
152-
webhookKey
152+
webhookKey,
153+
hookKeepAlive,
153154
} = options;
154-
return new HooksController(appId, databaseController, webhookKey);
155+
return new HooksController(appId, databaseController, webhookKey, hookKeepAlive);
155156
}
156157

157158
interface PushControlling {
@@ -228,4 +229,3 @@ export function getDatabaseAdapter(databaseURI, collectionPrefix, databaseOption
228229
});
229230
}
230231
}
231-

src/Options/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ export interface ParseServerOptions {
131131
startLiveQueryServer: ?boolean;
132132
/* Live query server configuration options (will start the liveQuery server) */
133133
liveQueryServerOptions: ?LiveQueryServerOptions;
134+
/* Keep hook HTTP connections alive */
135+
hookKeepAlive: ?boolean;
134136

135137
__indexBuildCompletionCallbackForTests: ?()=>void;
136138
}

0 commit comments

Comments
 (0)