1
0
mirror of https://github.com/redis/go-redis.git synced 2025-07-28 06:42:00 +03:00

Add support for individual passwords for ring shards

This commit is contained in:
Michael Benford
2019-07-09 16:00:10 -03:00
parent 850dea52a1
commit 9169633580
2 changed files with 37 additions and 3 deletions

17
ring.go
View File

@ -27,6 +27,10 @@ type RingOptions struct {
// Map of name => host:port addresses of ring shards.
Addrs map[string]string
// Map of name => password of ring shards, to allow different shards to have
// different passwords. It will be ignored if the Password field is set.
Passwords map[string]string
// Frequency of PING commands sent to check shards availability.
// Shard is considered down after 3 subsequent failed checks.
HeartbeatFrequency time.Duration
@ -98,12 +102,12 @@ func (opt *RingOptions) init() {
}
}
func (opt *RingOptions) clientOptions() *Options {
func (opt *RingOptions) clientOptions(shard string) *Options {
return &Options{
OnConnect: opt.OnConnect,
DB: opt.DB,
Password: opt.Password,
Password: opt.getPassword(shard),
DialTimeout: opt.DialTimeout,
ReadTimeout: opt.ReadTimeout,
@ -118,6 +122,13 @@ func (opt *RingOptions) clientOptions() *Options {
}
}
func (opt *RingOptions) getPassword(shard string) string {
if opt.Password == "" {
return opt.Passwords[shard]
}
return opt.Password
}
//------------------------------------------------------------------------------
type ringShard struct {
@ -365,7 +376,7 @@ func NewRing(opt *RingOptions) *Ring {
ring.cmdsInfoCache = newCmdsInfoCache(ring.cmdsInfo)
for name, addr := range opt.Addrs {
clopt := opt.clientOptions()
clopt := opt.clientOptions(name)
clopt.Addr = addr
ring.shards.Add(name, NewClient(clopt))
}