diff --git a/main.go b/main.go index 6958636..a46e5da 100644 --- a/main.go +++ b/main.go @@ -25,6 +25,7 @@ type receiver struct { cfg *config logger *log.Logger cache *cache + client *http.Client } type payload struct { @@ -227,7 +228,6 @@ func (rcv *receiver) multiAlertNotification(p *payload) *notification { } func (rcv *receiver) publish(n *notification) error { - client := &http.Client{Timeout: time.Second * 3} req, err := http.NewRequest(http.MethodPost, rcv.cfg.ntfy.Topic, strings.NewReader(n.body)) if err != nil { return err @@ -261,7 +261,7 @@ func (rcv *receiver) publish(n *notification) error { req.Header.Set("Actions", fmt.Sprintf("http, Silence, %s, method=POST, body=%s%s", url, n.silenceBody, authString)) } - resp, err := client.Do(req) + resp, err := rcv.client.Do(req) if err != nil { return err } @@ -376,7 +376,9 @@ func main() { logger.Errorf("config: %v", err) } - receiver := &receiver{cfg: cfg, logger: logger, cache: newCache(cfg.cache.Duration)} + client := &http.Client{Timeout: time.Second * 3} + + receiver := &receiver{cfg: cfg, logger: logger, cache: newCache(cfg.cache.Duration), client: client} logger.Infof("Listening on %s, ntfy-alertmanager %s", cfg.HTTPAddress, version) diff --git a/silence.go b/silence.go index d3c11a3..34c4a40 100644 --- a/silence.go +++ b/silence.go @@ -90,8 +90,6 @@ func (rcv *receiver) handleSilences(w http.ResponseWriter, r *http.Request) { return } - client := &http.Client{Timeout: time.Second * 3} - url := sb.AlertManagerURL if rcv.cfg.am.URL != "" { url = rcv.cfg.am.URL @@ -111,7 +109,7 @@ func (rcv *receiver) handleSilences(w http.ResponseWriter, r *http.Request) { } req.Header.Add("Content-Type", "application/json") - resp, err := client.Do(req) + resp, err := rcv.client.Do(req) if err != nil { rcv.logger.Debugf("silences: %v", err) return