Use http agents for hook requests (#4791)
This commit is contained in:
committed by
Florent Vilmart
parent
32d8c36ff5
commit
82fb06f9c9
@@ -6,18 +6,26 @@ import * as Parse from "parse/node";
|
|||||||
// @flow-disable-next
|
// @flow-disable-next
|
||||||
import * as request from "request";
|
import * as request from "request";
|
||||||
import { logger } from '../logger';
|
import { logger } from '../logger';
|
||||||
|
import http from 'http';
|
||||||
|
import https from 'https';
|
||||||
|
|
||||||
const DefaultHooksCollectionName = "_Hooks";
|
const DefaultHooksCollectionName = "_Hooks";
|
||||||
|
const HTTPAgents = {
|
||||||
|
http: new http.Agent({ keepAlive: true }),
|
||||||
|
https: new https.Agent({ keepAlive: true }),
|
||||||
|
}
|
||||||
|
|
||||||
export class HooksController {
|
export class HooksController {
|
||||||
_applicationId:string;
|
_applicationId:string;
|
||||||
_webhookKey:string;
|
_webhookKey:string;
|
||||||
database: any;
|
database: any;
|
||||||
|
keepAlive: boolean;
|
||||||
|
|
||||||
constructor(applicationId:string, databaseController, webhookKey) {
|
constructor(applicationId:string, databaseController, webhookKey, keepAlive) {
|
||||||
this._applicationId = applicationId;
|
this._applicationId = applicationId;
|
||||||
this._webhookKey = webhookKey;
|
this._webhookKey = webhookKey;
|
||||||
this.database = databaseController;
|
this.database = databaseController;
|
||||||
|
this.keepAlive = keepAlive;
|
||||||
}
|
}
|
||||||
|
|
||||||
load() {
|
load() {
|
||||||
@@ -85,7 +93,7 @@ export class HooksController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
addHookToTriggers(hook) {
|
addHookToTriggers(hook) {
|
||||||
var wrappedFunction = wrapToHTTPRequest(hook, this._webhookKey);
|
var wrappedFunction = wrapToHTTPRequest(hook, this._webhookKey, this.keepAlive);
|
||||||
wrappedFunction.url = hook.url;
|
wrappedFunction.url = hook.url;
|
||||||
if (hook.className) {
|
if (hook.className) {
|
||||||
triggers.addTrigger(hook.triggerName, hook.className, wrappedFunction, this._applicationId)
|
triggers.addTrigger(hook.triggerName, hook.className, wrappedFunction, this._applicationId)
|
||||||
@@ -159,7 +167,7 @@ export class HooksController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function wrapToHTTPRequest(hook, key) {
|
function wrapToHTTPRequest(hook, key, keepAlive) {
|
||||||
return (req, res) => {
|
return (req, res) => {
|
||||||
const jsonBody = {};
|
const jsonBody = {};
|
||||||
for (var i in req) {
|
for (var i in req) {
|
||||||
@@ -177,9 +185,14 @@ function wrapToHTTPRequest(hook, key) {
|
|||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
body: JSON.stringify(jsonBody)
|
body: JSON.stringify(jsonBody),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (keepAlive) {
|
||||||
|
const agent = hook.url.startsWith('https') ? HTTPAgents['https'] : HTTPAgents['http'];
|
||||||
|
jsonRequest.agent = agent;
|
||||||
|
}
|
||||||
|
|
||||||
if (key) {
|
if (key) {
|
||||||
jsonRequest.headers['X-Parse-Webhook-Key'] = key;
|
jsonRequest.headers['X-Parse-Webhook-Key'] = key;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -149,9 +149,10 @@ export function getDatabaseController(options: ParseServerOptions, cacheControll
|
|||||||
export function getHooksController(options: ParseServerOptions, databaseController: DatabaseController): HooksController {
|
export function getHooksController(options: ParseServerOptions, databaseController: DatabaseController): HooksController {
|
||||||
const {
|
const {
|
||||||
appId,
|
appId,
|
||||||
webhookKey
|
webhookKey,
|
||||||
|
hookKeepAlive,
|
||||||
} = options;
|
} = options;
|
||||||
return new HooksController(appId, databaseController, webhookKey);
|
return new HooksController(appId, databaseController, webhookKey, hookKeepAlive);
|
||||||
}
|
}
|
||||||
|
|
||||||
interface PushControlling {
|
interface PushControlling {
|
||||||
@@ -228,4 +229,3 @@ export function getDatabaseAdapter(databaseURI, collectionPrefix, databaseOption
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -131,6 +131,8 @@ export interface ParseServerOptions {
|
|||||||
startLiveQueryServer: ?boolean;
|
startLiveQueryServer: ?boolean;
|
||||||
/* Live query server configuration options (will start the liveQuery server) */
|
/* Live query server configuration options (will start the liveQuery server) */
|
||||||
liveQueryServerOptions: ?LiveQueryServerOptions;
|
liveQueryServerOptions: ?LiveQueryServerOptions;
|
||||||
|
/* Keep hook HTTP connections alive */
|
||||||
|
hookKeepAlive: ?boolean;
|
||||||
|
|
||||||
__indexBuildCompletionCallbackForTests: ?()=>void;
|
__indexBuildCompletionCallbackForTests: ?()=>void;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user