Skip to content

ljy2010a/redisgo-sentinel

Repository files navigation

redigo-sentinel

Build Status Coverage Status Go Report Card GoDoc

redigo-sentinel is a Go sentinel client for the Redis database base on Redigo refer by sentinel-clients-doc

Features

  • Redis service discovery via Sentinel
  • Handling reconnections
  • Sentinel failover disconnection
  • Connection pools
  • Error reporting
  • Sentinels list automatic refresh
  • Subscribe to Sentinel events to improve responsiveness
    • +switch-master
    • +sentinel

Future

  • Connecting to slaves
  • Subscribe to Sentinel more events to improve responsiveness

Documentation

Installation

Install redigo-sentinel using the "go get" command:

go get github.com/ljy2010a/redisgo-sentinel

The Go distribution is Redigo's only dependency.

Related Projects

  • redigo - Redigo is a Go client for the Redis database.

Example

	sentinel := &Sentinel{
		SentinelAddrs: []string{"127.0.0.1:26379", "127.0.0.1:26378"},
		MasterName:    "mymaster",
		SentinelDial: func(addr string) (redis.Conn, error) {
			c, err := redis.Dial("tcp", addr)
			if err != nil {
				log.Printf("sentinel not Available %v \n", addr)
				return nil, err
			}
			log.Printf("sentinel Available %v \n", addr)
			return c, nil
		},
		PoolDial: func(addr string) (redis.Conn, error) {
			log.Printf("masterpool connect to : %v ", addr)
			c, err := redis.Dial("tcp", addr)
			if err != nil {
				log.Printf("masterpool not Available %v \n", addr)
				return nil, err
			}
			log.Printf("masterpool Available at %v \n", addr)
			return c, nil
		},

		masterPool: &redis.Pool{
			MaxIdle:     10,
			MaxActive:   200,
			Wait:        true,
			IdleTimeout: 60 * time.Second,
		},
	}

	err := sentinel.Load()
	if err != nil {
		log.Panicf("%v\n", err)
	}
	timeTicker := time.NewTicker(time.Second)
	defer timeTicker.Stop()
	i := 0
	for {
		select {
		case <-timeTicker.C:
			i++
			if i > 300 {
				goto Exit
			}
			log.Printf("sentinels : %v \n", sentinel.SentinelsAddrs())
			pool := sentinel.Pool()
			if pool != nil {
				rconn := pool.Get()
				_, err := rconn.Do(
					"SETEX",
					fmt.Sprintf("test:%d", i),
					time.Second.Seconds()*3600,
					i,
				)
				rconn.Close()
				if err != nil {
					log.Printf("setex error :  %v\n", err)
				}
			}
		}
	}
Exit:
	sentinel.Close()
	log.Printf("Exit \n")

License

redigo-sentinel is available under the Apache License, Version 2.0.

About

base on redigo client implement sentinel

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages