sourcegraph/internal/txemail
Geoffrey Gilmore fd2c675c2b
fix/internal/txemail: add timeout for SMTP connection establishment (#63759)
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](https://pkg.go.dev/net#Dialer.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](https://pkg.go.dev/net/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
](67ee9b3a69/enterprise/dev/site-config.json (L8-L11))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.
2024-07-10 12:34:06 -07:00
..
txtypes bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
BUILD.bazel bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
siteconfig.go conf: Move most confvalidators into common package (#61810) 2024-04-12 06:30:52 +02:00
template_test.go Migrate to autogold/v2 (needed by Bazel) (#47891) 2023-02-21 10:37:13 +01:00
template.go email.templates: make 'text' template optional (#46085) 2023-01-03 11:17:24 -08:00
txemail_test.go txemail: add ability to set sender name (#49906) 2023-03-23 23:03:08 +09:00
txemail.go fix/internal/txemail: add timeout for SMTP connection establishment (#63759) 2024-07-10 12:34:06 -07:00