Skip to content

Commit f52e190

Browse files
author
Nick
committed
close redis conn
1 parent 5cc0e57 commit f52e190

File tree

5 files changed

+23
-15
lines changed

5 files changed

+23
-15
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
.DEFAULT: help
22

33
IMAGE_NAME ?= lampnick/kong-rate-limiting-plugin-golang
4-
CENTOS_IMAGE_TAG ?= kong-2.1.3-centos-v1.1.0
5-
ALPINE_IMAGE_TAG ?= kong-2.1.3-alpine-v1.1.0
4+
CENTOS_IMAGE_TAG ?= kong-2.1.3-centos-v1.1.1
5+
ALPINE_IMAGE_TAG ?= kong-2.1.3-alpine-v1.1.1
66

77
KONG_NET_NAME := $(shell docker network ls|grep kong-net|awk '{print $$2}')
88
P="\\033[34m===>\\033[0m"

custom-rate-limiting.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ const matchConditionAnd = "and"
4848

4949
var ctx = context.Background()
5050

51-
//redis客户端
52-
var redisClient *redis.Client
53-
5451
//限流资源列表
5552
var limitResourceList []limitResource
5653

@@ -95,8 +92,7 @@ func (conf Config) Access(kong *pdk.PDK) {
9592
_ = kong.Log.Err("[checkConfig] ", err.Error())
9693
return
9794
}
98-
//初始化redis
99-
conf.initRedisClient()
95+
10096
//检查当前请求是否需要限流
10197
limitKey, matched := conf.checkNeedRateLimit(kong)
10298
if !matched {
@@ -180,6 +176,8 @@ func (conf Config) getRemainingAndIncr(kong *pdk.PDK, identifier string, unix in
180176
end
181177
return newVal - 1
182178
`
179+
redisClient := conf.newRedisClient()
180+
defer redisClient.Close()
183181
result, err := redisClient.Eval(ctx, luaScript, []string{limitKey}, 1, 1).Result()
184182
if err == redis.Nil {
185183
return remaining, stop, nil
@@ -252,15 +250,15 @@ func (conf Config) getPrefix() string {
252250
return prefix + rateLimitPrefix
253251
}
254252

255-
//初始化redis客户端
256-
func (conf Config) initRedisClient() {
253+
//redis客户端
254+
func (conf Config) newRedisClient() *redis.Client {
257255
options := &redis.Options{
258256
Addr: conf.RedisHost + ":" + strconv.Itoa(conf.RedisPort),
259257
Password: conf.RedisAuth,
260258
DB: conf.RedisDB,
261259
DialTimeout: time.Duration(conf.RedisTimeoutSecond) * time.Second,
262260
}
263-
redisClient = redis.NewClient(options)
261+
return redis.NewClient(options)
264262
}
265263

266264
//检查并返回是否需要限流的key

custom-rate-limiting_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ const wrongJsonNokey = `
7171
`
7272

7373
//right redis host
74-
const redisHostRight = "10.5.24.223"
74+
const redisHostRight = "localhost"
7575

7676
//right redis port
7777
const redisPortRight = 6379
7878

7979
//right redis auth
80-
const redisAuthRight = "123"
80+
const redisAuthRight = ""
8181

8282
//err redis host
8383
const redisHostErr = "10.10.0.10"
@@ -453,7 +453,6 @@ func getDefaultConf() *Config {
453453
func TestGetRemainingAndIncr(t *testing.T) {
454454
kong := &pdk.PDK{}
455455
conf := getDefaultConf()
456-
conf.initRedisClient()
457456
remaining, stop, _ := conf.getRemainingAndIncr(kong, "username-nick", 1600067356)
458457
if remaining != 30 && stop != false {
459458
t.Errorf("getRemainingAndIncr return: [%v %v], rateLimitKeyExpected: [%v %v]", remaining, stop, 30, false)
@@ -463,7 +462,6 @@ func TestGetRemainingAndIncr(t *testing.T) {
463462
func TestGetRemainingAndIncrConcurrent(t *testing.T) {
464463
kong := &pdk.PDK{}
465464
conf := getDefaultConf()
466-
conf.initRedisClient()
467465
var wg sync.WaitGroup
468466
for i := 0; i <= 100; i++ {
469467
wg.Add(1)
@@ -534,7 +532,7 @@ func TestRedisEval(t *testing.T) {
534532
DB: 0,
535533
DialTimeout: time.Duration(2) * time.Second,
536534
}
537-
redisClient = redis.NewClient(options)
535+
redisClient := redis.NewClient(options)
538536
limitKey := "kong:customratelimit:service:0ff4659d-f65a-453f-bc68-7aa49bcf3a80:route:ca5ae44b-bd8f-4b4b-948f-9ba67e35085f:second:1599811581"
539537

540538
luaScript := `

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ require (
66
github.com/Kong/go-pdk v0.5.0
77
github.com/go-playground/universal-translator v0.17.0 // indirect
88
github.com/go-redis/redis/v8 v8.0.0-beta.12
9+
github.com/kr/text v0.2.0 // indirect
910
github.com/leodido/go-urn v1.2.0 // indirect
11+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
12+
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
1013
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
1114
gopkg.in/go-playground/validator.v9 v9.31.0
1215
)

go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ github.com/Kong/go-pdk v0.5.0 h1:nH9wIQKmNht9gvZXIqWU4vxiaJ59hES40npkmK5rHLQ=
44
github.com/Kong/go-pdk v0.5.0/go.mod h1:SgpwOhzZ7/44ETAm756laAIHGEAp0l2Vf9P0aRzeQDQ=
55
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
66
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
7+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
78
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
89
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
910
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -38,8 +39,12 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
3839
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
3940
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
4041
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
42+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
43+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
4144
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
4245
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
46+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
47+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
4348
github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78=
4449
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
4550
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
@@ -67,6 +72,7 @@ golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMx
6772
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
6873
golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
6974
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
75+
golang.org/x/mod v0.3.1-0.20200828183125-ce943fd02449 h1:xUIPaMhvROX9dhPvRCenIJtU78+lbEenGbgqB5hfHCQ=
7076
golang.org/x/mod v0.3.1-0.20200828183125-ce943fd02449/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
7177
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
7278
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
@@ -92,6 +98,7 @@ golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
9298
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
9399
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
94100
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
101+
golang.org/x/tools v0.0.0-20200207183749-b753a1ba74fa h1:5E4dL8+NgFOgjwbTKz+OOEGGhP+ectTmF842l6KjupQ=
95102
golang.org/x/tools v0.0.0-20200207183749-b753a1ba74fa/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
96103
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
97104
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
@@ -107,6 +114,8 @@ google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2
107114
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
108115
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
109116
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
117+
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
118+
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
110119
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
111120
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
112121
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=

0 commit comments

Comments
 (0)