@@ -112,12 +112,102 @@ type Redis struct {
112112}
113113
114114type Config struct {
115+ Enabled bool `json:"enabled" yaml:"enabled"`
115116 Rules []* Rule `json:"rules" yaml:"rules"`
116117 Redis * Redis `json:"redis" yaml:"redis"`
117118 ErrorCode int32 `json:"errorCode" yaml:"errorCode"`
118119 ErrorMessage string `json:"errorMessage" yaml:"errorMessage"`
119120}
120121
122+ func NewDefaultRedisConfig () * Redis {
123+ return & Redis {
124+ Addrs : []* RedisAddr {
125+ {
126+ Name : DefaultRedisAddrName ,
127+ Port : DefaultRedisAddrPort ,
128+ },
129+ },
130+ DialTimeout : DefaultRedisTimeout ,
131+ ReadTimeout : DefaultRedisTimeout ,
132+ WriteTimeout : DefaultRedisTimeout ,
133+ PoolTimeout : DefaultRedisTimeout ,
134+ PoolSize : DefaultRedisPoolSize ,
135+ MinIdleConns : DefaultRedisMinIdleConns ,
136+ MaxRetries : DefaultRedisMaxRetries ,
137+ }
138+ }
139+
140+ func NewDefaultConfig () * Config {
141+ return & Config {
142+ Enabled : false ,
143+ Rules : nil ,
144+ Redis : nil ,
145+ ErrorCode : DefaultErrorCode ,
146+ ErrorMessage : DefaultErrorMessage ,
147+ }
148+ }
149+
150+ func (c * Redis ) setDefaultConfigOptions () {
151+ if c == nil {
152+ return
153+ }
154+ if len (c .Addrs ) == 0 {
155+ c .Addrs = []* RedisAddr {
156+ {
157+ Name : DefaultRedisAddrName ,
158+ Port : DefaultRedisAddrPort ,
159+ },
160+ }
161+ }
162+ for i := range c .Addrs {
163+ if c .Addrs [i ] == nil {
164+ continue
165+ }
166+ if strings .TrimSpace (c .Addrs [i ].Name ) == "" {
167+ c .Addrs [i ].Name = DefaultRedisAddrName
168+ }
169+ if c .Addrs [i ].Port == 0 {
170+ c .Addrs [i ].Port = DefaultRedisAddrPort
171+ }
172+ }
173+ if c .DialTimeout == 0 {
174+ c .DialTimeout = DefaultRedisTimeout
175+ }
176+ if c .ReadTimeout == 0 {
177+ c .ReadTimeout = DefaultRedisTimeout
178+ }
179+ if c .WriteTimeout == 0 {
180+ c .WriteTimeout = DefaultRedisTimeout
181+ }
182+ if c .PoolTimeout == 0 {
183+ c .PoolTimeout = DefaultRedisTimeout
184+ }
185+ if c .PoolSize == 0 {
186+ c .PoolSize = DefaultRedisPoolSize
187+ }
188+ if c .MinIdleConns == 0 {
189+ c .MinIdleConns = DefaultRedisMinIdleConns
190+ }
191+ if c .MaxRetries == 0 {
192+ c .MaxRetries = DefaultRedisMaxRetries
193+ }
194+ }
195+
196+ func (c * Config ) setDefaultConfigOptions () {
197+ if c == nil {
198+ return
199+ }
200+ if c .ErrorCode == 0 {
201+ c .ErrorCode = DefaultErrorCode
202+ }
203+ if strings .TrimSpace (c .ErrorMessage ) == "" {
204+ c .ErrorMessage = DefaultErrorMessage
205+ }
206+ if c .Redis == nil {
207+ c .Redis = NewDefaultRedisConfig ()
208+ }
209+ }
210+
121211type SafeConfig struct {
122212 mu sync.RWMutex
123213 config * Config
@@ -153,6 +243,15 @@ func (c *SafeConfig) GetConfig() *Config {
153243 return c .config
154244}
155245
246+ func (c * SafeConfig ) IsEnabled () bool {
247+ cfg := c .GetConfig ()
248+ if cfg == nil {
249+ logging .Error (errors .New ("safe config is nil" ), "found safe config is nil" )
250+ return false
251+ }
252+ return cfg .Enabled
253+ }
254+
156255func GetErrorCode () int32 {
157256 cfg := globalConfig .GetConfig ()
158257 if cfg == nil {
0 commit comments