Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func TestRole ¶
TestRole wraps GetRole in a test to verify if the role matches an expected role string. If there was any error in querying the supplied connection, the function returns false. Works with Redis >= 2.8.12. It's not goroutine safe, but if you call this method on pooled connections then you are OK.
Types ¶
type NoSentinelsAvailable ¶
type NoSentinelsAvailable struct {
// contains filtered or unexported fields
}
NoSentinelsAvailable is returned when all sentinels in the list are exhausted (or none configured), and contains the last error returned by Dial (which may be nil)
func (NoSentinelsAvailable) Error ¶
func (ns NoSentinelsAvailable) Error() string
type Sentinel ¶
type Sentinel struct {
// Addrs is a slice with known Sentinel addresses.
Addrs []string
// MasterName is a name of Redis master Sentinel servers monitor.
MasterName string
// Dial is a user supplied function to connect to Sentinel on given address. This
// address will be chosen from Addrs slice.
// Note that as per the redis-sentinel client guidelines, a timeout is mandatory
// while connecting to Sentinels, and should not be set to 0.
Dial func(addr string) (redis.Conn, error)
// Pool is a user supplied function returning custom connection pool to Sentinel.
// This can be useful to tune options if you are not satisfied with what default
// Sentinel pool offers. See defaultPool() method for default pool implementation.
// In most cases you only need to provide Dial function and let this be nil.
Pool func(addr string) *redis.Pool
// contains filtered or unexported fields
}
Sentinel provides a way to add high availability (HA) to Redis Pool using preconfigured addresses of Sentinel servers and name of master which Sentinels monitor. It works with Redis >= 2.8.12 (mostly because of ROLE command that was introduced in that version, it's possible though to support old versions using INFO command).
Example of the simplest usage to contact master "mymaster":
func newSentinelPool() *redis.Pool {
sntnl := &sentinel.Sentinel{
Addrs: []string{":26379", ":26380", ":26381"},
MasterName: "mymaster",
Dial: func(addr string) (redis.Conn, error) {
timeout := 500 * time.Millisecond
c, err := redis.DialTimeout("tcp", addr, timeout, timeout, timeout)
if err != nil {
return nil, err
}
return c, nil
},
}
return &redis.Pool{
MaxIdle: 3,
MaxActive: 64,
Wait: true,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
masterAddr, err := sntnl.MasterAddr()
if err != nil {
return nil, err
}
c, err := redis.Dial("tcp", masterAddr)
if err != nil {
return nil, err
}
return c, nil
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
if !sentinel.TestRole(c, "master") {
return errors.New("Role check failed")
} else {
return nil
}
},
}
}
func (*Sentinel) Discover ¶
Discover allows to update list of known Sentinel addresses. From docs:
A client may update its internal list of Sentinel nodes following this procedure: 1) Obtain a list of other Sentinels for this master using the command SENTINEL sentinels <master-name>. 2) Add every ip:port pair not already existing in our list at the end of the list.
func (*Sentinel) MasterAddr ¶
MasterAddr returns an address of current Redis master instance.
func (*Sentinel) SentinelAddrs ¶
SentinelAddrs returns a slice of known Sentinel addresses Sentinel server aware of.
func (*Sentinel) SlaveAddrs ¶
SlaveAddrs returns a slice with known slave addresses of current master instance.