Skip to content

Commit

Permalink
Merge pull request #22 from TencentBlueKing/develop
Browse files Browse the repository at this point in the history
feat: add redis sentinel support (#21)
  • Loading branch information
zhu327 authored Aug 25, 2022
2 parents c33b87e + d0c9028 commit 50ef2c5
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 5 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.0
1.1.1
6 changes: 5 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,18 @@ backend:
appSecret: "a59ddb37-94ae-4d7a-b6b8-f3c255fff041"

redis:
id: "standalone"
id: "mq"
type: "standalone"
addr: "127.0.0.1:6379"
password: ""
db: 0
poolSize: 160
dialTimeout: 3
readTimeout: 1
writeTimeout: 1
sentinelAddr: ""
masterName: "mymaster"
sentinelPassword: "" # TODO: waiting for the replace var

redisKeys:
- id: "delete_queue_key"
Expand Down
6 changes: 6 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ type Crypto struct {
// Redis ...
type Redis struct {
ID string
Type string
Addr string
Password string
DB int
Expand All @@ -98,6 +99,11 @@ type Redis struct {
WriteTimeout int
PoolSize int
MinIdleConns int

// mode=sentinel required
SentinelAddr string
MasterName string
SentinelPassword string
}

// Config ...
Expand Down
64 changes: 61 additions & 3 deletions pkg/redis/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ package redis

import (
"context"
"fmt"
"runtime"
"strings"
"sync"
"time"

Expand All @@ -23,6 +25,11 @@ import (
"engine/pkg/config"
)

const (
ModeStandalone = "standalone"
ModeSentinel = "sentinel"
)

var mqRedisClient *redis.Client

var mqRedisClientInitOnce sync.Once
Expand All @@ -31,7 +38,14 @@ var mqRedisClientInitOnce sync.Once
func InitRedisClient(debugMode bool, redisConfig *config.Redis) {
if mqRedisClient == nil {
mqRedisClientInitOnce.Do(func() {
mqRedisClient = newRedisClient(redisConfig)
switch redisConfig.Type {
case ModeStandalone:
mqRedisClient = newStandaloneClient(redisConfig)
case ModeSentinel:
mqRedisClient = newSentinelClient(redisConfig)
default:
panic(fmt.Errorf("no support redis config type: %s", redisConfig.Type))
}

_, err := mqRedisClient.Ping(context.TODO()).Result()
if err != nil {
Expand All @@ -45,7 +59,7 @@ func InitRedisClient(debugMode bool, redisConfig *config.Redis) {
}
}

func newRedisClient(redisConfig *config.Redis) *redis.Client {
func newStandaloneClient(redisConfig *config.Redis) *redis.Client {
opt := &redis.Options{
Addr: redisConfig.Addr,
Password: redisConfig.Password,
Expand Down Expand Up @@ -79,8 +93,10 @@ func newRedisClient(redisConfig *config.Redis) *redis.Client {
}

log.Infof(
"connect to redis: %s[dialTimeout=%s, readTimeout=%s, writeTimeout=%s, poolSize=%d, minIdleConns=%d, idleTimeout=%s]",
"connect to redis: "+
"%s [db=%d, dialTimeout=%s, readTimeout=%s, writeTimeout=%s, poolSize=%d, minIdleConns=%d, idleTimeout=%s]",
opt.Addr,
opt.DB,
opt.DialTimeout,
opt.ReadTimeout,
opt.WriteTimeout,
Expand All @@ -92,6 +108,48 @@ func newRedisClient(redisConfig *config.Redis) *redis.Client {
return redis.NewClient(opt)
}

func newSentinelClient(redisConfig *config.Redis) *redis.Client {
sentinelAddrs := strings.Split(redisConfig.SentinelAddr, ",")
opt := &redis.FailoverOptions{
MasterName: redisConfig.MasterName,
SentinelAddrs: sentinelAddrs,
DB: redisConfig.DB,
Password: redisConfig.Password,
}

if redisConfig.SentinelPassword != "" {
opt.SentinelPassword = redisConfig.SentinelPassword
}

// set default options
opt.DialTimeout = 2 * time.Second
opt.ReadTimeout = 1 * time.Second
opt.WriteTimeout = 1 * time.Second
opt.PoolSize = 20 * runtime.NumCPU()
opt.MinIdleConns = 10 * runtime.NumCPU()
opt.IdleTimeout = 3 * time.Minute

// set custom options, from config.yaml
if redisConfig.DialTimeout > 0 {
opt.DialTimeout = time.Duration(redisConfig.DialTimeout) * time.Second
}
if redisConfig.ReadTimeout > 0 {
opt.ReadTimeout = time.Duration(redisConfig.ReadTimeout) * time.Second
}
if redisConfig.WriteTimeout > 0 {
opt.WriteTimeout = time.Duration(redisConfig.WriteTimeout) * time.Second
}

if redisConfig.PoolSize > 0 {
opt.PoolSize = redisConfig.PoolSize
}
if redisConfig.MinIdleConns > 0 {
opt.MinIdleConns = redisConfig.MinIdleConns
}

return redis.NewFailoverClient(opt)
}

// GetDefaultRedisClient 获取默认的Redis实例
func GetDefaultMQRedisClient() *redis.Client {
return mqRedisClient
Expand Down
5 changes: 5 additions & 0 deletions release.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# 1.1.1

- add: redis sentinel support

# 1.1.0

- add: rbac poicy support

# 1.0.6

- bugfix: create index use dynamic_templates
Expand Down

0 comments on commit 50ef2c5

Please sign in to comment.