mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 18:51:03 +01:00
Merge pull request #1546 from 1819997197/fix-1545
sessions: database: redis: fix clustering Former-commit-id: 063e026a06f61a0a1414bb8d97361bf7a6e8601c
This commit is contained in:
commit
cdf0fe24e9
|
@ -31,7 +31,6 @@ type Config struct {
|
||||||
Addr string
|
Addr string
|
||||||
// Clusters a list of network addresses for clusters.
|
// Clusters a list of network addresses for clusters.
|
||||||
// If not empty "Addr" is ignored.
|
// If not empty "Addr" is ignored.
|
||||||
// Currently only Radix() Driver supports it.
|
|
||||||
Clusters []string
|
Clusters []string
|
||||||
// Password string .If no password then no 'AUTH'. Defaults to "".
|
// Password string .If no password then no 'AUTH'. Defaults to "".
|
||||||
Password string
|
Password string
|
||||||
|
@ -119,8 +118,7 @@ func New(cfg ...Config) *Database {
|
||||||
db := &Database{c: c}
|
db := &Database{c: c}
|
||||||
_, err := db.c.Driver.PingPong()
|
_, err := db.c.Driver.PingPong()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
golog.Debugf("error connecting to redis: %v", err)
|
panic(err)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
// runtime.SetFinalizer(db, closeDB)
|
// runtime.SetFinalizer(db, closeDB)
|
||||||
return db
|
return db
|
||||||
|
|
|
@ -4,13 +4,18 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/mediocregopher/radix/v3"
|
"github.com/mediocregopher/radix/v3"
|
||||||
"github.com/mediocregopher/radix/v3/resp/resp2"
|
"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,
|
// RadixDriver the Redis service based on the radix go client,
|
||||||
// contains the config and the redis pool.
|
// contains the config and the redis pool.
|
||||||
type RadixDriver struct {
|
type RadixDriver struct {
|
||||||
|
@ -18,7 +23,7 @@ type RadixDriver struct {
|
||||||
Connected bool
|
Connected bool
|
||||||
// Config the read-only redis database config.
|
// Config the read-only redis database config.
|
||||||
Config Config
|
Config Config
|
||||||
pool *radix.Pool
|
pool radixPool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connect connects to the redis, called only once
|
// Connect connects to the redis, called only once
|
||||||
|
@ -70,6 +75,12 @@ func (r *RadixDriver) Connect(c Config) error {
|
||||||
|
|
||||||
var connFunc radix.ConnFunc
|
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 {
|
if len(c.Clusters) > 0 {
|
||||||
cluster, err := radix.NewCluster(c.Clusters)
|
cluster, err := radix.NewCluster(c.Clusters)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -84,16 +95,33 @@ func (r *RadixDriver) Connect(c Config) error {
|
||||||
return radix.Dial(c.Network, node.Addr, options...)
|
return radix.Dial(c.Network, node.Addr, options...)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
*/
|
||||||
connFunc = func(network, addr string) (radix.Conn, error) {
|
connFunc = func(network, addr string) (radix.Conn, error) {
|
||||||
return radix.Dial(c.Network, c.Addr, options...)
|
return radix.Dial(c.Network, c.Addr, options...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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))
|
||||||
}
|
}
|
||||||
|
|
||||||
pool, err := radix.NewPool(c.Network, c.Addr, c.MaxActive, radix.PoolConnFunc(connFunc))
|
cluster, err := radix.NewCluster(c.Clusters, radix.ClusterPoolFunc(poolFunc))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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
|
r.Connected = true
|
||||||
r.pool = pool
|
r.pool = pool
|
||||||
r.Config = c
|
r.Config = c
|
||||||
|
|
Loading…
Reference in New Issue
Block a user