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]) } } } } }