forked from mirror/redis
Merge pull request #782 from go-redis/fix/sentinel-client
Export SentinelClient
This commit is contained in:
commit
83fb42932f
29
sentinel.go
29
sentinel.go
|
@ -99,25 +99,23 @@ func NewFailoverClient(failoverOpt *FailoverOptions) *Client {
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
type sentinelClient struct {
|
type SentinelClient struct {
|
||||||
cmdable
|
|
||||||
baseClient
|
baseClient
|
||||||
}
|
}
|
||||||
|
|
||||||
func newSentinel(opt *Options) *sentinelClient {
|
func NewSentinelClient(opt *Options) *SentinelClient {
|
||||||
opt.init()
|
opt.init()
|
||||||
c := sentinelClient{
|
c := &SentinelClient{
|
||||||
baseClient: baseClient{
|
baseClient: baseClient{
|
||||||
opt: opt,
|
opt: opt,
|
||||||
connPool: newConnPool(opt),
|
connPool: newConnPool(opt),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
c.baseClient.init()
|
c.baseClient.init()
|
||||||
c.cmdable.setProcessor(c.Process)
|
return c
|
||||||
return &c
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *sentinelClient) PubSub() *PubSub {
|
func (c *SentinelClient) PubSub() *PubSub {
|
||||||
return &PubSub{
|
return &PubSub{
|
||||||
opt: c.opt,
|
opt: c.opt,
|
||||||
|
|
||||||
|
@ -128,13 +126,13 @@ func (c *sentinelClient) PubSub() *PubSub {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *sentinelClient) GetMasterAddrByName(name string) *StringSliceCmd {
|
func (c *SentinelClient) GetMasterAddrByName(name string) *StringSliceCmd {
|
||||||
cmd := NewStringSliceCmd("SENTINEL", "get-master-addr-by-name", name)
|
cmd := NewStringSliceCmd("SENTINEL", "get-master-addr-by-name", name)
|
||||||
c.Process(cmd)
|
c.Process(cmd)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *sentinelClient) Sentinels(name string) *SliceCmd {
|
func (c *SentinelClient) Sentinels(name string) *SliceCmd {
|
||||||
cmd := NewSliceCmd("SENTINEL", "sentinels", name)
|
cmd := NewSliceCmd("SENTINEL", "sentinels", name)
|
||||||
c.Process(cmd)
|
c.Process(cmd)
|
||||||
return cmd
|
return cmd
|
||||||
|
@ -151,7 +149,7 @@ type sentinelFailover struct {
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
masterName string
|
masterName string
|
||||||
_masterAddr string
|
_masterAddr string
|
||||||
sentinel *sentinelClient
|
sentinel *SentinelClient
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *sentinelFailover) Close() error {
|
func (d *sentinelFailover) Close() error {
|
||||||
|
@ -205,7 +203,7 @@ func (d *sentinelFailover) masterAddr() (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, sentinelAddr := range d.sentinelAddrs {
|
for i, sentinelAddr := range d.sentinelAddrs {
|
||||||
sentinel := newSentinel(&Options{
|
sentinel := NewSentinelClient(&Options{
|
||||||
Addr: sentinelAddr,
|
Addr: sentinelAddr,
|
||||||
|
|
||||||
DialTimeout: d.opt.DialTimeout,
|
DialTimeout: d.opt.DialTimeout,
|
||||||
|
@ -219,7 +217,8 @@ func (d *sentinelFailover) masterAddr() (string, error) {
|
||||||
|
|
||||||
masterAddr, err := sentinel.GetMasterAddrByName(d.masterName).Result()
|
masterAddr, err := sentinel.GetMasterAddrByName(d.masterName).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internal.Logf("sentinel: GetMasterAddrByName master=%q failed: %s", d.masterName, err)
|
internal.Logf("sentinel: GetMasterAddrByName master=%q failed: %s",
|
||||||
|
d.masterName, err)
|
||||||
sentinel.Close()
|
sentinel.Close()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -246,7 +245,7 @@ func (d *sentinelFailover) switchMaster(masterAddr string) {
|
||||||
d._masterAddr = masterAddr
|
d._masterAddr = masterAddr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *sentinelFailover) setSentinel(sentinel *sentinelClient) {
|
func (d *sentinelFailover) setSentinel(sentinel *SentinelClient) {
|
||||||
d.discoverSentinels(sentinel)
|
d.discoverSentinels(sentinel)
|
||||||
d.sentinel = sentinel
|
d.sentinel = sentinel
|
||||||
go d.listen(sentinel)
|
go d.listen(sentinel)
|
||||||
|
@ -268,7 +267,7 @@ func (d *sentinelFailover) _resetSentinel() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *sentinelFailover) discoverSentinels(sentinel *sentinelClient) {
|
func (d *sentinelFailover) discoverSentinels(sentinel *SentinelClient) {
|
||||||
sentinels, err := sentinel.Sentinels(d.masterName).Result()
|
sentinels, err := sentinel.Sentinels(d.masterName).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internal.Logf("sentinel: Sentinels master=%q failed: %s", d.masterName, err)
|
internal.Logf("sentinel: Sentinels master=%q failed: %s", d.masterName, err)
|
||||||
|
@ -292,7 +291,7 @@ func (d *sentinelFailover) discoverSentinels(sentinel *sentinelClient) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *sentinelFailover) listen(sentinel *sentinelClient) {
|
func (d *sentinelFailover) listen(sentinel *SentinelClient) {
|
||||||
var pubsub *PubSub
|
var pubsub *PubSub
|
||||||
for {
|
for {
|
||||||
if pubsub == nil {
|
if pubsub == nil {
|
||||||
|
|
Loading…
Reference in New Issue