You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
852 B

package pkg
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"time"
)
func NotifyService(url string, body interface{}) error {
var maxRetries = 3
var retryInterval = time.Second
for i := 0; i < maxRetries; i++ {
err := NotifyHTTP(url, body)
if err == nil {
return nil // 通知发送成功,返回 nil
}
// 通知发送失败,进行重试
retryInterval += 5 * time.Second
time.Sleep(retryInterval)
}
return errors.New("notify failed after 3 attempts")
}
func NotifyHTTP(url string, body interface{}) error {
jsonBody, _ := json.Marshal(&body)
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonBody))
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return errors.New("Failed to send notification, status code: " + resp.Status)
}
return nil
}