- access frequence limit

- output detail request when error
This commit is contained in:
gazebo
2018-10-27 10:10:54 +08:00
parent 56bea8ce11
commit ecb04fbe72
5 changed files with 121 additions and 14 deletions

View File

@@ -0,0 +1,40 @@
package platformapi
import (
"fmt"
"testing"
"time"
"git.rosy.net.cn/baseapi/utils"
)
func TestLimitSpeed(t *testing.T) {
limiter := New(map[string]*LimiterConfig{
"limited1persecond": &LimiterConfig{
MaxAccessCount: 1,
TimeGapInSecond: 1,
},
"limited10per10second": &LimiterConfig{
MaxAccessCount: 10,
TimeGapInSecond: 10,
},
})
go func() {
count := 0
for {
limiter.AccessAPI("limited1persecond")
fmt.Printf("limited1persecond, time:%s, count:%d\n", utils.GetCurTimeStr(), count)
count++
}
}()
go func() {
count := 0
for {
limiter.AccessAPI("limited10per10second")
fmt.Printf("limited10per10second, time:%s, count:%d\n", utils.GetCurTimeStr(), count)
count++
}
}()
time.Sleep(30 * time.Second)
}