/
options.go
155 lines (134 loc) · 4.31 KB
/
options.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
152
153
154
155
package redis
import (
"crypto/tls"
"time"
"github.com/go-redis/redis/v8"
)
// Options options to initiate your client
type Options struct {
Type ClientType
// Host address with port number
// For normal client will only used the first value
Hosts []string
// The network type, either tcp or unix.
// Default is tcp.
// Only for normal client
Network string
// Database to be selected after connecting to the server.
Database int
// Automatically adds a prefix to all keys
KeyPrefix string
// The maximum number of retries before giving up. Command is retried
// on network errors and MOVED/ASK redirects.
// Default is 16.
// In normal client this is the MaxRetries option
MaxRedirects int
// Enables read queries for a connection to a Redis Cluster slave node.
ReadOnly bool
// Enables routing read-only queries to the closest master or slave node.
// If set will change this client to read-only mode
RouteByLatency bool
// Following options are copied from Options struct.
Password string
// Dial timeout for establishing new connections.
// Default is 5 seconds.
DialTimeout time.Duration
// Timeout for socket reads. If reached, commands will fail
// with a timeout instead of blocking.
// Default is 3 seconds.
ReadTimeout time.Duration
// Timeout for socket writes. If reached, commands will fail
// with a timeout instead of blocking.
// Default is 3 seconds.
WriteTimeout time.Duration
// PoolSize applies per cluster node and not for the whole cluster.
// Maximum number of socket connections.
// Default is 10 connections.
PoolSize int
// Amount of time client waits for connection if all connections
// are busy before returning an error.
// Default is ReadTimeout + 1 second.
PoolTimeout time.Duration
// Amount of time after which client closes idle connections.
// Should be less than server's timeout.
// Default is to not close idle connections.
IdleTimeout time.Duration
// Frequency of idle checks.
// Default is 1 minute.
// When minus value is set, then idle check is disabled.
IdleCheckFrequency time.Duration
// TLS Config to use. When set TLS will be negotiated.
// Only for normal client
TLSConfig *tls.Config
}
// GetClusterConfig translates current configuration into a *redis.ClusterOptions
func (o Options) GetClusterConfig() *redis.ClusterOptions {
opts := &redis.ClusterOptions{
Addrs: o.Hosts,
ReadOnly: o.ReadOnly,
RouteByLatency: o.RouteByLatency,
Password: o.Password,
DialTimeout: o.DialTimeout,
ReadTimeout: o.ReadTimeout,
WriteTimeout: o.WriteTimeout,
PoolSize: o.PoolSize,
PoolTimeout: o.PoolTimeout,
IdleTimeout: o.IdleTimeout,
IdleCheckFrequency: o.IdleCheckFrequency,
}
if o.MaxRedirects > 0 {
opts.MaxRedirects = o.MaxRedirects
}
return opts
}
// GetNormalConfig translates current configuration into a *redis.Options struct
func (o Options) GetNormalConfig() *redis.Options {
opts := &redis.Options{
Addr: o.Hosts[0],
Password: o.Password,
DB: o.Database,
MaxRetries: o.MaxRedirects,
DialTimeout: o.DialTimeout,
ReadTimeout: o.ReadTimeout,
WriteTimeout: o.WriteTimeout,
PoolSize: o.PoolSize,
PoolTimeout: o.PoolTimeout,
IdleTimeout: o.IdleTimeout,
IdleCheckFrequency: o.IdleCheckFrequency,
TLSConfig: o.TLSConfig,
}
return opts
}
// ClientType type to define a redis client connector
type ClientType string
const (
// ClientNormal for standard instance client
ClientNormal ClientType = "normal"
// ClientCluster for official redis cluster
ClientCluster ClientType = "cluster"
)
// Client Reader and Writer
type RWType string
const (
// OnlyRead serves as a search suffix for configuration parameters
OnlyRead RWType = "READER"
// OnlyWrite serves as a search suffix for configuration parameters
OnlyWrite RWType = "WRITER"
// ReadAndWrite serves as a search suffix for configuration parameters
ReadAndWrite RWType = ""
)
// IsReadOnly will return Is it read-only
func (rw *RWType) IsReadOnly() bool {
if *rw == OnlyRead {
return true
} else {
return false
}
}
// FmtSuffix get fmtstring of key+ "_" + suffix
func (rw *RWType) FmtSuffix(key string) string {
if *rw == ReadAndWrite {
return key
}
return key + "_" + string(*rw)
}