@@ -6,18 +6,26 @@ import * as Parse from "parse/node";
66// @flow -disable-next
77import * as request from "request" ;
88import { logger } from '../logger' ;
9+ import http from 'http' ;
10+ import https from 'https' ;
911
1012const DefaultHooksCollectionName = "_Hooks" ;
13+ const HTTPAgents = {
14+ http : new http . Agent ( { keepAlive : true } ) ,
15+ https : new https . Agent ( { keepAlive : true } ) ,
16+ }
1117
1218export 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 {
0 commit comments