add retry controller for poster (#1082)
This commit is contained in:
parent
1ea30e03a4
commit
a8dcb1fe83
|
@ -6,9 +6,11 @@ import (
|
|||
"io/ioutil"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/toolkits/pkg/logger"
|
||||
)
|
||||
|
||||
func PostJSON(url string, timeout time.Duration, v interface{}) (response []byte, code int, err error) {
|
||||
func PostJSON(url string, timeout time.Duration, v interface{}, retreis ...int) (response []byte, code int, err error) {
|
||||
var bs []byte
|
||||
|
||||
bs, err = json.Marshal(v)
|
||||
|
@ -26,7 +28,29 @@ func PostJSON(url string, timeout time.Duration, v interface{}) (response []byte
|
|||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
var resp *http.Response
|
||||
resp, err = client.Do(req)
|
||||
|
||||
if len(retreis) > 0 {
|
||||
for i := 0; i < retreis[0]; i++ {
|
||||
resp, err = client.Do(req)
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
|
||||
tryagain := ""
|
||||
if i+1 < retreis[0] {
|
||||
tryagain = " try again"
|
||||
}
|
||||
|
||||
logger.Warningf("failed to curl %s error: %s"+tryagain, url, err)
|
||||
|
||||
if i+1 < retreis[0] {
|
||||
time.Sleep(time.Millisecond * 200)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
resp, err = client.Do(req)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
|
@ -66,7 +66,7 @@ func SendDingtalk(message DingtalkMessage) {
|
|||
}
|
||||
}
|
||||
|
||||
res, code, err := poster.PostJSON(ur, time.Second*5, body)
|
||||
res, code, err := poster.PostJSON(ur, time.Second*5, body, 3)
|
||||
if err != nil {
|
||||
logger.Errorf("dingtalk_sender: result=fail url=%s code=%d error=%v response=%s", ur, code, err, string(res))
|
||||
} else {
|
||||
|
|
|
@ -42,7 +42,7 @@ func SendFeishu(message FeishuMessage) {
|
|||
},
|
||||
}
|
||||
|
||||
res, code, err := poster.PostJSON(url, time.Second*5, body)
|
||||
res, code, err := poster.PostJSON(url, time.Second*5, body, 3)
|
||||
if err != nil {
|
||||
logger.Errorf("feishu_sender: result=fail url=%s code=%d error=%v response=%s", url, code, err, string(res))
|
||||
} else {
|
||||
|
|
|
@ -31,7 +31,7 @@ func SendWecom(message WecomMessage) {
|
|||
},
|
||||
}
|
||||
|
||||
res, code, err := poster.PostJSON(url, time.Second*5, body)
|
||||
res, code, err := poster.PostJSON(url, time.Second*5, body, 3)
|
||||
if err != nil {
|
||||
logger.Errorf("wecom_sender: result=fail url=%s code=%d error=%v response=%s", url, code, err, string(res))
|
||||
} else {
|
||||
|
|
|
@ -32,7 +32,7 @@ func callback(event *models.AlertCurEvent) {
|
|||
url = "http://" + url
|
||||
}
|
||||
|
||||
resp, code, err := poster.PostJSON(url, 5*time.Second, event)
|
||||
resp, code, err := poster.PostJSON(url, 5*time.Second, event, 3)
|
||||
if err != nil {
|
||||
logger.Errorf("event_callback(rule_id=%d url=%s) fail, resp: %s, err: %v, code: %d", event.RuleId, url, string(resp), err, code)
|
||||
} else {
|
||||
|
|
|
@ -119,7 +119,6 @@ func jwtMock() gin.HandlerFunc {
|
|||
"refresh_token": "",
|
||||
}, nil)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue