Skip to content

Commit

Permalink
add shutdown hook for redis
Browse files Browse the repository at this point in the history
  • Loading branch information
yinheli committed Jun 27, 2021
1 parent 5f45799 commit 0024a86
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pkg/redisx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/md5"
"encoding/hex"
"fmt"
"os"
"strings"
"time"

Expand All @@ -14,6 +15,7 @@ import (
"go.uber.org/zap"

"github.com/xinpianchang/xservice/pkg/log"
"github.com/xinpianchang/xservice/pkg/signalx"
)

var (
Expand Down Expand Up @@ -83,8 +85,8 @@ func Config(v *viper.Viper) {
log.Fatal("ping", zap.String("name", c.Name), zap.Error(err))
}

log.Debug(fmt.Sprint("redis ", c.Name, " ping"),
zap.String("rsp", r.Val()), zap.String("addr", c.Addr), zap.Int("db", c.DB))
// log.Debug(fmt.Sprint("redis ", c.Name, " ping"),
// zap.String("rsp", r.Val()), zap.String("addr", c.Addr), zap.Int("db", c.DB))

client.AddHook(&redisTracing{})

Expand All @@ -96,6 +98,12 @@ func Config(v *viper.Viper) {
Locker = redislock.New(client)
}
}

signalx.AddShutdownHook(func(os.Signal) {
for _, c := range clients {
_ = c.Close()
}
})
}

func GetClient(name string) *redis.Client {
Expand Down

0 comments on commit 0024a86

Please sign in to comment.