Skip to content

Use http agents for hook requests #4791

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 1 commit into from
May 29, 2018
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
21 changes: 17 additions & 4 deletions src/Controllers/HooksController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,26 @@ import * as Parse from "parse/node";
// @flow-disable-next
import * as request from "request";
import { logger } from '../logger';
import http from 'http';
import https from 'https';

const DefaultHooksCollectionName = "_Hooks";
const HTTPAgents = {
http: new http.Agent({ keepAlive: true }),
https: new https.Agent({ keepAlive: true }),
}

export class HooksController {
_applicationId:string;
_webhookKey:string;
database: any;
keepAlive: boolean;

constructor(applicationId:string, databaseController, webhookKey) {
constructor(applicationId:string, databaseController, webhookKey, keepAlive) {
this._applicationId = applicationId;
this._webhookKey = webhookKey;
this.database = databaseController;
this.keepAlive = keepAlive;
}

load() {
Expand Down Expand Up @@ -85,7 +93,7 @@ export class HooksController {
}

addHookToTriggers(hook) {
var wrappedFunction = wrapToHTTPRequest(hook, this._webhookKey);
var wrappedFunction = wrapToHTTPRequest(hook, this._webhookKey, this.keepAlive);
wrappedFunction.url = hook.url;
if (hook.className) {
triggers.addTrigger(hook.triggerName, hook.className, wrappedFunction, this._applicationId)
Expand Down Expand Up @@ -159,7 +167,7 @@ export class HooksController {
}
}

function wrapToHTTPRequest(hook, key) {
function wrapToHTTPRequest(hook, key, keepAlive) {
return (req, res) => {
const jsonBody = {};
for (var i in req) {
Expand All @@ -177,9 +185,14 @@ function wrapToHTTPRequest(hook, key) {
headers: {
'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) {
jsonRequest.headers['X-Parse-Webhook-Key'] = key;
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/Controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,10 @@ export function getDatabaseController(options: ParseServerOptions, cacheControll
export function getHooksController(options: ParseServerOptions, databaseController: DatabaseController): HooksController {
const {
appId,
webhookKey
webhookKey,
hookKeepAlive,
} = options;
return new HooksController(appId, databaseController, webhookKey);
return new HooksController(appId, databaseController, webhookKey, hookKeepAlive);
}

interface PushControlling {
Expand Down Expand Up @@ -228,4 +229,3 @@ export function getDatabaseAdapter(databaseURI, collectionPrefix, databaseOption
});
}
}

2 changes: 2 additions & 0 deletions src/Options/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ export interface ParseServerOptions {
startLiveQueryServer: ?boolean;
/* Live query server configuration options (will start the liveQuery server) */
liveQueryServerOptions: ?LiveQueryServerOptions;
/* Keep hook HTTP connections alive */
hookKeepAlive: ?boolean;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should default to true? What would be the drawback?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should flip the default (and potentially make it not configurable) in the future. Just wanted to dip our toe in here.

There should be very little drawback to enabling this.


__indexBuildCompletionCallbackForTests: ?()=>void;
}
Expand Down