Skip to content

Commit

Permalink
feat(storers): replace go-redis by rueidis (#438)
Browse files Browse the repository at this point in the history
* feat(storers): replace go-redis by rueidis

* feat(release): prepare v1.6.45
  • Loading branch information
darkweak committed Jan 15, 2024
1 parent 4700b6d commit 5a88d84
Show file tree
Hide file tree
Showing 215 changed files with 56,732 additions and 20,582 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/plugin_template.yml
Expand Up @@ -64,9 +64,8 @@ jobs:
comparison: contains
-
name: Run ${{ inputs.CAPITALIZED_NAME }} E2E tests
uses: anthonyvscode/newman-action@v1
uses: matt-ball/newman-action@master
with:
collection: "docs/e2e/Souin E2E.postman_collection.json"
folder: ${{ inputs.CAPITALIZED_NAME }}
reporters: cli
folder: '["${{ inputs.CAPITALIZED_NAME }}"]'
delayRequest: 5000
26 changes: 4 additions & 22 deletions .github/workflows/plugins.yml
Expand Up @@ -54,11 +54,10 @@ jobs:
run: cd plugins/caddy && ./caddy run &
-
name: Run Caddy E2E tests
uses: anthonyvscode/newman-action@v1
uses: matt-ball/newman-action@master
with:
collection: "docs/e2e/Souin E2E.postman_collection.json"
folder: Caddy
reporters: cli
folder: '["Caddy"]'
delayRequest: 5000
-
name: Run detached caddy
Expand All @@ -68,11 +67,10 @@ jobs:
run: cd plugins/caddy && ./caddy run --config ./configuration.json &
-
name: Run Caddy E2E tests
uses: anthonyvscode/newman-action@v1
uses: matt-ball/newman-action@master
with:
collection: "docs/e2e/Souin E2E.postman_collection.json"
folder: Caddy
reporters: cli
folder: '["Caddy"]'
delayRequest: 5000
build-beego-validator:
name: Check that Souin build as middleware
Expand Down Expand Up @@ -130,22 +128,6 @@ jobs:
CAPITALIZED_NAME: Goa
LOWER_NAME: goa
GO_VERSION: '1.21'
build-go-zero-validator:
name: Check that Souin build as middleware
uses: ./.github/workflows/plugin_template.yml
secrets: inherit
with:
CAPITALIZED_NAME: Go-zero
LOWER_NAME: go-zero
GO_VERSION: '1.21'
build-hertz-validator:
name: Check that Souin build as middleware
uses: ./.github/workflows/plugin_template.yml
secrets: inherit
with:
CAPITALIZED_NAME: Hertz
LOWER_NAME: hertz
GO_VERSION: '1.21'
build-kratos-validator:
name: Check that Souin build as middleware
uses: ./.github/workflows/plugin_template.yml
Expand Down
14 changes: 7 additions & 7 deletions .github/workflows/workflow_plugins_generator.sh
@@ -1,6 +1,8 @@
#!/bin/bash

plugins=("beego" "chi" "dotweb" "echo" "fiber" "gin" "goa" "go-zero" "hertz" "kratos" "roadrunner" "souin" "traefik" "tyk" "webgo")
# Disabled go-zero and hertz temporary
# plugins=("beego" "chi" "dotweb" "echo" "fiber" "gin" "goa" "go-zero" "hertz" "kratos" "roadrunner" "souin" "traefik" "tyk" "webgo")
plugins=("beego" "chi" "dotweb" "echo" "fiber" "gin" "goa" "kratos" "roadrunner" "souin" "traefik" "tyk" "webgo")
go_version=1.21

IFS= read -r -d '' tpl <<EOF
Expand Down Expand Up @@ -60,11 +62,10 @@ jobs:
run: cd plugins/caddy && ./caddy run &
-
name: Run Caddy E2E tests
uses: anthonyvscode/newman-action@v1
uses: matt-ball/newman-action@master
with:
collection: "docs/e2e/Souin E2E.postman_collection.json"
folder: Caddy
reporters: cli
folder: '["Caddy"]'
delayRequest: 5000
-
name: Run detached caddy
Expand All @@ -74,11 +75,10 @@ jobs:
run: cd plugins/caddy && ./caddy run --config ./configuration.json &
-
name: Run Caddy E2E tests
uses: anthonyvscode/newman-action@v1
uses: matt-ball/newman-action@master
with:
collection: "docs/e2e/Souin E2E.postman_collection.json"
folder: Caddy
reporters: cli
folder: '["Caddy"]'
delayRequest: 5000
EOF
workflow+="$tpl"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -970,7 +970,7 @@ experimental:
plugins:
souin:
moduleName: github.com/darkweak/souin
version: v1.6.44
version: v1.6.45
```
After that you can declare either the whole configuration at once in the middleware block or by service. See the examples below.
```yaml
Expand Down
2 changes: 1 addition & 1 deletion docs/e2e/Souin E2E.postman_collection.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -13,7 +13,7 @@ require (
github.com/pquerna/cachecontrol v0.2.0
github.com/prometheus/client_golang v1.16.0
github.com/prometheus/client_model v0.4.0
github.com/redis/go-redis/v9 v9.1.0
github.com/redis/rueidis v1.0.26
go.etcd.io/etcd/client/v3 v3.5.9
go.uber.org/zap v1.26.0
golang.org/x/sync v0.5.0
Expand Down
14 changes: 8 additions & 6 deletions go.sum
Expand Up @@ -27,8 +27,6 @@ github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kB
github.com/bits-and-blooms/bitset v1.2.0/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA=
github.com/bits-and-blooms/bitset v1.5.0 h1:NpE8frKRLGHIcEzkR+gZhiioW1+WbYV6fKwD6ZIpQT8=
github.com/bits-and-blooms/bitset v1.5.0/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA=
github.com/bsm/ginkgo/v2 v2.9.5 h1:rtVBYPs3+TC5iLUVOis1B9tjLTup7Cj5IfzosKtvTJ0=
github.com/bsm/gomega v1.26.0 h1:LhQm+AFcgV2M0WyKroMASzAzCAJVpAxQXv4SaI9a69Y=
github.com/buraksezer/consistent v0.10.0 h1:hqBgz1PvNLC5rkWcEBVAL9dFMBWz6I0VgUCW25rrZlU=
github.com/buraksezer/consistent v0.10.0/go.mod h1:6BrVajWq7wbKZlTOUPs/XVfR8c0maujuPowduSpZqmw=
github.com/buraksezer/olric v0.5.4 h1:LDgLIfVoyol4qzdNirrrDUKqzFw0yDsa7ukvLrpP4cU=
Expand Down Expand Up @@ -80,10 +78,12 @@ github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2
github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY=
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw=
github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
Expand Down Expand Up @@ -123,9 +123,10 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 h1:4/hN5RUoecvl+RmJRE2YxKWtnnQls6rQjjW5oV7qg2U=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down Expand Up @@ -207,11 +208,12 @@ github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vv
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
github.com/onsi/ginkgo/v2 v2.0.0/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
github.com/onsi/ginkgo/v2 v2.12.1 h1:uHNEO1RP2SpuZApSkel9nEh1/Mu+hmQe7Q+Pepg5OYA=
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs=
github.com/onsi/gomega v1.28.0 h1:i2rg/p9n/UqIDAMFUJ6qIUUMcsqOuUHgbpbu235Vr1c=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
Expand Down Expand Up @@ -244,8 +246,8 @@ github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsT
github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A=
github.com/prometheus/procfs v0.10.1 h1:kYK1Va/YMlutzCGazswoHKo//tZVlFpKYh+PymziUAg=
github.com/prometheus/procfs v0.10.1/go.mod h1:nwNm2aOCAYw8uTR/9bWRREkZFxAUcWzPHWJq+XBB/FM=
github.com/redis/go-redis/v9 v9.1.0 h1:137FnGdk+EQdCbye1FW+qOEcY5S+SpY9T0NiuqvtfMY=
github.com/redis/go-redis/v9 v9.1.0/go.mod h1:urWj3He21Dj5k4TK1y59xH8Uj6ATueP8AH1cY3lZl4c=
github.com/redis/rueidis v1.0.26 h1:u2QVp9yEREZQ0xfUgmaOBL0mED14u+j+akkfCRcOJYw=
github.com/redis/rueidis v1.0.26/go.mod h1:NT7lPuiVYijdZVsV0V8i9ZUhqe1OMGaq+NiQigNuKlg=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
Expand Down
101 changes: 56 additions & 45 deletions pkg/storage/redisProvider.go
Expand Up @@ -6,55 +6,69 @@ import (
"context"
"encoding/json"
"fmt"
"net"
"net/http"
"regexp"
"strings"
"time"

t "github.com/darkweak/souin/configurationtypes"
"github.com/darkweak/souin/pkg/rfc"
redis "github.com/redis/go-redis/v9"
redis "github.com/redis/rueidis"
"github.com/redis/rueidis/rueidiscompat"
"go.uber.org/zap"
)

// Redis provider type
type Redis struct {
*redis.Client
Client rueidiscompat.Cmdable
stale time.Duration
ctx context.Context
logger *zap.Logger
reconnecting bool
configuration redis.Options
configuration redis.ClientOption
close func()
}

// RedisConnectionFactory function create new Nuts instance
func RedisConnectionFactory(c t.AbstractConfigurationInterface) (Storer, error) {
dc := c.GetDefaultCache()
bc, _ := json.Marshal(dc.GetRedis().Configuration)

var options redis.Options
var options redis.ClientOption
if dc.GetRedis().Configuration != nil {
if err := json.Unmarshal(bc, &options); err != nil {
c.GetLogger().Sugar().Infof("Cannot parse your redis configuration: %+v", err)
}
} else {
options = redis.Options{
Addr: dc.GetRedis().URL,
Password: "",
DB: 0,
PoolSize: 1000,
PoolTimeout: dc.GetTimeout().Cache.Duration,
options = redis.ClientOption{
InitAddress: strings.Split(dc.GetRedis().URL, ","),
Dialer: net.Dialer{
Timeout: dc.GetTimeout().Cache.Duration,
},
}
}

cli := redis.NewClient(&options)
if options.Dialer.Timeout == 0 {
options.Dialer.Timeout = time.Second
}

cli, err := redis.NewClient(options)
if err != nil {
return nil, err
}

// TODO remove this adapter to use directly rueidis
compat := rueidiscompat.NewAdapter(cli)

return &Redis{
Client: cli,
Client: compat,
ctx: context.Background(),
stale: dc.GetStale(),
configuration: options,
logger: c.GetLogger(),
}, nil
close: cli.Close,
}, err
}

// Name returns the storer name
Expand All @@ -68,19 +82,8 @@ func (provider *Redis) ListKeys() []string {
provider.logger.Sugar().Error("Impossible to list the redis keys while reconnecting.")
return []string{}
}
keys := []string{}

iter := provider.Client.Scan(provider.ctx, 0, "*", 0).Iterator()
for iter.Next(provider.ctx) {
keys = append(keys, string(iter.Val()))
}
if err := iter.Err(); err != nil {
if !provider.reconnecting {
go provider.Reconnect()
}
provider.logger.Sugar().Error(err)
return []string{}
}
keys, _ := provider.Client.Scan(provider.ctx, 0, "*", 0).Val()

return keys
}
Expand Down Expand Up @@ -113,15 +116,15 @@ func (provider *Redis) Prefix(key string, req *http.Request, validator *rfc.Reva
in := make(chan *http.Response)
out := make(chan bool)

iter := provider.Client.Scan(provider.ctx, 0, key+"*", 0).Iterator()
go func(iterator *redis.ScanIterator) {
for iterator.Next(provider.ctx) {
keys, _, _ := provider.Client.Scan(provider.ctx, 0, key+"*", 0).Result()
go func(ks []string) {
for _, k := range ks {
select {
case <-out:
return
case <-time.After(1 * time.Nanosecond):
if varyVoter(key, req, iter.Val()) {
v, e := provider.Client.Get(provider.ctx, iter.Val()).Result()
if varyVoter(key, req, k) {
v, e := provider.Client.Get(provider.ctx, k).Result()
if e != nil && e != redis.Nil && !provider.reconnecting {
go provider.Reconnect()
in <- nil
Expand All @@ -130,20 +133,20 @@ func (provider *Redis) Prefix(key string, req *http.Request, validator *rfc.Reva
if res, err := http.ReadResponse(bufio.NewReader(bytes.NewBuffer([]byte(v))), req); err == nil {
rfc.ValidateETag(res, validator)
if validator.Matched {
provider.logger.Sugar().Debugf("The stored key %s matched the current iteration key ETag %+v", iter.Val(), validator)
provider.logger.Sugar().Debugf("The stored key %s matched the current iteration key ETag %+v", k, validator)
in <- res
return
}
provider.logger.Sugar().Errorf("The stored key %s didn't match the current iteration key ETag %+v", iter.Val(), validator)
provider.logger.Sugar().Errorf("The stored key %s didn't match the current iteration key ETag %+v", k, validator)
}
}
}
}
in <- nil
}(iter)
}(keys)

select {
case <-time.After(provider.Client.Options().PoolTimeout):
case <-time.After(provider.configuration.Dialer.Timeout):
out <- true
return nil
case v := <-in:
Expand Down Expand Up @@ -201,18 +204,13 @@ func (provider *Redis) DeleteMany(key string) {
}

keys := []string{}
iter := provider.Client.Scan(provider.ctx, 0, "*", 0).Iterator()
for iter.Next(provider.ctx) {
if re.MatchString(iter.Val()) {
keys = append(keys, iter.Val())
rKeys, _ := provider.Client.Scan(provider.ctx, 0, "*", 0).Val()
for _, rkey := range rKeys {
if re.MatchString(rkey) {
keys = append(keys, rkey)
}
}

if iter.Err() != nil && !provider.reconnecting {
go provider.Reconnect()
return
}

provider.Client.Del(provider.ctx, keys...)
}

Expand All @@ -227,13 +225,26 @@ func (provider *Redis) Reset() error {
provider.logger.Sugar().Error("Impossible to reset the redis instance while reconnecting.")
return nil
}
return provider.Client.Close()
provider.close()

return nil
}

func (provider *Redis) Reconnect() {
provider.reconnecting = true

if provider.Client = redis.NewClient(&provider.configuration); provider.Client != nil {
cli, err := redis.NewClient(provider.configuration)
if err != nil {
time.Sleep(10 * time.Second)
provider.Reconnect()

return
}

// TODO remove this adapter to use directly rueidis
provider.Client = rueidiscompat.NewAdapter(cli)
provider.close = cli.Close
if provider.Client != nil {
provider.reconnecting = false
} else {
time.Sleep(10 * time.Second)
Expand Down

0 comments on commit 5a88d84

Please sign in to comment.