github.com/jtzjtz/kit@v1.0.2/conn/redis_pool/options.go (about)

     1  package redis_pool
     2  
     3  import (
     4  	"errors"
     5  	"time"
     6  )
     7  
     8  var (
     9  	errInvalid = errors.New("invalid config")
    10  )
    11  
    12  //redis连接配置
    13  type Options struct {
    14  	Host               string
    15  	PassWord           string
    16  	Database           int
    17  	InitCap            int           // 最大空闲连接数,即会有这么多个连接提前等待着,但过了超时时间也会关闭。
    18  	MaxCap             int           // 最大连接数,即最多的tcp连接数,一般建议往大的配置,但不要超过操作系统文件句柄个数(centos下可以ulimit -n查看)
    19  	IsWait             bool          // 当超过最大连接数 是报错还是等待, true 等待 false 报错
    20  	IdleTimeout        time.Duration //空闲连接超时时间,但应该设置比redis服务器超时时间短。否则服务端超时了,客户端保持着连接也没用
    21  	DialConnectTimeout time.Duration
    22  	DialReadTimeout    time.Duration
    23  	DialWriteTimeout   time.Duration
    24  }
    25  
    26  //实例化一个默认配置
    27  func NewOptions() *Options {
    28  	o := &Options{}
    29  	o.InitCap = 10
    30  	o.MaxCap = 100
    31  	o.IsWait = true
    32  	o.Database = 0
    33  	o.IdleTimeout = 5 * time.Second
    34  	o.DialConnectTimeout = 5 * time.Second
    35  	o.DialReadTimeout = 5 * time.Second
    36  	o.DialWriteTimeout = 5 * time.Second
    37  	return o
    38  }
    39  
    40  //配置验证
    41  func (o *Options) validate() error {
    42  	if o.InitCap <= 0 ||
    43  		o.MaxCap <= 0 ||
    44  		o.InitCap > o.MaxCap ||
    45  		o.Host == "" ||
    46  		o.IdleTimeout == 0 ||
    47  		o.DialConnectTimeout == 0 ||
    48  		o.DialReadTimeout == 0 ||
    49  		o.DialWriteTimeout == 0 {
    50  		return errInvalid
    51  	}
    52  	return nil
    53  }