Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

fix/internal/txemail: add timeout for SMTP connection establishment#63759

Merged
ggilmore merged 1 commit intomainfrom
graphite-ggilmoreenhancement_internal_txemail_add_30_second_timeout_to_connect_to_smtp_server
Jul 10, 2024
Merged

fix/internal/txemail: add timeout for SMTP connection establishment#63759
ggilmore merged 1 commit intomainfrom
graphite-ggilmoreenhancement_internal_txemail_add_30_second_timeout_to_connect_to_smtp_server

Conversation

@ggilmore
Copy link
Contributor

@ggilmore ggilmore commented Jul 10, 2024

Before, the smtp.Dial function had no cancellation mechanism - meaning that you could be waiting for several minutes to try to establish a connection before it gives up.

I work around this by establishing a TCP connection myself to the appropriate address, and using net.DialContext to cancel the dial process if either:

  1. The parent context cancels
  2. 30 seconds have passed

Afterwards, we construct the smtp.Client ourselves using smtp.NewClient

This only changes what happens when we try to establish a connection - it doesn't change any behavior around sending emails afterwards.

Test plan

Manual testing.

  1. On local dev, I edited the credentials in the dev-private site configuration file to point to an invalid port.

  2. I attempted to create an access token, and after only30 seconds I saw this error print in the terminal:

[       frontend] WARN schemaResolver.CreateAccessToken graphqlbackend/access_tokens.go:127 Failed to send email to inform user of access token creation {"userID": 1, "error": "establishing TCP connection to \"smtp.gmail.com:57\": dial tcp 74.125.137.109:57: i/o timeout"}

Changelog

  • Instead of waiting forever, we wait at most 30 seconds before giving up when trying to connect to the configured mail server when sending an email.

@cla-bot cla-bot bot added the cla-signed label Jul 10, 2024
Copy link
Contributor Author

Copy link
Contributor Author

@ggilmore ggilmore changed the title enhancement/internal/txemail: add 30 second timeout to connect to SMTP server fix/internal/txemail: add timeout for SMTP connection establishment Jul 10, 2024
@ggilmore ggilmore marked this pull request as ready for review July 10, 2024 18:46
@ggilmore ggilmore requested review from a team July 10, 2024 18:46
Copy link
Member

@bobheadxi bobheadxi left a comment

Choose a reason for hiding this comment

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

Nice, thanks :D

@camdencheek
Copy link
Member

Upstream an smtp.DialContext?

@eseliger
Copy link
Member

They froze the package. I personally think this is quite acceptable, it's just composing the net and smpt packages together into something we like to use 🤔

@ggilmore ggilmore merged commit fd2c675 into main Jul 10, 2024
Copy link
Contributor Author

Merge activity

@ggilmore ggilmore deleted the graphite-ggilmoreenhancement_internal_txemail_add_30_second_timeout_to_connect_to_smtp_server branch July 10, 2024 19:34
@camdencheek
Copy link
Member

I personally think this is quite acceptable

Oh, agreed! Mostly just thought this would be a fun little opportunity to contribute to stdlib since adding context-accepting variants is pretty standard across stdlib. Didn't realize the package was frozen though

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

cla-signed team/product-platform team/source Tickets under the purview of Source - the one Source to graph it all

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants