- 超频重视逻辑改为按幂增长

+ jdapi.NewPageOnly
This commit is contained in:
gazebo
2019-08-15 08:54:02 +08:00
parent 2580d1a8c3
commit b338b87a02
2 changed files with 28 additions and 11 deletions

View File

@@ -144,6 +144,13 @@ func New(token, appKey, appSecret string, config ...*platformapi.APIConfig) *API
}
}
func NewPageOnly(cookie string, config ...*platformapi.APIConfig) *API {
api := New("", "", "", config...)
api.SetCookie(accessStorePageCookieName, cookie)
api.SetCookie(accessStorePageCookieName2, cookie)
return api
}
func (a *API) AccessAPI2(apiStr string, jdParams map[string]interface{}, traceInfo string) (retVal map[string]interface{}, err error) {
params := make(map[string]interface{})
params["v"] = "1.0"

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"io"
"io/ioutil"
"math"
"net"
"net/http"
"net/url"
@@ -17,18 +18,18 @@ import (
)
const (
DefClientTimeout = 30 * time.Second
DefSleepSecondWhenExceedLimit = 3 * time.Second
DefMaxRecoverableRetryCount = 1
DefMaxExceedLimitRetryCount = 25
DefClientTimeout = 30 * time.Second
DefMaxSleepSecondWhenExceedLimit = 62 // 超频类错误最大重试间隙(秒)
DefMaxExceedLimitRetryCount = 25 // 超频类错误最大重试次数
DefMaxRecoverableRetryCount = 1 // 可恢复类错误(一般指网络错),最大重试次数
KeyTrackInfo = "TrackInfo"
)
type APIRetryConfig struct {
MaxExceedLimitRetryCount int
MaxRecoverableRetryCount int
SleepSecondWhenExceedLimit time.Duration
MaxExceedLimitRetryCount int
MaxRecoverableRetryCount int
MaxSleepSecondWhenExceedLimit int
}
type APIConfig struct {
@@ -45,9 +46,9 @@ type AccessPlatformAPIWithRetryParam struct {
var (
DefAPIConfig = APIConfig{
APIRetryConfig: APIRetryConfig{
MaxExceedLimitRetryCount: DefMaxExceedLimitRetryCount,
MaxRecoverableRetryCount: DefMaxRecoverableRetryCount,
SleepSecondWhenExceedLimit: DefSleepSecondWhenExceedLimit,
MaxExceedLimitRetryCount: DefMaxExceedLimitRetryCount,
MaxRecoverableRetryCount: DefMaxRecoverableRetryCount,
MaxSleepSecondWhenExceedLimit: DefMaxSleepSecondWhenExceedLimit,
},
ClientTimeout: DefClientTimeout,
}
@@ -90,6 +91,11 @@ func getClonedData(requestURL *url.URL, r *bytes.Buffer) string {
return retVal
}
func NewDefAPIConfig() (conf *APIConfig) {
obj := DefAPIConfig
return &obj
}
func AccessPlatformAPIWithRetry(client *http.Client, handleRequest func() *http.Request, config *APIConfig, handleResponse func(response *http.Response, bodyStr string, bodyMap map[string]interface{}) (string, error)) error {
exceedLimitRetryCount := 0
recoverableErrorRetryCount := 0
@@ -172,7 +178,11 @@ func AccessPlatformAPIWithRetry(client *http.Client, handleRequest func() *http.
} else if errLevel == ErrLevelExceedLimit {
exceedLimitRetryCount++
if exceedLimitRetryCount <= config.MaxExceedLimitRetryCount {
time.Sleep(config.SleepSecondWhenExceedLimit)
sleepSeconds := int(math.Exp2(float64(exceedLimitRetryCount)))
if sleepSeconds > config.MaxSleepSecondWhenExceedLimit {
sleepSeconds = config.MaxSleepSecondWhenExceedLimit
}
time.Sleep(time.Duration(sleepSeconds) * time.Second)
continue
}
} else if errLevel == ErrLevelRecoverableErr {