Merge pull request #1546 from 1819997197/fix-1545

sessions: database: redis: fix clustering
Former-commit-id: 063e026a06f61a0a1414bb8d97361bf7a6e8601c
This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-06-27 14:00:43 +03:00 committed by GitHub
commit cdf0fe24e9
2 changed files with 37 additions and 11 deletions

View File

@ -31,7 +31,6 @@ type Config struct {
Addr string
// Clusters a list of network addresses for clusters.
// If not empty "Addr" is ignored.
// Currently only Radix() Driver supports it.
Clusters []string
// Password string .If no password then no 'AUTH'. Defaults to "".
Password string
@ -119,8 +118,7 @@ func New(cfg ...Config) *Database {
db := &Database{c: c}
_, err := db.c.Driver.PingPong()
if err != nil {
golog.Debugf("error connecting to redis: %v", err)
return nil
panic(err)
}
// runtime.SetFinalizer(db, closeDB)
return db

View File

@ -4,13 +4,18 @@ import (
"bufio"
"errors"
"fmt"
"math/rand"
"strconv"
"github.com/mediocregopher/radix/v3"
"github.com/mediocregopher/radix/v3/resp/resp2"
)
// radixPool an interface to complete both *radix.Pool and *radix.Cluster.
type radixPool interface {
Do(a radix.Action) error
Close() error
}
// RadixDriver the Redis service based on the radix go client,
// contains the config and the redis pool.
type RadixDriver struct {
@ -18,7 +23,7 @@ type RadixDriver struct {
Connected bool
// Config the read-only redis database config.
Config Config
pool *radix.Pool
pool radixPool
}
// Connect connects to the redis, called only once
@ -70,6 +75,12 @@ func (r *RadixDriver) Connect(c Config) error {
var connFunc radix.ConnFunc
/* Note(@kataras): according to #1545 the below does NOT work, and we should
use the Cluster instance itself to fire requests.
We need a separate `radix.Cluster` instance to do the calls,
fortunally both Pool and Cluster implement the same Do and Close methods we need,
so a new `radixPool` interface to remove any dupl code is used instead.
if len(c.Clusters) > 0 {
cluster, err := radix.NewCluster(c.Clusters)
if err != nil {
@ -84,14 +95,31 @@ func (r *RadixDriver) Connect(c Config) error {
return radix.Dial(c.Network, node.Addr, options...)
}
} else {
connFunc = func(network, addr string) (radix.Conn, error) {
return radix.Dial(c.Network, c.Addr, options...)
}
*/
connFunc = func(network, addr string) (radix.Conn, error) {
return radix.Dial(c.Network, c.Addr, options...)
}
pool, err := radix.NewPool(c.Network, c.Addr, c.MaxActive, radix.PoolConnFunc(connFunc))
if err != nil {
return err
var pool radixPool
if len(c.Clusters) > 0 {
poolFunc := func(network, addr string) (radix.Client, error) {
return radix.NewPool(network, addr, c.MaxActive, radix.PoolConnFunc(connFunc))
}
cluster, err := radix.NewCluster(c.Clusters, radix.ClusterPoolFunc(poolFunc))
if err != nil {
return err
}
pool = cluster
} else {
p, err := radix.NewPool(c.Network, c.Addr, c.MaxActive, radix.PoolConnFunc(connFunc))
if err != nil {
return err
}
pool = p
}
r.Connected = true