Skip to content
Merged
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
10 changes: 10 additions & 0 deletions cli/exp_scaletest_notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ func (r *RootCmd) scaletestNotifications() *serpent.Command {
triggerTimes[id] = make(chan time.Time, 1)
}

smtpHTTPTransport := &http.Transport{
MaxConnsPerHost: 512,
MaxIdleConnsPerHost: 512,
IdleConnTimeout: 60 * time.Second,
}
smtpHTTPClient := &http.Client{
Transport: smtpHTTPTransport,
}

configs := make([]notifications.Config, 0, userCount)
for range templateAdminCount {
config := notifications.Config{
Expand All @@ -157,6 +166,7 @@ func (r *RootCmd) scaletestNotifications() *serpent.Command {
Metrics: metrics,
SMTPApiURL: smtpAPIURL,
SMTPRequestTimeout: smtpRequestTimeout,
SMTPHttpClient: smtpHTTPClient,
}
if err := config.Validate(); err != nil {
return xerrors.Errorf("validate config: %w", err)
Expand Down
8 changes: 8 additions & 0 deletions scaletest/notifications/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package notifications

import (
"net/http"
"sync"
"time"

Expand Down Expand Up @@ -40,6 +41,9 @@ type Config struct {

// SMTPRequestTimeout is the timeout for SMTP requests.
SMTPRequestTimeout time.Duration `json:"smtp_request_timeout"`

// SMTPHttpClient is the HTTP client for SMTP requests.
SMTPHttpClient *http.Client `json:"-"`
}

func (c Config) Validate() error {
Expand Down Expand Up @@ -68,6 +72,10 @@ func (c Config) Validate() error {
return xerrors.New("smtp_request_timeout must be set if smtp_api_url is set")
}

if c.SMTPApiURL != "" && c.SMTPHttpClient == nil {
return xerrors.New("smtp_http_client must be set if smtp_api_url is set")
}

if c.DialTimeout <= 0 {
return xerrors.New("dial_timeout must be greater than 0")
}
Expand Down
15 changes: 9 additions & 6 deletions scaletest/notifications/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,16 @@ func (r *Runner) watchNotificationsSMTP(ctx context.Context, user codersdk.User,
receivedNotifications := make(map[uuid.UUID]struct{})

apiURL := fmt.Sprintf("%s/messages?email=%s", r.cfg.SMTPApiURL, user.Email)
httpClient := &http.Client{
Timeout: r.cfg.SMTPRequestTimeout,
}
httpClient := r.cfg.SMTPHttpClient

const smtpPollInterval = 2 * time.Second
done := xerrors.New("done")

tkr := r.clock.TickerFunc(ctx, smtpPollInterval, func() error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiURL, nil)
reqCtx, cancel := context.WithTimeout(ctx, r.cfg.SMTPRequestTimeout)
defer cancel()

req, err := http.NewRequestWithContext(reqCtx, http.MethodGet, apiURL, nil)
if err != nil {
logger.Error(ctx, "create SMTP API request", slog.Error(err))
r.cfg.Metrics.AddError("smtp_create_request")
Expand All @@ -317,14 +318,16 @@ func (r *Runner) watchNotificationsSMTP(ctx context.Context, user codersdk.User,
if err != nil {
logger.Error(ctx, "poll smtp api for notifications", slog.Error(err))
r.cfg.Metrics.AddError("smtp_poll")
return xerrors.Errorf("poll smtp api: %w", err)
return nil
}

if resp.StatusCode != http.StatusOK {
// discard the response to allow reusing of the connection
_, _ = io.Copy(io.Discard, resp.Body)
_ = resp.Body.Close()
logger.Error(ctx, "smtp api returned non-200 status", slog.F("status", resp.StatusCode))
r.cfg.Metrics.AddError("smtp_bad_status")
return xerrors.Errorf("smtp api returned status %d", resp.StatusCode)
return nil
}

var summaries []smtpmock.EmailSummary
Expand Down
3 changes: 3 additions & 0 deletions scaletest/notifications/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ func TestRunWithSMTP(t *testing.T) {
smtpTrap := mClock.Trap().TickerFunc("smtp")
defer smtpTrap.Close()

httpClient := &http.Client{}

// Start receiving runners who will receive notifications
receivingRunners := make([]*notifications.Runner, 0, numReceivingUsers)
for i := range numReceivingUsers {
Expand All @@ -229,6 +231,7 @@ func TestRunWithSMTP(t *testing.T) {
ExpectedNotificationsIDs: expectedNotificationsIDs,
SMTPApiURL: smtpAPIServer.URL,
SMTPRequestTimeout: testutil.WaitLong,
SMTPHttpClient: httpClient,
}
err := runnerCfg.Validate()
require.NoError(t, err)
Expand Down
Loading