- up
This commit is contained in:
@@ -39,10 +39,11 @@ func (l *Limiter) AccessAPI(apiName string) {
|
|||||||
if bucket == nil && l.defaultConfig != nil {
|
if bucket == nil && l.defaultConfig != nil {
|
||||||
l.Lock()
|
l.Lock()
|
||||||
if bucket = l.limitConfig[apiName]; bucket == nil {
|
if bucket = l.limitConfig[apiName]; bucket == nil {
|
||||||
v := l.defaultConfig
|
if v := l.defaultConfig; v != nil {
|
||||||
bucket = ratelimit.NewBucketWithQuantum(time.Duration(v.TimeGapMilliSecond)*time.Millisecond, 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()
|
||||||
}
|
}
|
||||||
if bucket != nil {
|
if bucket != nil {
|
||||||
|
|||||||
@@ -4,8 +4,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.rosy.net.cn/baseapi/utils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLimitSpeed(t *testing.T) {
|
func TestLimitSpeed(t *testing.T) {
|
||||||
@@ -27,7 +25,7 @@ func TestLimitSpeed(t *testing.T) {
|
|||||||
count := 0
|
count := 0
|
||||||
for {
|
for {
|
||||||
limiter.AccessAPI("limited1persecond")
|
limiter.AccessAPI("limited1persecond")
|
||||||
fmt.Printf("limited1persecond, time:%s, count:%d\n", utils.GetCurTimeStr(), count)
|
fmt.Printf("limited1persecond, time:%s, count:%d\n", time.Now().Format("2006-01-02 15:04:05.00000"), count)
|
||||||
count++
|
count++
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@@ -35,7 +33,7 @@ func TestLimitSpeed(t *testing.T) {
|
|||||||
count := 0
|
count := 0
|
||||||
for {
|
for {
|
||||||
limiter.AccessAPI("limited10per10second")
|
limiter.AccessAPI("limited10per10second")
|
||||||
fmt.Printf("limited10per10second, time:%s, count:%d\n", utils.GetCurTimeStr(), count)
|
fmt.Printf("limited10per10second, time:%s, count:%d\n", time.Now().Format("2006-01-02 15:04:05.00000"), count)
|
||||||
count++
|
count++
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@@ -43,7 +41,7 @@ func TestLimitSpeed(t *testing.T) {
|
|||||||
count := 0
|
count := 0
|
||||||
for {
|
for {
|
||||||
limiter.AccessAPI("otherAPI")
|
limiter.AccessAPI("otherAPI")
|
||||||
fmt.Printf("otherAPI, time:%s, count:%d\n", utils.GetCurTimeStr(), count)
|
fmt.Printf("otherAPI, time:%s, count:%d\n", time.Now().Format("2006-01-02 15:04:05.00000"), count)
|
||||||
count++
|
count++
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|||||||
Reference in New Issue
Block a user