forked from mirror/redis
Add client instantiation callback to allow more flexible configuration (#1281)
* Add NewClient to Ring options
This commit is contained in:
parent
1c4dd844c4
commit
9a49a4d91d
10
ring.go
10
ring.go
|
@ -56,6 +56,9 @@ type RingOptions struct {
|
||||||
// See https://arxiv.org/abs/1406.2294 for reference
|
// See https://arxiv.org/abs/1406.2294 for reference
|
||||||
HashReplicas int
|
HashReplicas int
|
||||||
|
|
||||||
|
// NewClient creates a shard client with provided name and options.
|
||||||
|
NewClient func(name string, opt *Options) *Client
|
||||||
|
|
||||||
// Optional hook that is called when a new shard is created.
|
// Optional hook that is called when a new shard is created.
|
||||||
OnNewShard func(*Client)
|
OnNewShard func(*Client)
|
||||||
|
|
||||||
|
@ -390,7 +393,12 @@ func NewRing(opt *RingOptions) *Ring {
|
||||||
func newRingShard(opt *RingOptions, name, addr string) *Client {
|
func newRingShard(opt *RingOptions, name, addr string) *Client {
|
||||||
clopt := opt.clientOptions(name)
|
clopt := opt.clientOptions(name)
|
||||||
clopt.Addr = addr
|
clopt.Addr = addr
|
||||||
shard := NewClient(clopt)
|
var shard *Client
|
||||||
|
if opt.NewClient != nil {
|
||||||
|
shard = opt.NewClient(name, clopt)
|
||||||
|
} else {
|
||||||
|
shard = NewClient(clopt)
|
||||||
|
}
|
||||||
if opt.OnNewShard != nil {
|
if opt.OnNewShard != nil {
|
||||||
opt.OnNewShard(shard)
|
opt.OnNewShard(shard)
|
||||||
}
|
}
|
||||||
|
|
14
ring_test.go
14
ring_test.go
|
@ -196,6 +196,20 @@ var _ = Describe("Redis Ring", func() {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Describe("new client callback", func() {
|
||||||
|
It("can be initialized with a new client callback", func() {
|
||||||
|
opts := redisRingOptions()
|
||||||
|
opts.NewClient = func(name string, opt *redis.Options) *redis.Client {
|
||||||
|
opt.Password = "password1"
|
||||||
|
return redis.NewClient(opt)
|
||||||
|
}
|
||||||
|
ring = redis.NewRing(opts)
|
||||||
|
|
||||||
|
err := ring.Ping().Err()
|
||||||
|
Expect(err).To(MatchError("ERR Client sent AUTH, but no password is set"))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
It("supports Process hook", func() {
|
It("supports Process hook", func() {
|
||||||
err := ring.Ping().Err()
|
err := ring.Ping().Err()
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
Loading…
Reference in New Issue