Move http.Client to receiver struct
This commit is contained in:
parent
84c4c45d5b
commit
1daa751cb2
2 changed files with 6 additions and 6 deletions
8
main.go
8
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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue