123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- package tests
- import (
- "gd_auth_check/common.in/cache"
- "gd_auth_check/common.in/clinit"
- "gd_auth_check/common.in/config"
- "gd_auth_check/limit"
- "encoding/json"
- "fmt"
- "log"
- "strings"
- "testing"
- "time"
- _ "github.com/go-sql-driver/mysql"
- )
- func TestLimiter(t *testing.T) {
- // 连接数据库服务器
- clinit.InitMySQL(&clinit.MysqlConfig{
- User: "root",
- Password: "Pwd#123456",
- Addr: "47.103.130.208:3308",
- DB: "db_gd_management",
- Charset: "utf8",
- MaxIdle: "10",
- MaxConn: "100",
- })
- // 连接redis服务器
- cache.InitRedis(&cache.RedisConfig{
- Addrs: strings.Split("47.103.130.208:63779", ","),
- Password: "gd_prv",
- DB: "4",
- PoolSize: "100",
- MinIdleConns: "10",
- MaxRetries: "1",
- IsCluster: "false",
- })
- config.Conf = &config.Configure{
- RateLimit: config.RateLimitConf{
- RateLimitChannel: "AAAAAAAAAAAA",
- RescueTickerTime: json.Number("20"),
- },
- }
- limit.InitLimiter()
- // router := "/api/v2/vehicle/P010/series"
- // fmt.Println(limit.Allow(router, "A"))
- // exist, allow, token := limit.Allow("feaa17a9c5bbff40c98976df8ad85d74", "/api/v2/maintenance/city")
- // log.Println(exist, allow, token)
- // // if exist && allow {
- // // limit.ReleaseMerchantLimiterToken(router, "feaa17a9c5bbff40c98976df8ad85d74", "dfd9ea35-e7e1-4c8a-abad-c4ac2a972e65-1")
- // // }
- // // limit.Reload("1")
- exist, allow := limit.Allow("/api/v2/vehicle/P011/brand", "")
- log.Println(exist, allow)
- time.Sleep(30 * time.Second)
- if exist == nil {
- limit.ReleaseRouterLimiterToken(allow.Router, allow.RouterToken)
- }
- }
- func TestReleases(t *testing.T) {
- // 连接redis服务器
- cache.InitRedis(&cache.RedisConfig{
- Addrs: strings.Split("47.103.130.208:63779", ","),
- Password: "gd_prv",
- DB: "0",
- PoolSize: "100",
- MinIdleConns: "10",
- MaxRetries: "1",
- IsCluster: "false",
- })
- }
- func TestRedisEx(t *testing.T) {
- // 连接数据库服务器
- clinit.InitMySQL(&clinit.MysqlConfig{
- User: "root",
- Password: "Pwd#123456",
- Addr: "47.103.130.208:3308",
- DB: "db_gd_management",
- Charset: "utf8",
- MaxIdle: "10",
- MaxConn: "100",
- })
- // 连接redis服务器
- cache.InitRedis(&cache.RedisConfig{
- Addrs: strings.Split("47.103.130.208:63779", ","),
- Password: "gd_prv",
- DB: "4",
- PoolSize: "100",
- MinIdleConns: "10",
- MaxRetries: "1",
- IsCluster: "false",
- })
- config.Conf = &config.Configure{
- RateLimit: config.RateLimitConf{
- RateLimitChannel: "AAAAAAAAAAAA",
- RescueTickerTime: json.Number("3"),
- },
- }
- limit.InitLimiter()
- pubsub := cache.Redis.Subscribe("__keyevent@4__:expired")
- defer pubsub.Close()
- eventKey := "__keyevent@4__:expired"
- redisUsedTokenKey := "rate_limit:used_token:"
- for msg := range pubsub.Channel() {
- fmt.Println(msg.Channel, msg.Channel == eventKey, msg.Payload, strings.Index(msg.Payload, redisUsedTokenKey))
- if msg.Channel == eventKey {
- if msg.Payload != "" && strings.Index(msg.Payload, redisUsedTokenKey) == 0 {
- key := strings.Replace(msg.Payload, redisUsedTokenKey, "", 1)
- fmt.Println(key)
- keyArr := strings.Split(key, ":")
- fmt.Printf("keyArr: %+v\n", keyArr)
- switch len(keyArr) {
- case 2:
- limit.ReleaseRouterLimiterToken(keyArr[0], keyArr[1])
- case 3:
- limit.ReleaseMerchantLimiterToken(keyArr[1], keyArr[0], keyArr[2])
- }
- }
- }
- }
- }
|