去除io.TeeReader的使用

This commit is contained in:
gazebo
2019-12-11 14:35:27 +08:00
parent 0df9427112
commit 0531b16f62

View File

@@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"math" "math"
"net" "net"
@@ -81,15 +80,14 @@ var (
ErrStrCallbackSignatureIsWrong = "wrong signature" 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 { if strings.Index(requestURL.String(), "uploadImg") >= 0 {
return "binary content" return "binary content"
} }
retVal := string(r.Bytes()) if len(data) > maxDataSizeDontOutput {
if len(retVal) > maxDataSizeDontOutput {
return "request data is too large" return "request data is too large"
} }
return retVal return string(data)
} }
func NewDefAPIConfig() (conf *APIConfig) { func NewDefAPIConfig() (conf *APIConfig) {
@@ -101,10 +99,16 @@ func AccessPlatformAPIWithRetry(client *http.Client, handleRequest func() *http.
exceedLimitRetryCount := 0 exceedLimitRetryCount := 0
recoverableErrorRetryCount := 0 recoverableErrorRetryCount := 0
for { for {
savedBuf := new(bytes.Buffer) var savedBuf []byte
request := handleRequest() request := handleRequest()
if request.Body != nil { 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() beginTime := time.Now()
trackInfo := request.Header.Get(KeyTrackInfo) trackInfo := request.Header.Get(KeyTrackInfo)