github.com/polarismesh/polaris@v1.17.8/common/redispool/config_option.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package redispool
    19  
    20  import (
    21  	"crypto/tls"
    22  	"time"
    23  )
    24  
    25  // Option functional options for Config
    26  type Option func(c *Config)
    27  
    28  // WithAddr set redis addr
    29  func WithAddr(addr string) Option {
    30  	return func(c *Config) {
    31  		c.KvAddr = addr
    32  	}
    33  }
    34  
    35  // WithPwd set pwd
    36  func WithPwd(pwd string) Option {
    37  	return func(c *Config) {
    38  		c.KvPasswd = pwd
    39  	}
    40  }
    41  
    42  // WithMinIdleConns set minIdleConns
    43  func WithMinIdleConns(minIdleConns int) Option {
    44  	return func(c *Config) {
    45  		c.MinIdleConns = minIdleConns
    46  	}
    47  }
    48  
    49  // WithIdleTimeout set idleTimeout
    50  func WithIdleTimeout(idleTimeout time.Duration) Option {
    51  	return func(c *Config) {
    52  		c.IdleTimeout = idleTimeout
    53  	}
    54  }
    55  
    56  // WithConnectTimeout set connection timeout
    57  func WithConnectTimeout(timeout time.Duration) Option {
    58  	return func(c *Config) {
    59  		c.ConnectTimeout = timeout
    60  	}
    61  }
    62  
    63  // WithConcurrency set concurrency size
    64  func WithConcurrency(size int) Option {
    65  	return func(c *Config) {
    66  		c.Concurrency = size
    67  	}
    68  }
    69  
    70  // WithCompatible set Compatible
    71  func WithCompatible(b bool) Option {
    72  	return func(c *Config) {
    73  		c.Compatible = b
    74  	}
    75  }
    76  
    77  // WithMaxRetry set pool MaxRetry
    78  func WithMaxRetry(maxRetry int) Option {
    79  	return func(c *Config) {
    80  		c.MaxRetry = maxRetry
    81  	}
    82  }
    83  
    84  // WithMinBatchCount set MinBatchCount
    85  func WithMinBatchCount(n int) Option {
    86  	return func(c *Config) {
    87  		c.MinBatchCount = n
    88  	}
    89  }
    90  
    91  // WithWaitTime set wait timeout
    92  func WithWaitTime(t time.Duration) Option {
    93  	return func(c *Config) {
    94  		c.WaitTime = t
    95  	}
    96  }
    97  
    98  // WithMaxRetries set maxRetries
    99  func WithMaxRetries(maxRetries int) Option {
   100  	return func(c *Config) {
   101  		c.MaxRetries = maxRetries
   102  	}
   103  }
   104  
   105  // WithDB set redis db
   106  func WithDB(num int) Option {
   107  	return func(c *Config) {
   108  		c.DB = num
   109  	}
   110  }
   111  
   112  // WithReadTimeout set readTimeout
   113  func WithReadTimeout(timeout time.Duration) Option {
   114  	return func(c *Config) {
   115  		c.ReadTimeout = timeout
   116  	}
   117  }
   118  
   119  // WithWriteTimeout set writeTimeout
   120  func WithWriteTimeout(timeout time.Duration) Option {
   121  	return func(c *Config) {
   122  		c.WriteTimeout = timeout
   123  	}
   124  }
   125  
   126  // WithPoolSize set pool size
   127  func WithPoolSize(poolSize int) Option {
   128  	return func(c *Config) {
   129  		c.PoolSize = poolSize
   130  	}
   131  }
   132  
   133  // WithPoolTimeout set pool timeout
   134  func WithPoolTimeout(poolTimeout time.Duration) Option {
   135  	return func(c *Config) {
   136  		c.PoolTimeout = poolTimeout
   137  	}
   138  }
   139  
   140  // WithMaxConnAge set MaxConnAge
   141  func WithMaxConnAge(maxConnAge time.Duration) Option {
   142  	return func(c *Config) {
   143  		c.MaxConnAge = maxConnAge
   144  	}
   145  }
   146  
   147  // WithUsername set username
   148  func WithUsername(username string) Option {
   149  	return func(c *Config) {
   150  		c.KvUser = username
   151  	}
   152  }
   153  
   154  // WithTLSConfig set TLSConfig
   155  func WithTLSConfig(tlsConfig *tls.Config) Option {
   156  	return func(c *Config) {
   157  		c.WithTLS = true
   158  		c.tlsConfig = tlsConfig
   159  	}
   160  }
   161  
   162  // WithCluster use redis cluster
   163  func WithCluster(cc ClusterConfig) Option {
   164  	return func(c *Config) {
   165  		c.DeployMode = redisCluster
   166  		c.StandaloneConfig.KvAddr = ""
   167  		c.ClusterConfig = cc
   168  	}
   169  }
   170  
   171  // WithSentinel use redis sentinel
   172  func WithSentinel(sc SentinelConfig) Option {
   173  	return func(c *Config) {
   174  		c.DeployMode = redisSentinel
   175  		c.StandaloneConfig.KvAddr = ""
   176  		c.SentinelConfig = sc
   177  	}
   178  }