Skip to content
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

feat: add keep alive support #1226

Closed
wants to merge 3 commits into from
Closed
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
12 changes: 12 additions & 0 deletions packages/mail/src/classes/mail-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/**
* Dependencies
*/
const Agent = require('http').Agent;
const {Client} = require('@sendgrid/client');
const {classes: {Mail}} = require('@sendgrid/helpers');

Expand Down Expand Up @@ -40,6 +41,17 @@ class MailService {
return this;
}

/**
* Reuse connections
*/
setKeepAlive(keepAlive) {
if (keepAlive === false) {
return;
Copy link
Contributor

Choose a reason for hiding this comment

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

What about turning off keep alive? If this just returns, you can never "unset" it.

Copy link
Author

Choose a reason for hiding this comment

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

keepAlive is already false by default, its an opt-in feature

}

this.client.setDefaultRequest('agent', new Agent({ keepAlive: true }));
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the key needs to be httpsAgent, according to the documentation. This logic should be moved to the underlying Client with a separate helper method that we can call from here, similar to calling this.client.setApiKey from setApiKey.

Copy link
Author

Choose a reason for hiding this comment

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

sure, good call out

}

/**
* Twilio Email Auth passthrough for convenience.
*/
Expand Down
5 changes: 5 additions & 0 deletions packages/mail/src/mail.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ declare class MailService {
*/
setApiKey(apiKey: string): void;

/**
* Reuse connections
*/
setKeepAlive(keepAlive: boolean): void;

/**
* Twilio Email Auth passthrough for convenience.
*/
Expand Down
3 changes: 3 additions & 0 deletions test/typescript/mail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import sgMail = require("@sendgrid/mail");
// Test setApiKey() method
sgMail.setApiKey("MY_SENDGRID_API_KEY");

// Test setKeepAlive() method
sgMail.setKeepAlive(true);

// Test setSubstitutionWrappers() method
sgMail.setSubstitutionWrappers("{{", "}}")

Expand Down