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.
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?
Most helpful comment
@thinkingserious Any idea when a new version with this change will be released?