diff --git a/config/config.go b/config/config.go index f8aa639..6d021d5 100644 --- a/config/config.go +++ b/config/config.go @@ -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 { diff --git a/server/replication.go b/server/replication.go index cfc4f26..911488c 100644 --- a/server/replication.go +++ b/server/replication.go @@ -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 }