Move http.Client to receiver struct

This commit is contained in:
Thorben Günther 2023-02-17 01:45:14 +01:00
parent 84c4c45d5b
commit 1daa751cb2
No known key found for this signature in database
GPG key ID: 415CD778D8C5AFED
2 changed files with 6 additions and 6 deletions

View file

@ -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)

View file

@ -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