Documentation ¶
Index ¶
- Variables
- func NewRedis(redisConf *MyRedisConf) (pool *redis.Pool, err error)
- func TestRole(c redis.Conn, expectedRole string) bool
- type MyRedisConf
- func (c *MyRedisConf) SetDialConnectTimeout(dialConnectTimeout int) *MyRedisConf
- func (c *MyRedisConf) SetDialReadTimeoutB(dialReadTimeout int) *MyRedisConf
- func (c *MyRedisConf) SetDialWriteTimeout(dialWriteTimeout int) *MyRedisConf
- func (c *MyRedisConf) SetRedisDB(redisDB int) *MyRedisConf
- func (c *MyRedisConf) SetRedisHost(redisHost string) *MyRedisConf
- func (c *MyRedisConf) SetRedisIdleTimeout(redisIdleTimeout int) *MyRedisConf
- func (c *MyRedisConf) SetRedisMaxActive(redisMaxActive int) *MyRedisConf
- func (c *MyRedisConf) SetRedisMaxIdle(redisMaxIdle int) *MyRedisConf
- func (c *MyRedisConf) SetRedisPass(redisPass string) *MyRedisConf
- type NoSentinelsAvailable
- type Sentinel
- type Slave
Constants ¶
This section is empty.
Variables ¶
var MyRedisDefaultTimeout = 1
MyRedisDefaultTimeout default global connect ing timeout
var NewRedisPool = NewRedis
NewRedisPool alias NewRedis
Functions ¶
func NewRedis ¶
func NewRedis(redisConf *MyRedisConf) (pool *redis.Pool, err error)
NewRedis new a redis pool
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 MyRedisConf ¶
type MyRedisConf struct { RedisHost string `yaml:"host"` // Maximum number of idle connections in the pool. RedisMaxIdle int `yaml:"max_idle"` // Maximum number of connections allocated by the pool at a given time. // When zero, there is no limit on the number of connections in the pool. RedisMaxActive int `yaml:"max_active"` // Close connections after remaining idle for this duration. If the value // is zero, then idle connections are not closed. Applications should set // the timeout to a value less than the server's timeout. RedisIdleTimeout int `yaml:"idle_timeout"` RedisDB int `yaml:"database"` RedisPass string `yaml:"pass"` // sentinel IsCluster bool `yaml:"is_cluster"` MasterName string `yaml:"master_name"` // timeout, second DialConnectTimeout int `yaml:"dial_connect_timeout"` DialReadTimeout int `yaml:"dial_read_timeout"` DialWriteTimeout int `yaml:"dial_write_timeout"` }
MyRedisConf redis config
func (*MyRedisConf) SetDialConnectTimeout ¶ added in v1.0.13
func (c *MyRedisConf) SetDialConnectTimeout(dialConnectTimeout int) *MyRedisConf
func (*MyRedisConf) SetDialReadTimeoutB ¶ added in v1.0.13
func (c *MyRedisConf) SetDialReadTimeoutB(dialReadTimeout int) *MyRedisConf
func (*MyRedisConf) SetDialWriteTimeout ¶ added in v1.0.13
func (c *MyRedisConf) SetDialWriteTimeout(dialWriteTimeout int) *MyRedisConf
func (*MyRedisConf) SetRedisDB ¶ added in v1.0.13
func (c *MyRedisConf) SetRedisDB(redisDB int) *MyRedisConf
func (*MyRedisConf) SetRedisHost ¶ added in v1.0.13
func (c *MyRedisConf) SetRedisHost(redisHost string) *MyRedisConf
func (*MyRedisConf) SetRedisIdleTimeout ¶ added in v1.0.13
func (c *MyRedisConf) SetRedisIdleTimeout(redisIdleTimeout int) *MyRedisConf
func (*MyRedisConf) SetRedisMaxActive ¶ added in v1.0.13
func (c *MyRedisConf) SetRedisMaxActive(redisMaxActive int) *MyRedisConf
func (*MyRedisConf) SetRedisMaxIdle ¶ added in v1.0.13
func (c *MyRedisConf) SetRedisMaxIdle(redisMaxIdle int) *MyRedisConf
func (*MyRedisConf) SetRedisPass ¶ added in v1.0.13
func (c *MyRedisConf) SetRedisPass(redisPass string) *MyRedisConf
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.