Skip to content

Commit 9a49a4d

Browse files
authored
Add client instantiation callback to allow more flexible configuration (#1281)
* Add NewClient to Ring options
1 parent 1c4dd84 commit 9a49a4d

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

ring.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ type RingOptions struct {
5656
// See https://arxiv.org/abs/1406.2294 for reference
5757
HashReplicas int
5858

59+
// NewClient creates a shard client with provided name and options.
60+
NewClient func(name string, opt *Options) *Client
61+
5962
// Optional hook that is called when a new shard is created.
6063
OnNewShard func(*Client)
6164

@@ -390,7 +393,12 @@ func NewRing(opt *RingOptions) *Ring {
390393
func newRingShard(opt *RingOptions, name, addr string) *Client {
391394
clopt := opt.clientOptions(name)
392395
clopt.Addr = addr
393-
shard := NewClient(clopt)
396+
var shard *Client
397+
if opt.NewClient != nil {
398+
shard = opt.NewClient(name, clopt)
399+
} else {
400+
shard = NewClient(clopt)
401+
}
394402
if opt.OnNewShard != nil {
395403
opt.OnNewShard(shard)
396404
}

ring_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,20 @@ var _ = Describe("Redis Ring", func() {
196196
})
197197
})
198198

199+
Describe("new client callback", func() {
200+
It("can be initialized with a new client callback", func() {
201+
opts := redisRingOptions()
202+
opts.NewClient = func(name string, opt *redis.Options) *redis.Client {
203+
opt.Password = "password1"
204+
return redis.NewClient(opt)
205+
}
206+
ring = redis.NewRing(opts)
207+
208+
err := ring.Ping().Err()
209+
Expect(err).To(MatchError("ERR Client sent AUTH, but no password is set"))
210+
})
211+
})
212+
199213
It("supports Process hook", func() {
200214
err := ring.Ping().Err()
201215
Expect(err).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)