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