v1_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package tests
  2. import (
  3. "gd_auth_check/common.in/cache"
  4. "gd_auth_check/common.in/clinit"
  5. "gd_auth_check/common.in/config"
  6. "gd_auth_check/limit"
  7. "encoding/json"
  8. "fmt"
  9. "log"
  10. "strings"
  11. "testing"
  12. "time"
  13. _ "github.com/go-sql-driver/mysql"
  14. )
  15. func TestLimiter(t *testing.T) {
  16. // 连接数据库服务器
  17. clinit.InitMySQL(&clinit.MysqlConfig{
  18. User: "root",
  19. Password: "Pwd#123456",
  20. Addr: "47.103.130.208:3308",
  21. DB: "db_gd_management",
  22. Charset: "utf8",
  23. MaxIdle: "10",
  24. MaxConn: "100",
  25. })
  26. // 连接redis服务器
  27. cache.InitRedis(&cache.RedisConfig{
  28. Addrs: strings.Split("47.103.130.208:63779", ","),
  29. Password: "gd_prv",
  30. DB: "4",
  31. PoolSize: "100",
  32. MinIdleConns: "10",
  33. MaxRetries: "1",
  34. IsCluster: "false",
  35. })
  36. config.Conf = &config.Configure{
  37. RateLimit: config.RateLimitConf{
  38. RateLimitChannel: "AAAAAAAAAAAA",
  39. RescueTickerTime: json.Number("20"),
  40. },
  41. }
  42. limit.InitLimiter()
  43. // router := "/api/v2/vehicle/P010/series"
  44. // fmt.Println(limit.Allow(router, "A"))
  45. // exist, allow, token := limit.Allow("feaa17a9c5bbff40c98976df8ad85d74", "/api/v2/maintenance/city")
  46. // log.Println(exist, allow, token)
  47. // // if exist && allow {
  48. // // limit.ReleaseMerchantLimiterToken(router, "feaa17a9c5bbff40c98976df8ad85d74", "dfd9ea35-e7e1-4c8a-abad-c4ac2a972e65-1")
  49. // // }
  50. // // limit.Reload("1")
  51. exist, allow := limit.Allow("/api/v2/vehicle/P011/brand", "")
  52. log.Println(exist, allow)
  53. time.Sleep(30 * time.Second)
  54. if exist == nil {
  55. limit.ReleaseRouterLimiterToken(allow.Router, allow.RouterToken)
  56. }
  57. }
  58. func TestReleases(t *testing.T) {
  59. // 连接redis服务器
  60. cache.InitRedis(&cache.RedisConfig{
  61. Addrs: strings.Split("47.103.130.208:63779", ","),
  62. Password: "gd_prv",
  63. DB: "0",
  64. PoolSize: "100",
  65. MinIdleConns: "10",
  66. MaxRetries: "1",
  67. IsCluster: "false",
  68. })
  69. }
  70. func TestRedisEx(t *testing.T) {
  71. // 连接数据库服务器
  72. clinit.InitMySQL(&clinit.MysqlConfig{
  73. User: "root",
  74. Password: "Pwd#123456",
  75. Addr: "47.103.130.208:3308",
  76. DB: "db_gd_management",
  77. Charset: "utf8",
  78. MaxIdle: "10",
  79. MaxConn: "100",
  80. })
  81. // 连接redis服务器
  82. cache.InitRedis(&cache.RedisConfig{
  83. Addrs: strings.Split("47.103.130.208:63779", ","),
  84. Password: "gd_prv",
  85. DB: "4",
  86. PoolSize: "100",
  87. MinIdleConns: "10",
  88. MaxRetries: "1",
  89. IsCluster: "false",
  90. })
  91. config.Conf = &config.Configure{
  92. RateLimit: config.RateLimitConf{
  93. RateLimitChannel: "AAAAAAAAAAAA",
  94. RescueTickerTime: json.Number("3"),
  95. },
  96. }
  97. limit.InitLimiter()
  98. pubsub := cache.Redis.Subscribe("__keyevent@4__:expired")
  99. defer pubsub.Close()
  100. eventKey := "__keyevent@4__:expired"
  101. redisUsedTokenKey := "rate_limit:used_token:"
  102. for msg := range pubsub.Channel() {
  103. fmt.Println(msg.Channel, msg.Channel == eventKey, msg.Payload, strings.Index(msg.Payload, redisUsedTokenKey))
  104. if msg.Channel == eventKey {
  105. if msg.Payload != "" && strings.Index(msg.Payload, redisUsedTokenKey) == 0 {
  106. key := strings.Replace(msg.Payload, redisUsedTokenKey, "", 1)
  107. fmt.Println(key)
  108. keyArr := strings.Split(key, ":")
  109. fmt.Printf("keyArr: %+v\n", keyArr)
  110. switch len(keyArr) {
  111. case 2:
  112. limit.ReleaseRouterLimiterToken(keyArr[0], keyArr[1])
  113. case 3:
  114. limit.ReleaseMerchantLimiterToken(keyArr[1], keyArr[0], keyArr[2])
  115. }
  116. }
  117. }
  118. }
  119. }