Skip to content

Commit 03f0590

Browse files
authored
Merge pull request #840 from go-redis/fix/universal-options
Add missing options to UniversalOptions
2 parents 8ebf0b7 + 0d13296 commit 03f0590

File tree

4 files changed

+137
-39
lines changed

4 files changed

+137
-39
lines changed

cluster.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ type ClusterOptions struct {
5454

5555
OnConnect func(*Conn) error
5656

57+
Password string
58+
5759
MaxRetries int
5860
MinRetryBackoff time.Duration
5961
MaxRetryBackoff time.Duration
60-
Password string
6162

6263
DialTimeout time.Duration
6364
ReadTimeout time.Duration

internal_test.go

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package redis
2+
3+
import (
4+
. "github.com/onsi/ginkgo"
5+
. "github.com/onsi/gomega"
6+
)
7+
8+
var _ = Describe("newClusterState", func() {
9+
var state *clusterState
10+
11+
createClusterState := func(slots []ClusterSlot) *clusterState {
12+
nodes := newClusterNodes(&ClusterOptions{})
13+
state, err := newClusterState(nodes, slots, "10.10.10.10:1234")
14+
Expect(err).NotTo(HaveOccurred())
15+
return state
16+
}
17+
18+
Describe("sorting", func() {
19+
BeforeEach(func() {
20+
state = createClusterState([]ClusterSlot{{
21+
Start: 1000,
22+
End: 1999,
23+
}, {
24+
Start: 0,
25+
End: 999,
26+
}, {
27+
Start: 2000,
28+
End: 2999,
29+
}})
30+
})
31+
32+
It("sorts slots", func() {
33+
Expect(state.slots).To(Equal([]*clusterSlot{
34+
{start: 0, end: 999, nodes: nil},
35+
{start: 1000, end: 1999, nodes: nil},
36+
{start: 2000, end: 2999, nodes: nil},
37+
}))
38+
})
39+
})
40+
41+
Describe("loopback", func() {
42+
BeforeEach(func() {
43+
state = createClusterState([]ClusterSlot{{
44+
Nodes: []ClusterNode{{Addr: "127.0.0.1:7001"}},
45+
}, {
46+
Nodes: []ClusterNode{{Addr: "127.0.0.1:7002"}},
47+
}, {
48+
Nodes: []ClusterNode{{Addr: "1.2.3.4:1234"}},
49+
}, {
50+
Nodes: []ClusterNode{{Addr: ":1234"}},
51+
}})
52+
})
53+
54+
It("replaces loopback hosts in addresses", func() {
55+
slotAddr := func(slot *clusterSlot) string {
56+
return slot.nodes[0].Client.Options().Addr
57+
}
58+
59+
Expect(slotAddr(state.slots[0])).To(Equal("10.10.10.10:7001"))
60+
Expect(slotAddr(state.slots[1])).To(Equal("10.10.10.10:7002"))
61+
Expect(slotAddr(state.slots[2])).To(Equal("1.2.3.4:1234"))
62+
Expect(slotAddr(state.slots[3])).To(Equal(":1234"))
63+
})
64+
})
65+
})

sentinel.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,17 @@ type FailoverOptions struct {
2929
Password string
3030
DB int
3131

32-
MaxRetries int
32+
MaxRetries int
33+
MinRetryBackoff time.Duration
34+
MaxRetryBackoff time.Duration
3335

3436
DialTimeout time.Duration
3537
ReadTimeout time.Duration
3638
WriteTimeout time.Duration
3739

3840
PoolSize int
41+
MinIdleConns int
42+
MaxConnAge time.Duration
3943
PoolTimeout time.Duration
4044
IdleTimeout time.Duration
4145
IdleCheckFrequency time.Duration

universal.go

+65-37
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,38 @@ type UniversalOptions struct {
1212
// of cluster/sentinel nodes.
1313
Addrs []string
1414

15-
// The sentinel master name.
16-
// Only failover clients.
17-
MasterName string
18-
1915
// Database to be selected after connecting to the server.
2016
// Only single-node and failover clients.
2117
DB int
2218

23-
// Only cluster clients.
24-
25-
// Enables read only queries on slave nodes.
26-
ReadOnly bool
27-
28-
MaxRedirects int
29-
RouteByLatency bool
30-
31-
// Common options
19+
// Common options.
3220

3321
OnConnect func(*Conn) error
34-
MaxRetries int
3522
Password string
23+
MaxRetries int
24+
MinRetryBackoff time.Duration
25+
MaxRetryBackoff time.Duration
3626
DialTimeout time.Duration
3727
ReadTimeout time.Duration
3828
WriteTimeout time.Duration
3929
PoolSize int
30+
MinIdleConns int
31+
MaxConnAge time.Duration
4032
PoolTimeout time.Duration
4133
IdleTimeout time.Duration
4234
IdleCheckFrequency time.Duration
4335
TLSConfig *tls.Config
36+
37+
// Only cluster clients.
38+
39+
MaxRedirects int
40+
ReadOnly bool
41+
RouteByLatency bool
42+
RouteRandomly bool
43+
44+
// The sentinel master name.
45+
// Only failover clients.
46+
MasterName string
4447
}
4548

4649
func (o *UniversalOptions) cluster() *ClusterOptions {
@@ -49,22 +52,31 @@ func (o *UniversalOptions) cluster() *ClusterOptions {
4952
}
5053

5154
return &ClusterOptions{
52-
Addrs: o.Addrs,
55+
Addrs: o.Addrs,
56+
OnConnect: o.OnConnect,
57+
58+
Password: o.Password,
59+
5360
MaxRedirects: o.MaxRedirects,
54-
RouteByLatency: o.RouteByLatency,
5561
ReadOnly: o.ReadOnly,
62+
RouteByLatency: o.RouteByLatency,
63+
RouteRandomly: o.RouteRandomly,
64+
65+
MaxRetries: o.MaxRetries,
66+
MinRetryBackoff: o.MinRetryBackoff,
67+
MaxRetryBackoff: o.MaxRetryBackoff,
5668

57-
OnConnect: o.OnConnect,
58-
MaxRetries: o.MaxRetries,
59-
Password: o.Password,
6069
DialTimeout: o.DialTimeout,
6170
ReadTimeout: o.ReadTimeout,
6271
WriteTimeout: o.WriteTimeout,
6372
PoolSize: o.PoolSize,
73+
MinIdleConns: o.MinIdleConns,
74+
MaxConnAge: o.MaxConnAge,
6475
PoolTimeout: o.PoolTimeout,
6576
IdleTimeout: o.IdleTimeout,
6677
IdleCheckFrequency: o.IdleCheckFrequency,
67-
TLSConfig: o.TLSConfig,
78+
79+
TLSConfig: o.TLSConfig,
6880
}
6981
}
7082

@@ -76,19 +88,27 @@ func (o *UniversalOptions) failover() *FailoverOptions {
7688
return &FailoverOptions{
7789
SentinelAddrs: o.Addrs,
7890
MasterName: o.MasterName,
79-
DB: o.DB,
91+
OnConnect: o.OnConnect,
92+
93+
DB: o.DB,
94+
Password: o.Password,
95+
96+
MaxRetries: o.MaxRetries,
97+
MinRetryBackoff: o.MinRetryBackoff,
98+
MaxRetryBackoff: o.MaxRetryBackoff,
99+
100+
DialTimeout: o.DialTimeout,
101+
ReadTimeout: o.ReadTimeout,
102+
WriteTimeout: o.WriteTimeout,
80103

81-
OnConnect: o.OnConnect,
82-
MaxRetries: o.MaxRetries,
83-
Password: o.Password,
84-
DialTimeout: o.DialTimeout,
85-
ReadTimeout: o.ReadTimeout,
86-
WriteTimeout: o.WriteTimeout,
87104
PoolSize: o.PoolSize,
105+
MinIdleConns: o.MinIdleConns,
106+
MaxConnAge: o.MaxConnAge,
88107
PoolTimeout: o.PoolTimeout,
89108
IdleTimeout: o.IdleTimeout,
90109
IdleCheckFrequency: o.IdleCheckFrequency,
91-
TLSConfig: o.TLSConfig,
110+
111+
TLSConfig: o.TLSConfig,
92112
}
93113
}
94114

@@ -99,20 +119,28 @@ func (o *UniversalOptions) simple() *Options {
99119
}
100120

101121
return &Options{
102-
Addr: addr,
103-
DB: o.DB,
122+
Addr: addr,
123+
OnConnect: o.OnConnect,
124+
125+
DB: o.DB,
126+
Password: o.Password,
127+
128+
MaxRetries: o.MaxRetries,
129+
MinRetryBackoff: o.MinRetryBackoff,
130+
MaxRetryBackoff: o.MaxRetryBackoff,
131+
132+
DialTimeout: o.DialTimeout,
133+
ReadTimeout: o.ReadTimeout,
134+
WriteTimeout: o.WriteTimeout,
104135

105-
OnConnect: o.OnConnect,
106-
MaxRetries: o.MaxRetries,
107-
Password: o.Password,
108-
DialTimeout: o.DialTimeout,
109-
ReadTimeout: o.ReadTimeout,
110-
WriteTimeout: o.WriteTimeout,
111136
PoolSize: o.PoolSize,
137+
MinIdleConns: o.MinIdleConns,
138+
MaxConnAge: o.MaxConnAge,
112139
PoolTimeout: o.PoolTimeout,
113140
IdleTimeout: o.IdleTimeout,
114141
IdleCheckFrequency: o.IdleCheckFrequency,
115-
TLSConfig: o.TLSConfig,
142+
143+
TLSConfig: o.TLSConfig,
116144
}
117145
}
118146

0 commit comments

Comments
 (0)