-
Notifications
You must be signed in to change notification settings - Fork 2
/
redis.go
151 lines (134 loc) · 3.76 KB
/
redis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package queue
import (
"crypto/tls"
"fmt"
"net"
"runtime"
"strings"
"time"
"github.com/hibiken/asynq"
)
// RedisConfig holds the configuration for the Redis connection.
type RedisConfig struct {
Network string
Addr string
Username string
Password string
DB int
DialTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
PoolSize int
TLSConfig *tls.Config
}
// validate checks if the RedisConfig's fields are correctly set.
func (c *RedisConfig) Validate() error {
if c.Addr == "" {
return ErrRedisEmptyAddress
}
if c.Network != "tcp" && c.Network != "unix" {
return fmt.Errorf("%w: %q", ErrRedisUnsupportedNetwork, c.Network)
}
if _, _, err := net.SplitHostPort(c.Addr); err != nil && c.Network == "tcp" {
return fmt.Errorf("%w: %v", ErrRedisInvalidAddress, err)
}
if c.TLSConfig == nil && strings.HasPrefix(c.Addr, "rediss://") {
return ErrRedisTLSRequired
}
return nil
}
// NewRedisConfig creates a new RedisConfig with the given options applied.
func NewRedisConfig(opts ...RedisOption) *RedisConfig {
config := DefaultRedisConfig()
for _, opt := range opts {
opt(config)
}
return config
}
// DefaultRedisConfig returns a RedisConfig initialized with default values.
func DefaultRedisConfig() *RedisConfig {
return &RedisConfig{
Network: "tcp",
Addr: "localhost:6379",
Username: "",
Password: "",
DB: 0,
DialTimeout: 5 * time.Second,
ReadTimeout: 3 * time.Second,
WriteTimeout: 3 * time.Second,
PoolSize: runtime.NumCPU() * 10,
TLSConfig: nil,
}
}
// ToAsynqRedisOpt converts RedisConfig to asynq.RedisClientOpt.
// This utility function bridges Redis configuration to asynq's expected format.
func (c *RedisConfig) ToAsynqRedisOpt() asynq.RedisClientOpt {
return asynq.RedisClientOpt{
Network: c.Network,
Addr: c.Addr,
Username: c.Username,
Password: c.Password,
DB: c.DB,
DialTimeout: c.DialTimeout,
ReadTimeout: c.ReadTimeout,
WriteTimeout: c.WriteTimeout,
PoolSize: c.PoolSize,
TLSConfig: c.TLSConfig,
}
}
// RedisOption defines a function signature for configuring RedisConfig.
type RedisOption func(*RedisConfig)
// WithRedisAddress sets the Redis server address.
func WithRedisAddress(addr string) RedisOption {
return func(c *RedisConfig) {
c.Addr = addr
}
}
// WithRedisUsername sets the username for Redis authentication.
func WithRedisUsername(username string) RedisOption {
return func(c *RedisConfig) {
c.Username = username
}
}
// WithRedisPassword sets the password for Redis authentication.
func WithRedisPassword(password string) RedisOption {
return func(c *RedisConfig) {
c.Password = password
}
}
// WithRedisDB sets the Redis database number.
func WithRedisDB(db int) RedisOption {
return func(c *RedisConfig) {
c.DB = db
}
}
// WithRedisTLSConfig sets the TLS configuration for the Redis connection.
func WithRedisTLSConfig(tlsConfig *tls.Config) RedisOption {
return func(c *RedisConfig) {
c.TLSConfig = tlsConfig
}
}
// WithRedisDialTimeout sets the timeout for connecting to Redis.
func WithRedisDialTimeout(timeout time.Duration) RedisOption {
return func(c *RedisConfig) {
c.DialTimeout = timeout
}
}
// WithRedisReadTimeout sets the timeout for reading from Redis.
func WithRedisReadTimeout(timeout time.Duration) RedisOption {
return func(c *RedisConfig) {
c.ReadTimeout = timeout
}
}
// WithRedisWriteTimeout sets the timeout for writing to Redis.
func WithRedisWriteTimeout(timeout time.Duration) RedisOption {
return func(c *RedisConfig) {
c.WriteTimeout = timeout
}
}
// WithRedisPoolSize sets the size of the connection pool for Redis.
func WithRedisPoolSize(size int) RedisOption {
return func(c *RedisConfig) {
c.PoolSize = size
}
}