This commit is contained in:
Ulric Qin 2022-08-02 12:21:01 +08:00
parent a8dcb1fe83
commit bb17751a81
1 changed files with 5 additions and 5 deletions

View File

@ -10,7 +10,7 @@ import (
"github.com/toolkits/pkg/logger" "github.com/toolkits/pkg/logger"
) )
func PostJSON(url string, timeout time.Duration, v interface{}, retreis ...int) (response []byte, code int, err error) { func PostJSON(url string, timeout time.Duration, v interface{}, retries ...int) (response []byte, code int, err error) {
var bs []byte var bs []byte
bs, err = json.Marshal(v) bs, err = json.Marshal(v)
@ -29,21 +29,21 @@ func PostJSON(url string, timeout time.Duration, v interface{}, retreis ...int)
var resp *http.Response var resp *http.Response
if len(retreis) > 0 { if len(retries) > 0 {
for i := 0; i < retreis[0]; i++ { for i := 0; i < retries[0]; i++ {
resp, err = client.Do(req) resp, err = client.Do(req)
if err == nil { if err == nil {
break break
} }
tryagain := "" tryagain := ""
if i+1 < retreis[0] { if i+1 < retries[0] {
tryagain = " try again" tryagain = " try again"
} }
logger.Warningf("failed to curl %s error: %s"+tryagain, url, err) logger.Warningf("failed to curl %s error: %s"+tryagain, url, err)
if i+1 < retreis[0] { if i+1 < retries[0] {
time.Sleep(time.Millisecond * 200) time.Sleep(time.Millisecond * 200)
} }
} }