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