- use millisecond for limit access speed
This commit is contained in:
@@ -2,28 +2,40 @@ package ebaiapi
|
||||
|
||||
import "git.rosy.net.cn/baseapi/platformapi"
|
||||
|
||||
const (
|
||||
allAPI = "all"
|
||||
)
|
||||
|
||||
var (
|
||||
apiLimitConfigs = map[string]*platformapi.LimiterConfig{
|
||||
allAPI: &platformapi.LimiterConfig{
|
||||
MaxAccessCount: 5,
|
||||
TimeGapMilliSecond: 1000,
|
||||
},
|
||||
"sku.create": &platformapi.LimiterConfig{
|
||||
MaxAccessCount: 5 - 1,
|
||||
TimeGapInSecond: 1,
|
||||
MaxAccessCount: 5 - 1,
|
||||
TimeGapMilliSecond: 1000,
|
||||
},
|
||||
"sku.delete": &platformapi.LimiterConfig{
|
||||
MaxAccessCount: 1,
|
||||
TimeGapInSecond: 1,
|
||||
MaxAccessCount: 1,
|
||||
TimeGapMilliSecond: 2000,
|
||||
},
|
||||
"sku.shop.category.delete": &platformapi.LimiterConfig{
|
||||
MaxAccessCount: 5 - 2,
|
||||
TimeGapInSecond: 1,
|
||||
MaxAccessCount: 1,
|
||||
TimeGapMilliSecond: 1000,
|
||||
},
|
||||
"sku.shop.category.create": &platformapi.LimiterConfig{
|
||||
MaxAccessCount: 5 - 1,
|
||||
TimeGapInSecond: 1,
|
||||
MaxAccessCount: 2,
|
||||
TimeGapMilliSecond: 1000,
|
||||
},
|
||||
"sku.list": &platformapi.LimiterConfig{
|
||||
MaxAccessCount: 5 - 3,
|
||||
TimeGapMilliSecond: 1000,
|
||||
},
|
||||
}
|
||||
|
||||
defaultAPILimitConfig = &platformapi.LimiterConfig{
|
||||
MaxAccessCount: 5 - 2,
|
||||
TimeGapInSecond: 1,
|
||||
MaxAccessCount: 5 - 2,
|
||||
TimeGapMilliSecond: 1000,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -72,6 +72,7 @@ func (a *API) signParams(params url.Values) string {
|
||||
}
|
||||
|
||||
func (a *API) AccessAPI(cmd string, body map[string]interface{}) (retVal *ResponseResult, err error) {
|
||||
// a.speedLimiter.AccessAPI(allAPI)
|
||||
a.speedLimiter.AccessAPI(cmd)
|
||||
if body == nil {
|
||||
body = make(map[string]interface{}, 0)
|
||||
|
||||
@@ -8,8 +8,8 @@ import (
|
||||
)
|
||||
|
||||
type LimiterConfig struct {
|
||||
MaxAccessCount int
|
||||
TimeGapInSecond int
|
||||
MaxAccessCount int
|
||||
TimeGapMilliSecond int
|
||||
}
|
||||
|
||||
type Limiter struct {
|
||||
@@ -23,7 +23,7 @@ func New(config map[string]*LimiterConfig, defaultConfig ...*LimiterConfig) *Lim
|
||||
limitConfig: make(map[string]*ratelimit.Bucket),
|
||||
}
|
||||
for k, v := range config {
|
||||
limiter.limitConfig[k] = ratelimit.NewBucketWithQuantum(time.Duration(v.TimeGapInSecond)*time.Second, int64(v.MaxAccessCount), int64(v.MaxAccessCount))
|
||||
limiter.limitConfig[k] = ratelimit.NewBucketWithQuantum(time.Duration(v.TimeGapMilliSecond)*time.Millisecond, int64(v.MaxAccessCount), int64(v.MaxAccessCount))
|
||||
}
|
||||
if len(defaultConfig) > 0 {
|
||||
limiter.defaultConfig = defaultConfig[0]
|
||||
@@ -40,7 +40,7 @@ func (l *Limiter) AccessAPI(apiName string) {
|
||||
l.Lock()
|
||||
if bucket = l.limitConfig[apiName]; bucket == nil {
|
||||
v := l.defaultConfig
|
||||
bucket = ratelimit.NewBucketWithQuantum(time.Duration(v.TimeGapInSecond)*time.Second, int64(v.MaxAccessCount), int64(v.MaxAccessCount))
|
||||
bucket = ratelimit.NewBucketWithQuantum(time.Duration(v.TimeGapMilliSecond)*time.Millisecond, int64(v.MaxAccessCount), int64(v.MaxAccessCount))
|
||||
l.limitConfig[apiName] = bucket
|
||||
}
|
||||
l.Unlock()
|
||||
|
||||
@@ -11,16 +11,16 @@ import (
|
||||
func TestLimitSpeed(t *testing.T) {
|
||||
limiter := New(map[string]*LimiterConfig{
|
||||
"limited1persecond": &LimiterConfig{
|
||||
MaxAccessCount: 1,
|
||||
TimeGapInSecond: 1,
|
||||
MaxAccessCount: 1,
|
||||
TimeGapMilliSecond: 1000,
|
||||
},
|
||||
"limited10per10second": &LimiterConfig{
|
||||
MaxAccessCount: 10,
|
||||
TimeGapInSecond: 10,
|
||||
MaxAccessCount: 10,
|
||||
TimeGapMilliSecond: 10 * 1000,
|
||||
},
|
||||
}, &LimiterConfig{
|
||||
MaxAccessCount: 1,
|
||||
TimeGapInSecond: 2,
|
||||
MaxAccessCount: 1,
|
||||
TimeGapMilliSecond: 2000,
|
||||
})
|
||||
|
||||
go func() {
|
||||
|
||||
@@ -43,7 +43,7 @@ func UnmarshalUseNumber(data []byte, result interface{}) error {
|
||||
func MustMarshal(obj interface{}) []byte {
|
||||
byteArr, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("err when Marshal obj:%v", obj))
|
||||
panic(fmt.Sprintf("err when Marshal obj:%v with error:%v", obj, err))
|
||||
}
|
||||
return byteArr
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user