|
5 | 5 | "context"
|
6 | 6 | "encoding/json"
|
7 | 7 | "errors"
|
| 8 | +"fmt" |
8 | 9 | "io"
|
9 | 10 | "net/http"
|
10 | 11 | "text/template"
|
@@ -39,7 +40,22 @@ type WebhookPayload struct {
|
39 | 40 | }
|
40 | 41 |
|
41 | 42 | funcNewWebhookHandler(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 | +varrt 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}} |
43 | 59 | }
|
44 | 60 |
|
45 | 61 | func (w*WebhookHandler)Dispatcher(payload types.MessagePayload,titleMarkdown,bodyMarkdownstring,_ template.FuncMap) (DeliveryFunc,error) {
|
|