Skip to content

Commit

Permalink
chore: pass http agent options to client
Browse files Browse the repository at this point in the history
  • Loading branch information
tiwarishubham635 committed Oct 23, 2024
1 parent e2ed9ac commit 08b4487
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 5 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,27 @@ const client = require('twilio')(accountSid, authToken, {
});
```

### Set HTTP Agent Options

`twilio-node` allows you to set HTTP Agent Options in the Request Client. This feature allows you to re-use your connections. To enable this feature, instantiate the Twilio client with the `keepAlive` flag set to `true`.

Optionally, the socket timeout and maximum number of sockets can also be set. See the example below:

```javascript
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;

const client = require('twilio')(accountSid, authToken, {
timeout: 30000, // HTTPS agent's socket timeout in milliseconds, default is 30000
keepAlive: true, // https.Agent keepAlive option, default is false
keepAliveMsecs: 1000, // https.Agent keepAliveMsecs option in milliseconds, default is 1000
maxSockets: 20, // https.Agent maxSockets option, default is 20
maxTotalSockets: 100, // https.Agent maxTotalSockets option, default is 100
maxFreeSockets: 5, // https.Agent maxFreeSockets option, default is 5
scheduling: "lifo", // https.Agent scheduling option, default is 'lifo'
});
```

### Specify Region and/or Edge

To take advantage of Twilio's [Global Infrastructure](https://www.twilio.com/docs/global-infrastructure), specify the target Region and/or Edge for the client:
Expand Down
48 changes: 47 additions & 1 deletion src/base/BaseTwilio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,20 @@ namespace Twilio {
logLevel?: string;
userAgentExtensions?: string[];
autoRetry?: boolean;
maxRetryDelay?: number;
maxRetries?: number;

/**
https.Agent options
*/
timeout?: number;
keepAlive?: boolean;
keepAliveMsecs?: number;
maxSockets?: number;
maxTotalSockets?: number;
maxFreeSockets?: number;
scheduling?: "fifo" | "lifo" | undefined;
ca?: string | Buffer;
}

export interface RequestOpts {
Expand Down Expand Up @@ -51,8 +64,22 @@ namespace Twilio {
edge?: string;
region?: string;
logLevel?: string;
autoRetry: boolean;
autoRetry?: boolean;
maxRetryDelay?: number;
maxRetries?: number;

/**
https.Agent options
*/
timeout?: number;
keepAlive?: boolean;
keepAliveMsecs?: number;
maxSockets?: number;
maxTotalSockets?: number;
maxFreeSockets?: number;
scheduling?: "fifo" | "lifo" | undefined;
ca?: string | Buffer;

userAgentExtensions?: string[];
_httpClient?: RequestClient;

Expand Down Expand Up @@ -99,7 +126,17 @@ namespace Twilio {
this.opts.logLevel ??
this.env.TWILIO_LOG_LEVEL ??
process.env.TWILIO_LOG_LEVEL;

this.timeout = this.opts.timeout;
this.keepAlive = this.opts.keepAlive || false;
this.keepAliveMsecs = this.opts.keepAliveMsecs;
this.maxSockets = this.opts.maxSockets;
this.maxTotalSockets = this.opts.maxTotalSockets;
this.maxFreeSockets= this.opts.maxFreeSockets;
this.scheduling = this.opts.scheduling;
this.ca = this.opts.ca;
this.autoRetry = this.opts.autoRetry || false;
this.maxRetryDelay = this.opts.maxRetryDelay;
this.maxRetries = this.opts.maxRetries;
this.userAgentExtensions = this.opts.userAgentExtensions || [];
this._httpClient = this.opts.httpClient;
Expand All @@ -120,7 +157,16 @@ namespace Twilio {
get httpClient() {
if (!this._httpClient) {
this._httpClient = new RequestClient({
timeout: this.timeout,
keepAlive: this.keepAlive,
keepAliveMsecs: this.keepAliveMsecs,
maxSockets: this.maxSockets,
maxTotalSockets: this.maxTotalSockets,
maxFreeSockets: this.maxFreeSockets,
scheduling: this.scheduling,
ca: this.ca,
autoRetry: this.autoRetry,
maxRetryDelay: this.maxRetryDelay,
maxRetries: this.maxRetries,
});
}
Expand Down
11 changes: 7 additions & 4 deletions src/base/RequestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const DEFAULT_TIMEOUT = 30000;
const DEFAULT_INITIAL_RETRY_INTERVAL_MILLIS = 100;
const DEFAULT_MAX_RETRY_DELAY = 3000;
const DEFAULT_MAX_RETRIES = 3;
const DEFAULT_MAX_SOCKETS = 20;
const DEFAULT_MAX_FREE_SOCKETS = 5;
const DEFAULT_MAX_TOTAL_SOCKETS = 100;

interface BackoffAxiosRequestConfig extends AxiosRequestConfig {
/**
Expand Down Expand Up @@ -98,11 +101,11 @@ class RequestClient {
// construct an https agent
let agentOpts: https.AgentOptions = {
timeout: this.defaultTimeout,
keepAlive: opts.keepAlive,
keepAlive: opts.keepAlive || false,
keepAliveMsecs: opts.keepAliveMsecs,
maxSockets: opts.maxSockets,
maxTotalSockets: opts.maxTotalSockets,
maxFreeSockets: opts.maxFreeSockets,
maxSockets: opts.maxSockets || DEFAULT_MAX_SOCKETS, // no of sockets open per host
maxTotalSockets: opts.maxTotalSockets || DEFAULT_MAX_TOTAL_SOCKETS, // no of sockets open in total
maxFreeSockets: opts.maxFreeSockets || DEFAULT_MAX_FREE_SOCKETS, // no of free sockets open per host
scheduling: opts.scheduling,
ca: opts.ca,
};
Expand Down

0 comments on commit 08b4487

Please sign in to comment.