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 }