github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/engines/redis/settings.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  	"fmt"
    18  	"strconv"
    19  	"time"
    20  
    21  	"github.com/dapr/kit/config"
    22  )
    23  
    24  type Settings struct {
    25  	// The Redis host
    26  	Host string `mapstructure:"redisHost"`
    27  	// The Redis password
    28  	Password string `mapstructure:"redisPassword"`
    29  	// The Redis username
    30  	Username string `mapstructure:"redisUsername"`
    31  	// Database to be selected after connecting to the server.
    32  	DB int `mapstructure:"redisDB"`
    33  	// The redis type node or cluster
    34  	RedisType string `mapstructure:"redisType"`
    35  	// Maximum number of retries before giving up.
    36  	// A value of -1 (not 0) disables retries
    37  	// Default is 3 retries
    38  	RedisMaxRetries int `mapstructure:"redisMaxRetries"`
    39  	// Minimum backoff between each retry.
    40  	// Default is 8 milliseconds; -1 disables backoff.
    41  	RedisMinRetryInterval Duration `mapstructure:"redisMinRetryInterval"`
    42  	// Maximum backoff between each retry.
    43  	// Default is 512 milliseconds; -1 disables backoff.
    44  	RedisMaxRetryInterval Duration `mapstructure:"redisMaxRetryInterval"`
    45  	// Dial timeout for establishing new connections.
    46  	DialTimeout Duration `mapstructure:"dialTimeout"`
    47  	// Timeout for socket reads. If reached, commands will fail
    48  	// with a timeout instead of blocking. Use value -1 for no timeout and 0 for default.
    49  	ReadTimeout Duration `mapstructure:"readTimeout"`
    50  	// Timeout for socket writes. If reached, commands will fail
    51  	WriteTimeout Duration `mapstructure:"writeTimeout"`
    52  	// Maximum number of socket connections.
    53  	PoolSize int `mapstructure:"poolSize"`
    54  	// Minimum number of idle connections which is useful when establishing
    55  	// new connection is slow.
    56  	MinIdleConns int `mapstructure:"minIdleConns"`
    57  	// Connection age at which client retires (closes) the connection.
    58  	// Default is to not close aged connections.
    59  	MaxConnAge Duration `mapstructure:"maxConnAge"`
    60  	// Amount of time client waits for connection if all connections
    61  	// are busy before returning an error.
    62  	// Default is ReadTimeout + 1 second.
    63  	PoolTimeout Duration `mapstructure:"poolTimeout"`
    64  	// Amount of time after which client closes idle connections.
    65  	// Should be less than server's timeout.
    66  	// Default is 5 minutes. -1 disables idle timeout check.
    67  	IdleTimeout Duration `mapstructure:"idleTimeout"`
    68  	// Frequency of idle checks made by idle connections reaper.
    69  	// Default is 1 minute. -1 disables idle connections reaper,
    70  	// but idle connections are still discarded by the client
    71  	// if IdleTimeout is set.
    72  	IdleCheckFrequency Duration `mapstructure:"idleCheckFrequency"`
    73  	// The master name
    74  	SentinelMasterName string `mapstructure:"sentinelMasterName"`
    75  	// Use Redis Sentinel for automatic failover.
    76  	Failover bool `mapstructure:"failover"`
    77  
    78  	// A flag to enables TLS by setting InsecureSkipVerify to true
    79  	EnableTLS bool `mapstructure:"enableTLS"`
    80  }
    81  
    82  func (s *Settings) Decode(in interface{}) error {
    83  	if err := config.Decode(in, s); err != nil {
    84  		return fmt.Errorf("decode failed. %w", err)
    85  	}
    86  
    87  	return nil
    88  }
    89  
    90  type Duration time.Duration
    91  
    92  func (r *Duration) DecodeString(value string) error {
    93  	if val, err := strconv.Atoi(value); err == nil {
    94  		if val < 0 {
    95  			*r = Duration(val)
    96  
    97  			return nil
    98  		}
    99  		*r = Duration(time.Duration(val) * time.Millisecond)
   100  
   101  		return nil
   102  	}
   103  
   104  	// Convert it by parsing
   105  	d, err := time.ParseDuration(value)
   106  	if err != nil {
   107  		return err
   108  	}
   109  
   110  	*r = Duration(d)
   111  
   112  	return nil
   113  }