add retry controller for poster (#1082)
This commit is contained in:
parent
1ea30e03a4
commit
a8dcb1fe83
|
@ -6,9 +6,11 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"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
|
var bs []byte
|
||||||
|
|
||||||
bs, err = json.Marshal(v)
|
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")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
var resp *http.Response
|
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 {
|
if err != nil {
|
||||||
return
|
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 {
|
if err != nil {
|
||||||
logger.Errorf("dingtalk_sender: result=fail url=%s code=%d error=%v response=%s", ur, code, err, string(res))
|
logger.Errorf("dingtalk_sender: result=fail url=%s code=%d error=%v response=%s", ur, code, err, string(res))
|
||||||
} else {
|
} 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 {
|
if err != nil {
|
||||||
logger.Errorf("feishu_sender: result=fail url=%s code=%d error=%v response=%s", url, code, err, string(res))
|
logger.Errorf("feishu_sender: result=fail url=%s code=%d error=%v response=%s", url, code, err, string(res))
|
||||||
} else {
|
} 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 {
|
if err != nil {
|
||||||
logger.Errorf("wecom_sender: result=fail url=%s code=%d error=%v response=%s", url, code, err, string(res))
|
logger.Errorf("wecom_sender: result=fail url=%s code=%d error=%v response=%s", url, code, err, string(res))
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -32,7 +32,7 @@ func callback(event *models.AlertCurEvent) {
|
||||||
url = "http://" + url
|
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 {
|
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)
|
logger.Errorf("event_callback(rule_id=%d url=%s) fail, resp: %s, err: %v, code: %d", event.RuleId, url, string(resp), err, code)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -119,7 +119,6 @@ func jwtMock() gin.HandlerFunc {
|
||||||
"refresh_token": "",
|
"refresh_token": "",
|
||||||
}, nil)
|
}, nil)
|
||||||
c.Abort()
|
c.Abort()
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue