proxy.go 1.5 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
package redis_manager

import (
	"github.com/eolinker/goku/common/redis"
)

type redisProxy struct {
	redis.Cmdable
	config RedisConfig
}


func (p *redisProxy) Nodes() []string {


	ch:= make(chan string,1)
	switch p.config.GetMode() {
	case RedisModeCluster:
		{
			conn := p.Cmdable.(*redis.ClusterClient)
			go func(ch chan string ) {
				conn.ForEachMaster(func(client *redis.Client) error {
					ch<-client.Options().Addr
					return nil
				})
				close(ch)
			}(ch)

		}
	case RedisModeSentinel:
		{

			conn := p.Cmdable.(*redis.SentinelRing)
			go func(ch chan string ) {
				conn.ForEachAddr(func(addr string) error {
					ch<-addr
					return nil
				})
				close(ch)
			}(ch)
		}
	case RedisModeStand:
		{

			conn := p.Cmdable.(*redis.Ring)
			go func(ch chan string ) {
				conn.ForEachShard(func(client *redis.Client) error {
					ch<-client.Options().Addr
					return nil
				})
				close(ch)
			}(ch)
		}
	}


	 nodes:= make([]string,0,10)
	for addr:=range ch{
		nodes = append(nodes,addr)
	}
	return nodes
}

func (p *redisProxy) Foreach(fn func(client *redis.Client) error) error {
	switch p.config.GetMode() {
	case RedisModeCluster:
		{
			conn := p.Cmdable.(*redis.ClusterClient)
			return conn.ForEachMaster(fn)
		}
	case RedisModeSentinel:
		{

			conn := p.Cmdable.(*redis.SentinelRing)
			return conn.ForEachShard(fn)
		}
	case RedisModeStand:
		{

			conn := p.Cmdable.(*redis.Ring)
			return conn.ForEachShard(fn)
		}
	}

	panic("未知错误")

}

func (p *redisProxy) GetConfig() RedisConfig {
	return p.config
}