Skip to content

Commit

Permalink
feat: use shared agent and for all SF connections for more efficient …
Browse files Browse the repository at this point in the history
…socket pooling Increase timeout values so that larger deploy requests using POST REST do not cause a timeout, allow changing and overwriting the shared agent to increase socket pool or strategy
  • Loading branch information
Codeneos committed Feb 15, 2024
1 parent bff8d30 commit a2961ff
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
56 changes: 38 additions & 18 deletions packages/salesforce/src/connection/httpTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,34 @@ export interface HttpResponse {
body?: any;
}

/**
* Represents the information required to make an HTTP request.
*/
export interface HttpRequestInfo {
/**
* URL to make the request to
*/
url: string;
/**
* HTTP method to use for the request (GET, POST, PATCH, DELETE, PUT)
*/
method: HttpMethod;
/**
* Extra headers to include in the request on top of the standard headers
*/
headers?: http.OutgoingHttpHeaders;
/**
* Body of the request in case of a POST, PATCH or PUT request
*/
body?: string | undefined;
/**
* Parts of the request in case of a multipart request
*/
parts?: HttpRequestPart[] | undefined;
/**
* Timeout in milliseconds for the request
*/
timeout?: number;
}

export interface HttpRequestPart {
Expand Down Expand Up @@ -65,12 +87,6 @@ interface HttpTransportOptions {
*/
useGzipEncoding: boolean;

/**
* Include a keep-alive header in all requests to re-use the HTTP connection and socket.
* @default true
*/
shouldKeepAlive: boolean;

/**
* Parse set-cookies header and store cookies to be included in the request header on subsequent requests.
*
Expand All @@ -92,6 +108,12 @@ interface HttpTransportOptions {
* @default undefined
*/
recorder?: { record<T extends HttpResponse>(info: HttpRequestInfo, responsePromise: Promise<T>): Promise<T> };

/**
* Optional HTTP agent used by the transport for connection pooling
* @default HttpTransport.httpAgent
*/
agent?: https.Agent;
}

export interface Transport {
Expand All @@ -103,15 +125,14 @@ export class HttpTransport implements Transport {
private multiPartBoundary = `--${randomUUID()}`

/**
* HTTP agent used by this {@link HttpTransport} used for connection pooling
* Default shared HTTP agent used by this {@link HttpTransport} used for connection pooling
*/
private httpAgent = new https.Agent({
port: 443,
public static httpAgent = new https.Agent({
keepAlive: true,
keepAliveMsecs: 5000,
maxSockets: 10,
maxSockets: 5,
scheduling: 'lifo',
timeout: 120000 // Time out connections after 120 seconds
timeout: 5 * 60 * 1000 // Time out connections after 5 minutes
});

/**
Expand All @@ -131,7 +152,6 @@ export class HttpTransport implements Transport {
static options: HttpTransportOptions = {
gzipThreshold: 128,
useGzipEncoding: true,
shouldKeepAlive: true,
handleCookies: true,
responseDecoders: {
json: (buffer, encoding) => JSON.parse(buffer.toString(encoding)),
Expand All @@ -158,7 +178,6 @@ export class HttpTransport implements Transport {
const features = new Array<string>();
this.options.useGzipEncoding && features.push('gzip');
this.options.handleCookies && features.push('cookies');
this.options.shouldKeepAlive && features.push('keepAlive');
return features;
}

Expand Down Expand Up @@ -216,8 +235,9 @@ export class HttpTransport implements Transport {
url.protocol = 'https';
}

const httpAgent = this.options.agent || HttpTransport.httpAgent;
const request = https.request({
agent: this.httpAgent,
agent: httpAgent,
host: url.host,
path: url.pathname + url.search,
port: url.port,
Expand All @@ -226,12 +246,12 @@ export class HttpTransport implements Transport {
method: info.method
});

if (this.options.shouldKeepAlive) {
request.shouldKeepAlive = true;
if (info.timeout) {
request.setTimeout(info.timeout);
}

if (this.httpAgent.options.keepAlive !== this.options.shouldKeepAlive) {
this.httpAgent.options.keepAlive = this.options.shouldKeepAlive;
if (httpAgent.options.keepAlive) {
request.setSocketKeepAlive(true);
}

if (this.options.useGzipEncoding) {
Expand Down
1 change: 0 additions & 1 deletion packages/salesforce/src/connection/oath2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export class SalesforceOAuth2 {
handleCookies: false,
// OAuth endpoints do not support gzip encoding
useGzipEncoding: false,
shouldKeepAlive: false,
instanceUrl: options.loginUrl,
baseUrl: options.loginUrl
}, LogManager.get(SalesforceOAuth2));
Expand Down

0 comments on commit a2961ff

Please sign in to comment.