Back to godoc.org
bosun.org / cmd / bosun / database / sentinel

Package sentinel

v0.0.0-...-8a7cd2d
Latest Go to latest

The latest major version is .

Published: Jun 18, 2020 | License: MIT | Module: bosun.org

Index

func TestRole

func TestRole(c redis.Conn, expectedRole string) bool

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.

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) Close

func (s *Sentinel) Close() error

Close closes current connection to Sentinel.

func (*Sentinel) Discover

func (s *Sentinel) Discover() error

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

func (s *Sentinel) MasterAddr() (string, error)

MasterAddr returns an address of current Redis master instance.

func (*Sentinel) SentinelAddrs

func (s *Sentinel) SentinelAddrs() ([]string, error)

SentinelAddrs returns a slice of known Sentinel addresses Sentinel server aware of.

func (*Sentinel) SlaveAddrs

func (s *Sentinel) SlaveAddrs() ([]string, error)

SlaveAddrs returns a slice with known slave addresses of current master instance.

func (*Sentinel) Slaves

func (s *Sentinel) Slaves() ([]*Slave, error)

Slaves returns a slice with known slaves of master instance.

type Slave

type Slave struct {
	// contains filtered or unexported fields
}

Slave represents a Redis slave instance which is known by Sentinel.

func (*Slave) Addr

func (s *Slave) Addr() string

Addr returns an address of slave.

func (*Slave) Available

func (s *Slave) Available() bool

Available returns if slave is in working state at moment based on information in slave flags.

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier