Merge pull request #1087 from mbenford/individual-passwords-ring

Add support for individual passwords for ring shards
This commit is contained in:
Vladimir Mihailenco 2019-07-18 10:34:38 +03:00 committed by GitHub
commit 94359d94c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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. // Map of name => host:port addresses of ring shards.
Addrs map[string]string 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. // Frequency of PING commands sent to check shards availability.
// Shard is considered down after 3 subsequent failed checks. // Shard is considered down after 3 subsequent failed checks.
HeartbeatFrequency time.Duration 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{ return &Options{
OnConnect: opt.OnConnect, OnConnect: opt.OnConnect,
DB: opt.DB, DB: opt.DB,
Password: opt.Password, Password: opt.getPassword(shard),
DialTimeout: opt.DialTimeout, DialTimeout: opt.DialTimeout,
ReadTimeout: opt.ReadTimeout, 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 { type ringShard struct {
@ -365,7 +376,7 @@ func NewRing(opt *RingOptions) *Ring {
ring.cmdsInfoCache = newCmdsInfoCache(ring.cmdsInfo) ring.cmdsInfoCache = newCmdsInfoCache(ring.cmdsInfo)
for name, addr := range opt.Addrs { for name, addr := range opt.Addrs {
clopt := opt.clientOptions() clopt := opt.clientOptions(name)
clopt.Addr = addr clopt.Addr = addr
ring.shards.Add(name, NewClient(clopt)) ring.shards.Add(name, NewClient(clopt))
} }

View File

@ -172,6 +172,29 @@ var _ = Describe("Redis Ring", func() {
Expect(ringShard2.Info().Val()).To(ContainSubstring("keys=100")) Expect(ringShard2.Info().Val()).To(ContainSubstring("keys=100"))
}) })
}) })
Describe("shard passwords", func() {
It("can be initialized with a single password, used for all shards", func() {
opts := redisRingOptions()
opts.Password = "password"
ring = redis.NewRing(opts)
err := ring.Ping().Err()
Expect(err).To(MatchError("ERR Client sent AUTH, but no password is set"))
})
It("can be initialized with a passwords map, one for each shard", func() {
opts := redisRingOptions()
opts.Passwords = map[string]string{
"ringShardOne": "password1",
"ringShardTwo": "password2",
}
ring = redis.NewRing(opts)
err := ring.Ping().Err()
Expect(err).To(MatchError("ERR Client sent AUTH, but no password is set"))
})
})
}) })
var _ = Describe("empty Redis Ring", func() { var _ = Describe("empty Redis Ring", func() {