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

+ 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) { func (a *API) AccessAPI2(apiStr string, jdParams map[string]interface{}, traceInfo string) (retVal map[string]interface{}, err error) {
params := make(map[string]interface{}) params := make(map[string]interface{})
params["v"] = "1.0" params["v"] = "1.0"

View File

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