github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/engines/redis/redis.go (about) 1 /* 2 Copyright 2021 The Dapr Authors 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 http://www.apache.org/licenses/LICENSE-2.0 7 Unless required by applicable law or agreed to in writing, software 8 distributed under the License is distributed on an "AS IS" BASIS, 9 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 See the License for the specific language governing permissions and 11 limitations under the License. 12 */ 13 14 package redis 15 16 import ( 17 "crypto/tls" 18 "fmt" 19 "strings" 20 "time" 21 22 "github.com/redis/go-redis/v9" 23 ) 24 25 const ( 26 ClusterType = "cluster" 27 NodeType = "node" 28 ) 29 30 func ParseClientFromProperties(properties map[string]string, defaultSettings *Settings) (client redis.UniversalClient, settings *Settings, err error) { 31 if defaultSettings == nil { 32 settings = &Settings{} 33 } else { 34 settings = defaultSettings 35 } 36 err = settings.Decode(properties) 37 if err != nil { 38 return nil, nil, fmt.Errorf("redis client configuration error: %w", err) 39 } 40 if settings.Failover { 41 return newFailoverClient(settings), settings, nil 42 } 43 44 return newClient(settings), settings, nil 45 } 46 47 func newFailoverClient(s *Settings) redis.UniversalClient { 48 if s == nil { 49 return nil 50 } 51 opts := &redis.FailoverOptions{ 52 DB: s.DB, 53 MasterName: s.SentinelMasterName, 54 SentinelAddrs: []string{s.Host}, 55 Password: s.Password, 56 Username: s.Username, 57 MaxRetries: s.RedisMaxRetries, 58 MaxRetryBackoff: time.Duration(s.RedisMaxRetryInterval), 59 MinRetryBackoff: time.Duration(s.RedisMinRetryInterval), 60 DialTimeout: time.Duration(s.DialTimeout), 61 ReadTimeout: time.Duration(s.ReadTimeout), 62 WriteTimeout: time.Duration(s.WriteTimeout), 63 PoolSize: s.PoolSize, 64 MinIdleConns: s.MinIdleConns, 65 PoolTimeout: time.Duration(s.PoolTimeout), 66 } 67 68 /* #nosec */ 69 if s.EnableTLS { 70 opts.TLSConfig = &tls.Config{ 71 InsecureSkipVerify: s.EnableTLS, 72 } 73 } 74 75 if s.RedisType == ClusterType { 76 opts.SentinelAddrs = strings.Split(s.Host, ",") 77 78 return redis.NewFailoverClusterClient(opts) 79 } 80 81 return redis.NewFailoverClient(opts) 82 } 83 84 func newClient(s *Settings) redis.UniversalClient { 85 if s == nil { 86 return nil 87 } 88 if s.RedisType == ClusterType { 89 options := &redis.ClusterOptions{ 90 Addrs: strings.Split(s.Host, ","), 91 Password: s.Password, 92 Username: s.Username, 93 MaxRetries: s.RedisMaxRetries, 94 MaxRetryBackoff: time.Duration(s.RedisMaxRetryInterval), 95 MinRetryBackoff: time.Duration(s.RedisMinRetryInterval), 96 DialTimeout: time.Duration(s.DialTimeout), 97 ReadTimeout: time.Duration(s.ReadTimeout), 98 WriteTimeout: time.Duration(s.WriteTimeout), 99 PoolSize: s.PoolSize, 100 MinIdleConns: s.MinIdleConns, 101 PoolTimeout: time.Duration(s.PoolTimeout), 102 } 103 /* #nosec */ 104 if s.EnableTLS { 105 options.TLSConfig = &tls.Config{ 106 InsecureSkipVerify: s.EnableTLS, 107 } 108 } 109 110 return redis.NewClusterClient(options) 111 } 112 113 options := &redis.Options{ 114 Addr: s.Host, 115 Password: s.Password, 116 Username: s.Username, 117 DB: s.DB, 118 MaxRetries: s.RedisMaxRetries, 119 MaxRetryBackoff: time.Duration(s.RedisMaxRetryInterval), 120 MinRetryBackoff: time.Duration(s.RedisMinRetryInterval), 121 DialTimeout: time.Duration(s.DialTimeout), 122 ReadTimeout: time.Duration(s.ReadTimeout), 123 WriteTimeout: time.Duration(s.WriteTimeout), 124 PoolSize: s.PoolSize, 125 MinIdleConns: s.MinIdleConns, 126 PoolTimeout: time.Duration(s.PoolTimeout), 127 } 128 129 /* #nosec */ 130 if s.EnableTLS { 131 options.TLSConfig = &tls.Config{ 132 InsecureSkipVerify: s.EnableTLS, 133 } 134 } 135 136 return redis.NewClient(options) 137 }