add replaciton master auth support (#336)

This commit is contained in:
benjafire 2018-03-28 17:50:05 +08:00 committed by siddontang
parent 17ea9a433d
commit 36de957bf3
2 changed files with 28 additions and 4 deletions

View File

@ -83,6 +83,7 @@ type ReplicationConfig struct {
SyncLog int `toml:"sync_log"`
Compression bool `toml:"compression"`
UseMmap bool `toml:"use_mmap"`
MasterPassword string `toml:"master_password"`
}
type SnapshotConfig struct {

View File

@ -110,12 +110,35 @@ func (m *master) checkConn() error {
var err error
if m.conn == nil {
m.conn, err = goredis.Connect(m.addr)
} else {
if _, err = m.conn.Do("PING"); err != nil {
m.conn.Close()
m.conn = nil
if err != nil {
return err
}
}
// already connected and has master password
if len(m.app.cfg.Replication.MasterPassword) != 0 {
var res string
res, err = goredis.String(m.conn.Do("auth", m.app.cfg.Replication.MasterPassword))
if err != nil || strings.ToUpper(res) != "OK" {
m.conn.Close()
m.conn = nil
if err == nil {
err = fmt.Errorf("master auth fail , res=%s , password=%s", res, m.app.cfg.Replication.MasterPassword)
}
return err
}
}
if _, err = m.conn.Do("PING"); err != nil {
m.conn.Close()
m.conn = nil
}
return err
}