From 0531b16f62b6ab76fc5099d4bcdbc38a0bce862a Mon Sep 17 00:00:00 2001 From: gazebo Date: Wed, 11 Dec 2019 14:35:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E9=99=A4io.TeeReader=E7=9A=84?= =?UTF-8?q?=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- platformapi/platformapi.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/platformapi/platformapi.go b/platformapi/platformapi.go index aab9b2ed..e305d9d5 100644 --- a/platformapi/platformapi.go +++ b/platformapi/platformapi.go @@ -4,7 +4,6 @@ import ( "bytes" "errors" "fmt" - "io" "io/ioutil" "math" "net" @@ -81,15 +80,14 @@ var ( ErrStrCallbackSignatureIsWrong = "wrong signature" ) -func getClonedData(requestURL *url.URL, r *bytes.Buffer) string { +func getClonedData(requestURL *url.URL, data []byte) string { if strings.Index(requestURL.String(), "uploadImg") >= 0 { return "binary content" } - retVal := string(r.Bytes()) - if len(retVal) > maxDataSizeDontOutput { + if len(data) > maxDataSizeDontOutput { return "request data is too large" } - return retVal + return string(data) } func NewDefAPIConfig() (conf *APIConfig) { @@ -101,10 +99,16 @@ func AccessPlatformAPIWithRetry(client *http.Client, handleRequest func() *http. exceedLimitRetryCount := 0 recoverableErrorRetryCount := 0 for { - savedBuf := new(bytes.Buffer) + var savedBuf []byte request := handleRequest() if request.Body != nil { - request.Body = ioutil.NopCloser(io.TeeReader(request.Body, savedBuf)) + savedBuf2, err2 := ioutil.ReadAll(request.Body) + if err2 == nil { + savedBuf = savedBuf2 + request.Body = ioutil.NopCloser(bytes.NewReader(savedBuf)) + } else { + baseapi.SugarLogger.Warnf("AccessPlatformAPIWithRetry failed with err:%v", err2) + } } beginTime := time.Now() trackInfo := request.Header.Get(KeyTrackInfo)