yuriy0803 3 years ago
parent
commit
d8c87e3e45
  1. 9
      api.json
  2. 6
      proxy/proxy.go
  3. 38
      storage/redis.go

9
api.json

@ -93,7 +93,14 @@
"endpoint": "127.0.0.1:6379", "endpoint": "127.0.0.1:6379",
"poolSize": 10, "poolSize": 10,
"database": 0, "database": 0,
"password": "" "password": "",
"sentinelEnabled": false,
"masterName": "mymaster",
"sentinelAddrs": [
"127.0.0.1:26379",
"127.0.0.1:26389",
"127.0.0.1:26399"
]
}, },
"exchange": { "exchange": {

6
proxy/proxy.go

@ -169,6 +169,12 @@ func (s *ProxyServer) rpc() *rpc.RPCClient {
} }
func (s *ProxyServer) checkUpstreams() { func (s *ProxyServer) checkUpstreams() {
idx := atomic.LoadInt32(&s.upstream)
current := s.upstreams[idx]
if current.Check() {
return
}
candidate := int32(0) candidate := int32(0)
backup := false backup := false

38
storage/redis.go

@ -16,10 +16,13 @@ import (
) )
type Config struct { type Config struct {
Endpoint string `json:"endpoint"` SentinelEnabled bool `json:"sentinelEnabled"`
Password string `json:"password"` Endpoint string `json:"endpoint"`
Database int64 `json:"database"` Password string `json:"password"`
PoolSize int `json:"poolSize"` Database int64 `json:"database"`
PoolSize int `json:"poolSize"`
MasterName string `json:"masterName"`
SentinelAddrs []string `json:"sentinelAddrs"`
} }
type RedisClient struct { type RedisClient struct {
@ -156,16 +159,25 @@ type Worker struct {
} }
func NewRedisClient(cfg *Config, prefix string, pplns int64, CoinName string) *RedisClient { func NewRedisClient(cfg *Config, prefix string, pplns int64, CoinName string) *RedisClient {
options := redis.Options{ var client *redis.Client
Addr: cfg.Endpoint, if cfg.SentinelEnabled && len(cfg.MasterName) != 0 && len(cfg.SentinelAddrs) != 0 {
Password: cfg.Password, // sentinel mode
DB: cfg.Database, client = redis.NewFailoverClient(&redis.FailoverOptions{
PoolSize: cfg.PoolSize, MasterName: cfg.MasterName,
} SentinelAddrs: cfg.SentinelAddrs,
if cfg.Endpoint[0:1] == "/" { Password: cfg.Password,
options.Network = "unix" DB: cfg.Database,
PoolSize: cfg.PoolSize,
})
} else {
// single instance
client = redis.NewClient(&redis.Options{
Addr: cfg.Endpoint,
Password: cfg.Password,
DB: cfg.Database,
PoolSize: cfg.PoolSize,
})
} }
client := redis.NewClient(&options)
return &RedisClient{client: client, prefix: prefix, pplns: pplns, CoinName: CoinName} return &RedisClient{client: client, prefix: prefix, pplns: pplns, CoinName: CoinName}
} }

Loading…
Cancel
Save