Skip to content

Commit 338e8b5

Browse files
authored
fix: use new http transport for webhook handler (#19462)
1 parent 2521e73 commit 338e8b5

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

coderd/notifications/dispatch/webhook.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"encoding/json"
77
"errors"
8+
"fmt"
89
"io"
910
"net/http"
1011
"text/template"
@@ -39,7 +40,22 @@ type WebhookPayload struct {
3940
}
4041

4142
func NewWebhookHandler(cfg codersdk.NotificationsWebhookConfig, log slog.Logger) *WebhookHandler {
42-
return &WebhookHandler{cfg: cfg, log: log, cl: &http.Client{}}
43+
// Create a new transport in favor of reusing the default, since other http clients may interfere.
44+
// http.Transport maintains its own connection pool, and we want to avoid cross-contamination.
45+
var rt http.RoundTripper
46+
47+
def := http.DefaultTransport
48+
t, ok := def.(*http.Transport)
49+
if !ok {
50+
// The API has changed (very unlikely), so let's use the default transport (previous behavior) and log.
51+
log.Warn(context.Background(), "failed to clone default HTTP transport, unexpected type", slog.F("type", fmt.Sprintf("%T", def)))
52+
rt = def
53+
} else {
54+
// Clone the transport's exported fields, but not its connection pool.
55+
rt = t.Clone()
56+
}
57+
58+
return &WebhookHandler{cfg: cfg, log: log, cl: &http.Client{Transport: rt}}
4359
}
4460

4561
func (w *WebhookHandler) Dispatcher(payload types.MessagePayload, titleMarkdown, bodyMarkdown string, _ template.FuncMap) (DeliveryFunc, error) {

coderd/notifications/dispatch/webhook_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func TestWebhook(t *testing.T) {
131131
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
132132
tc.serverFn(msgID, w, r)
133133
}))
134-
defer server.Close()
134+
t.Cleanup(server.Close)
135135

136136
endpoint, err = url.Parse(server.URL)
137137
require.NoError(t, err)

0 commit comments

Comments
 (0)