From a8dcb1fe838404d44986978b2ff98d279b3e66f1 Mon Sep 17 00:00:00 2001 From: ulricqin Date: Tue, 2 Aug 2022 12:20:02 +0800 Subject: [PATCH] add retry controller for poster (#1082) --- src/pkg/poster/post.go | 28 ++++++++++++++++++++++++++-- src/server/common/sender/dingtalk.go | 2 +- src/server/common/sender/feishu.go | 2 +- src/server/common/sender/wecom.go | 2 +- src/server/engine/callback.go | 2 +- src/webapi/router/router_mw.go | 1 - 6 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/pkg/poster/post.go b/src/pkg/poster/post.go index 158df3a6..abbcfce0 100644 --- a/src/pkg/poster/post.go +++ b/src/pkg/poster/post.go @@ -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 } diff --git a/src/server/common/sender/dingtalk.go b/src/server/common/sender/dingtalk.go index 178e62ce..032a4611 100644 --- a/src/server/common/sender/dingtalk.go +++ b/src/server/common/sender/dingtalk.go @@ -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 { diff --git a/src/server/common/sender/feishu.go b/src/server/common/sender/feishu.go index 829a06b4..d91e2cfb 100644 --- a/src/server/common/sender/feishu.go +++ b/src/server/common/sender/feishu.go @@ -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 { diff --git a/src/server/common/sender/wecom.go b/src/server/common/sender/wecom.go index 1634247e..77cb35db 100644 --- a/src/server/common/sender/wecom.go +++ b/src/server/common/sender/wecom.go @@ -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 { diff --git a/src/server/engine/callback.go b/src/server/engine/callback.go index b5578beb..60cf8e9c 100644 --- a/src/server/engine/callback.go +++ b/src/server/engine/callback.go @@ -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 { diff --git a/src/webapi/router/router_mw.go b/src/webapi/router/router_mw.go index 73d4788b..3d71a020 100644 --- a/src/webapi/router/router_mw.go +++ b/src/webapi/router/router_mw.go @@ -119,7 +119,6 @@ func jwtMock() gin.HandlerFunc { "refresh_token": "", }, nil) c.Abort() - return } }