Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: improve http connection pooling for smtp notifications#20605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
kacpersaw wants to merge2 commits intomain
base:main
Choose a base branch
Loading
fromkacpersaw/scaletest-notifications-smtp-tuning
Open
Show file tree
Hide file tree
Changes fromall 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
11 changes: 11 additions & 0 deletionscli/exp_scaletest_notifications.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -142,6 +142,16 @@ func (r *RootCmd) scaletestNotifications() *serpent.Command {
triggerTimes[id] = make(chan time.Time, 1)
}

smtpHTTPTransport := &http.Transport{
MaxConnsPerHost: 512,
MaxIdleConnsPerHost: 512,
MaxIdleConns: 2048,
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 +167,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 deletionsscaletest/notifications/config.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
package notifications

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

Expand DownExpand 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 DownExpand 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 deletionsscaletest/notifications/run.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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")
returnxerrors.Errorf("poll smtp api: %w", err)
returnnil
}

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")
returnxerrors.Errorf("smtp api returned status %d", resp.StatusCode)
returnnil
}

var summaries []smtpmock.EmailSummary
Expand Down
3 changes: 3 additions & 0 deletionsscaletest/notifications/run_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp