// Package redis ... // // Description : redis ... // // Author : go_developer@163.com<白茶清欢> // // Date : 2022-06-15 11:52 package redis import ( "fmt" "time" "github.com/go-redis/redis/v8" "git.zhangdeman.cn/zhangdeman/logger" ) // FullConfig 完整配置 // // Author : go_developer@163.com<白茶清欢> // // Date : 12:23 2022/6/15 type FullConfig struct { Logger *logger.InputLogConfig `json:"logger" yaml:"logger"` // 日志配置 Master *Config `json:"master" yaml:"master"` // 主节点 Slave *Config `json:"slave" yaml:"slave"` // 从节点 } // Config redis的配置 // // Author : go_developer@163.com<白茶清欢> // // Date : 11:52 2022/6/15 type Config struct { Network string `json:"network" yaml:"network"` // 连接方式 tcp 或 unix , 默认 tcp Host string `yaml:"host" json:"host"` // 地址 Port int `yaml:"port" json:"port"` // 端口 // Dialer creates new network connection and has priority over // Network and Addr options. // Dialer func(ctx context.Context, network, addr string) (net.Conn, error) // Hook that is called when new connection is established. // OnConnect func(ctx context.Context, cn *redis.Conn) error // Use the specified Username to authenticate the current connection // with one of the connections defined in the ACL list when connecting // to a Redis 6.0 instance, or greater, that is using the Redis ACL system. Username string `json:"username" yaml:"username"` // 账号 // Optional password. Must match the password specified in the // requirepass server configuration option (if connecting to a Redis 5.0 instance, or lower), // or the User Password when connecting to a Redis 6.0 instance, or greater, // that is using the Redis ACL system. Password string `json:"password" yaml:"password"` // 密码 // Database to be selected after connecting to the server. DB int `json:"db" yaml:"db"` // 选中的数据库 // Maximum number of retries before giving up. // Default is 3 retries; -1 (not 0) disables retries. MaxRetries int `json:"max_retries" yaml:"max_retries"` // 最大重试次数, 默认 3 , -1 为不重试 // Minimum backoff between each retry. // Default is 8 milliseconds; -1 disables backoff. MinRetryBackoff int64 `json:"min_retry_backoff" yaml:"min_retry_backoff"` // 最小重试的时间间隔, 默认 8ms, -1禁用 // Maximum backoff between each retry. // Default is 512 milliseconds; -1 disables backoff. MaxRetryBackoff int `json:"max_retry_backoff" yaml:"max_retry_backoff"` // 最大重试的时间间隔, 默认 512ms, -1禁用 // Dial timeout for establishing new connections. // Default is 5 seconds. DialTimeout int64 `json:"dial_timeout" yaml:"dial_timeout"` // 建立新连接的超时时间, 默认 5s // Timeout for socket reads. If reached, commands will fail // with a timeout instead of blocking. Use value -1 for no timeout and 0 for default. // Default is 3 seconds. ReadTimeout int64 `json:"read_timeout" yaml:"read_timeout"` // 读取超时时间,默认 3s , -1 不设置超时 // Timeout for socket writes. If reached, commands will fail // with a timeout instead of blocking. // Default is ReadTimeout. WriteTimeout int64 `json:"write_timeout" yaml:"write_timeout"` // 写入超时时间, 默认值和 ReadTimeout 配置一致 // Type of connection pool. // true for FIFO pool, false for LIFO pool. // Note that fifo has higher overhead compared to lifo. PoolFIFO bool `json:"pool_fifo" yaml:"pool_fifo"` // 连接吃采用 FIFO // Maximum number of socket connections. // Default is 10 connections per every available CPU as reported by runtime.GOMAXPROCS. PoolSize int `json:"pool_size" yaml:"pool_size"` // 连接池大小, 默认 runtime.GOMAXPROCS * 10 // Minimum number of idle connections which is useful when establishing // new connection is slow. MinIdleConns int `json:"min_idle_conns" yaml:"min_idle_conns"` // 最小的 空闲 连接数数量 // Connection age at which client retires (closes) the connection. // Default is to not close aged connections. MaxConnAge int64 `json:"max_conn_age" yaml:"max_conn_age"` // 连接最大的存活时常 // Amount of time client waits for connection if all connections // are busy before returning an error. // Default is ReadTimeout + 1 second. PoolTimeout int64 `json:"pool_timeout" yaml:"pool_timeout"` // 连接池无可用连接, 等待的时常, 默认值 ReadTimeout + 1 // Amount of time after which client closes idle connections. // Should be less than server's timeout. // Default is 5 minutes. -1 disables idle timeout check. IdleTimeout int64 `json:"idle_timeout" yaml:"idle_timeout"` // 空闲链接的超时时间, 默认 5 分钟 -1 金童 // Frequency of idle checks made by idle connections reaper. // Default is 1 minute. -1 disables idle connections reaper, // but idle connections are still discarded by the client // if IdleTimeout is set. IdleCheckFrequency int64 `json:"idle_check_frequency" yaml:"idle_check_frequency"` // 空闲连接检查频率, 默认 1 分钟, -1 禁用, 注意 : 即使禁用,超过 IdleTimeout 时常的连接也不可被读取到 } // Config2Options 将 自定义的配置文件转为 redis 包需要的配置格式 // // Author : go_developer@163.com<白茶清欢> // // Date : 17:37 2022/6/15 func Config2Options(cfg *Config) *redis.Options { option := &redis.Options{ Network: cfg.Network, Addr: fmt.Sprintf("%v:%v", cfg.Host, cfg.Port), Username: cfg.Username, Password: cfg.Password, DB: cfg.DB, MaxRetries: cfg.MaxRetries, MinRetryBackoff: time.Duration(cfg.MinRetryBackoff) * time.Millisecond, MaxRetryBackoff: time.Duration(cfg.MaxRetryBackoff) * time.Millisecond, DialTimeout: time.Duration(cfg.DialTimeout) * time.Millisecond, ReadTimeout: time.Duration(cfg.ReadTimeout) * time.Millisecond, WriteTimeout: time.Duration(cfg.WriteTimeout) * time.Millisecond, PoolFIFO: cfg.PoolFIFO, PoolSize: cfg.PoolSize, MinIdleConns: cfg.MinIdleConns, MaxConnAge: time.Duration(cfg.MaxConnAge) * time.Millisecond, PoolTimeout: time.Duration(cfg.PoolTimeout) * time.Millisecond, IdleTimeout: time.Duration(cfg.IdleTimeout) * time.Millisecond, IdleCheckFrequency: time.Duration(cfg.IdleCheckFrequency) * time.Millisecond, TLSConfig: nil, Limiter: nil, } if len(option.Network) == 0 { option.Network = NetworkTypeTCP } return option } const ( // NetworkTypeTCP ... NetworkTypeTCP = "tcp" // NetworkTypeUnix ... NetworkTypeUnix = "unix" )