41 lines
764 B
Go
41 lines
764 B
Go
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)
|
|
}
|