Sendgrid-nodejs: Timeout implementation for @sendgrid/mail

Created on 22 Feb 2019  路  6Comments  路  Source: sendgrid/sendgrid-nodejs

I am using @sendgrid/mail in an AWS lambda function. For some reason, some of the requests do not receive a response within the max allowed execution time of the function (5s). This causes duplicate deliveries of the same mail (oops).

My solution would be to implement a timeout on the request itself which is < than the max allowed execution time of the function (to prevent retry).

Currently, the @sendgrid/mail package does not have a way a to set a timeout on @sendgrid/client. Fortunately, the @sendgrid/client class has a setDefaultRequest method which allows for a clean implementation of the timeout:

/**
 * Set client timeout
 */
setTimeout(timeout) {
  if (typeof timeout === 'undefined') {
    return;
  }

  this.client.setDefaultRequest('timeout', timeout);
}

Usage:

sgMail.setTimeout(6000);

This would change the timeout option in https://github.com/request/request#requestoptions-callback:

Time to wait for a server to send response headers (and start the response body) before aborting the request.

I'm happy to make a PR if this is acceptable.

medium work in progress community enhancement

Most helpful comment

@thinkingserious Any idea when a new version with this change will be released?

All 6 comments

I love it! Thanks for offering to submit the PR!

@thinkingserious It seems PRs keep piling up. Anything I can do to help?

I have been trying to use the timeout function you have created but I am keep getting the same error.

sgMail.setTimeout is not a function

Any thoughts on that?

It looks like this didn't make it into 6.4.0. I'd appreciate it if you could release a new minor version with this in it.

Yea it is taking quite some time indeed. I just created a patch with this specific change and integrated it in my build process.

@thinkingserious Any idea when a new version with this change will be released?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mikemaccana picture mikemaccana  路  4Comments

amlcodes picture amlcodes  路  4Comments

prasoonjalan picture prasoonjalan  路  3Comments

TobiahRex picture TobiahRex  路  3Comments

kiranshashiny picture kiranshashiny  路  4Comments