@@ -24,7 +24,7 @@ const (
24
24
var errConnLost = errors .New ("redis: connection lost while awaiting response" )
25
25
26
26
// ClientConfig defines a Client setup.
27
- type ClientConfig [ Key , Value String ] struct {
27
+ type ClientConfig struct {
28
28
// The host defaults to localhost, and the port defaults to 6379.
29
29
// Thus, the empty string defaults to "localhost:6379". Use an
30
30
// absolute file path (e.g. "/var/run/redis.sock") for Unix
@@ -51,21 +51,13 @@ type ClientConfig[Key, Value String] struct {
51
51
DB int64
52
52
}
53
53
54
- // NewClient launches a managed connection to a node (address).
55
- func (c * ClientConfig [Key , Value ]) NewClient () * Client [Key , Value ] {
56
- return newClient (* c )
57
- }
58
-
59
54
// Client manages a connection to a Redis node until Close. Broken connection
60
55
// states cause automated reconnects.
61
56
//
62
57
// Multiple goroutines may invoke methods on a Client simultaneously. Command
63
58
// invocation applies <https://redis.io/topics/pipelining> on concurrency.
64
59
type Client [Key , Value String ] struct {
65
- // Normalized node address in use. This field is read-only.
66
- Addr string
67
-
68
- config ClientConfig [Key , Value ]
60
+ ClientConfig // read-only attributes
69
61
70
62
noCopy noCopy
71
63
@@ -85,29 +77,18 @@ type Client[Key, Value String] struct {
85
77
readTerm chan struct {}
86
78
}
87
79
88
- // NewClient launches a managed connection to a node (address).
89
- // The host defaults to localhost, and the port defaults to 6379.
90
- // Thus, the empty string defaults to "localhost:6379". Use an
91
- // absolute file path (e.g. "/var/run/redis.sock") for Unix
92
- // domain sockets.
93
- //
94
- // A command time-out limits execution duration when nonzero. Expiry causes a
95
- // reconnect (to prevent stale connections) and a net.Error with Timeout() true.
96
- //
97
- // The dial time-out limits the duration for network connection establishment.
98
- // Expiry causes an abort + retry. Zero defaults to one second. Any command
99
- // submission blocks on the first attempt. When connection establishment fails,
100
- // then command submission receives the error of the last attempt, until the
101
- // connection restores.
102
- func NewClient [Key , Value String ](addr string , commandTimeout , dialTimeout time.Duration ) * Client [Key , Value ] {
103
- return newClient (ClientConfig [Key , Value ]{
80
+ // NewDefaultClient launches a managed connection to a node (address).
81
+ // Both CommandTimeout and DialTimeout are set to one second.
82
+ func NewDefaultClient [Key , Value String ](addr string ) * Client [Key , Value ] {
83
+ return NewClient [Key , Value ](ClientConfig {
104
84
Addr : addr ,
105
- CommandTimeout : commandTimeout ,
106
- DialTimeout : dialTimeout ,
85
+ CommandTimeout : time . Second ,
86
+ DialTimeout : time . Second ,
107
87
})
108
88
}
109
89
110
- func newClient [Key , Value String ](config ClientConfig [Key , Value ]) * Client [Key , Value ] {
90
+ // NewClient launches a managed connection to a node (address).
91
+ func NewClient [Key , Value String ](config ClientConfig ) * Client [Key , Value ] {
111
92
config .Addr = normalizeAddr (config .Addr )
112
93
if config .DialTimeout == 0 {
113
94
config .DialTimeout = time .Second
@@ -119,8 +100,7 @@ func newClient[Key, Value String](config ClientConfig[Key, Value]) *Client[Key,
119
100
}
120
101
121
102
c := & Client [Key , Value ]{
122
- Addr : config .Addr , // decouple
123
- config : config ,
103
+ ClientConfig : config ,
124
104
125
105
connSem : make (chan * redisConn , 1 ),
126
106
readQueue : make (chan chan <- * bufio.Reader , queueSize ),
@@ -172,7 +152,7 @@ func (c *Client[Key, Value]) Close() error {
172
152
func (c * Client [Key , Value ]) connectOrClosed () {
173
153
var retryDelay time.Duration
174
154
for {
175
- conn , reader , err := c .config . connect (conservativeMSS )
155
+ conn , reader , err := c .connect (conservativeMSS )
176
156
if err != nil {
177
157
retry := time .NewTimer (retryDelay )
178
158
@@ -237,8 +217,8 @@ func (c *Client[Key, Value]) exchange(req *request) (*bufio.Reader, error) {
237
217
238
218
// apply time-out if set
239
219
var deadline time.Time
240
- if c .config . CommandTimeout != 0 {
241
- deadline = time .Now ().Add (c .config . CommandTimeout )
220
+ if c .CommandTimeout != 0 {
221
+ deadline = time .Now ().Add (c .CommandTimeout )
242
222
conn .SetWriteDeadline (deadline )
243
223
}
244
224
@@ -430,7 +410,7 @@ func (c *Client[Key, Value]) dropConnFromRead() {
430
410
}
431
411
}
432
412
433
- func (c * ClientConfig [ Key , Value ] ) connect (readBufferSize int ) (net.Conn , * bufio.Reader , error ) {
413
+ func (c * ClientConfig ) connect (readBufferSize int ) (net.Conn , * bufio.Reader , error ) {
434
414
network := "tcp"
435
415
if isUnixAddr (c .Addr ) {
436
416
network = "unix"
0 commit comments