mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-17 06:09:53 +05:00
Modified redis to handle auth and select per connection in the pool, added maxConns parameter in constructor
This commit is contained in:
@@ -56,11 +56,11 @@ func init() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
case "redis":
|
||||
ratingStorage, _ = NewRedisStorage("127.0.0.1:6379", 12, "", utils.MSGPACK)
|
||||
ratingStorage, _ = NewRedisStorage("127.0.0.1:6379", 12, "", utils.MSGPACK, utils.REDIS_MAX_CONNS)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
accountingStorage, _ = NewRedisStorage("127.0.0.1:6379", 13, "", utils.MSGPACK)
|
||||
accountingStorage, _ = NewRedisStorage("127.0.0.1:6379", 13, "", utils.MSGPACK, utils.REDIS_MAX_CONNS)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ import (
|
||||
"github.com/cgrates/cgrates/cache2go"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
"github.com/mediocregopher/radix.v2/pool"
|
||||
"github.com/mediocregopher/radix.v2/redis"
|
||||
|
||||
"io/ioutil"
|
||||
"time"
|
||||
@@ -38,24 +39,29 @@ type RedisStorage struct {
|
||||
ms Marshaler
|
||||
}
|
||||
|
||||
func NewRedisStorage(address string, db int, pass, mrshlerStr string) (*RedisStorage, error) {
|
||||
p, err := pool.New("tcp", address, 10)
|
||||
//p, err := redis.Dial("tcp", address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
conn, err := p.Get()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer p.Put(conn)
|
||||
if err := conn.Cmd("SELECT", db).Err; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if pass != "" {
|
||||
if err := conn.Cmd("AUTH", pass).Err; err != nil {
|
||||
func NewRedisStorage(address string, db int, pass, mrshlerStr string, maxConns int) (*RedisStorage, error) {
|
||||
df := func(network, addr string) (*redis.Client, error) {
|
||||
client, err := redis.Dial(network, addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(pass) != 0 {
|
||||
if err = client.Cmd("AUTH", pass).Err; err != nil {
|
||||
client.Close()
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if db != 0 {
|
||||
if err = client.Cmd("SELECT", db).Err; err != nil {
|
||||
client.Close()
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
p, err := pool.NewCustom("tcp", address, maxConns, df)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var mrshler Marshaler
|
||||
if mrshlerStr == utils.MSGPACK {
|
||||
|
||||
@@ -35,7 +35,7 @@ func TestConnectRedis(t *testing.T) {
|
||||
return
|
||||
}
|
||||
cfg, _ := config.NewDefaultCGRConfig()
|
||||
rds, err = NewRedisStorage(fmt.Sprintf("%s:%s", cfg.TpDbHost, cfg.TpDbPort), 4, cfg.TpDbPass, cfg.DBDataEncoding)
|
||||
rds, err = NewRedisStorage(fmt.Sprintf("%s:%s", cfg.TpDbHost, cfg.TpDbPort), 4, cfg.TpDbPass, cfg.DBDataEncoding, utils.REDIS_MAX_CONNS)
|
||||
if err != nil {
|
||||
t.Fatal("Could not connect to Redis", err.Error())
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ func ConfigureRatingStorage(db_type, host, port, name, user, pass, marshaler str
|
||||
if port != "" {
|
||||
host += ":" + port
|
||||
}
|
||||
d, err = NewRedisStorage(host, db_nb, pass, marshaler)
|
||||
d, err = NewRedisStorage(host, db_nb, pass, marshaler, utils.REDIS_MAX_CONNS)
|
||||
default:
|
||||
err = errors.New("unknown db")
|
||||
}
|
||||
@@ -63,7 +63,7 @@ func ConfigureAccountingStorage(db_type, host, port, name, user, pass, marshaler
|
||||
if port != "" {
|
||||
host += ":" + port
|
||||
}
|
||||
d, err = NewRedisStorage(host, db_nb, pass, marshaler)
|
||||
d, err = NewRedisStorage(host, db_nb, pass, marshaler, utils.REDIS_MAX_CONNS)
|
||||
case utils.MONGO:
|
||||
d, err = NewMongoStorage(host, port, name, user, pass)
|
||||
db = d.(AccountingStorage)
|
||||
|
||||
@@ -31,6 +31,7 @@ var (
|
||||
const (
|
||||
VERSION = "0.9.1~rc8"
|
||||
DIAMETER_FIRMWARE_REVISION = 918
|
||||
REDIS_MAX_CONNS = 10
|
||||
POSTGRES = "postgres"
|
||||
MYSQL = "mysql"
|
||||
MONGO = "mongo"
|
||||
|
||||
Reference in New Issue
Block a user