mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-02-01 18:20:17 +01:00
Revert "Revert "V0.4.0 imporve error response""
This reverts commit 9744f45e86.
This commit is contained in:
@@ -4,21 +4,20 @@ import (
|
||||
"github.com/gomodule/redigo/redis"
|
||||
"github.com/spf13/viper"
|
||||
"runtime/debug"
|
||||
"time"
|
||||
)
|
||||
|
||||
var RedisClient = Redis{}
|
||||
|
||||
type ConsumeFunc func(channel string, message []byte) error
|
||||
var RedisClient *Redis
|
||||
|
||||
type Redis struct {
|
||||
pool *redis.Pool
|
||||
}
|
||||
|
||||
func NewRedisClient() *Redis {
|
||||
return &Redis{pool: NewRedisPool()}
|
||||
}
|
||||
func (r *Redis) RPush(collection string, value interface{}) error {
|
||||
c, err := GetRedisConn()
|
||||
if err != nil {
|
||||
debug.PrintStack()
|
||||
return err
|
||||
}
|
||||
c := r.pool.Get()
|
||||
defer c.Close()
|
||||
|
||||
if _, err := c.Do("RPUSH", collection, value); err != nil {
|
||||
@@ -29,11 +28,7 @@ func (r *Redis) RPush(collection string, value interface{}) error {
|
||||
}
|
||||
|
||||
func (r *Redis) LPop(collection string) (string, error) {
|
||||
c, err := GetRedisConn()
|
||||
if err != nil {
|
||||
debug.PrintStack()
|
||||
return "", err
|
||||
}
|
||||
c := r.pool.Get()
|
||||
defer c.Close()
|
||||
|
||||
value, err2 := redis.String(c.Do("LPOP", collection))
|
||||
@@ -44,11 +39,7 @@ func (r *Redis) LPop(collection string) (string, error) {
|
||||
}
|
||||
|
||||
func (r *Redis) HSet(collection string, key string, value string) error {
|
||||
c, err := GetRedisConn()
|
||||
if err != nil {
|
||||
debug.PrintStack()
|
||||
return err
|
||||
}
|
||||
c := r.pool.Get()
|
||||
defer c.Close()
|
||||
|
||||
if _, err := c.Do("HSET", collection, key, value); err != nil {
|
||||
@@ -59,11 +50,7 @@ func (r *Redis) HSet(collection string, key string, value string) error {
|
||||
}
|
||||
|
||||
func (r *Redis) HGet(collection string, key string) (string, error) {
|
||||
c, err := GetRedisConn()
|
||||
if err != nil {
|
||||
debug.PrintStack()
|
||||
return "", err
|
||||
}
|
||||
c := r.pool.Get()
|
||||
defer c.Close()
|
||||
|
||||
value, err2 := redis.String(c.Do("HGET", collection, key))
|
||||
@@ -74,11 +61,7 @@ func (r *Redis) HGet(collection string, key string) (string, error) {
|
||||
}
|
||||
|
||||
func (r *Redis) HDel(collection string, key string) error {
|
||||
c, err := GetRedisConn()
|
||||
if err != nil {
|
||||
debug.PrintStack()
|
||||
return err
|
||||
}
|
||||
c := r.pool.Get()
|
||||
defer c.Close()
|
||||
|
||||
if _, err := c.Do("HDEL", collection, key); err != nil {
|
||||
@@ -88,11 +71,7 @@ func (r *Redis) HDel(collection string, key string) error {
|
||||
}
|
||||
|
||||
func (r *Redis) HKeys(collection string) ([]string, error) {
|
||||
c, err := GetRedisConn()
|
||||
if err != nil {
|
||||
debug.PrintStack()
|
||||
return []string{}, err
|
||||
}
|
||||
c := r.pool.Get()
|
||||
defer c.Close()
|
||||
|
||||
value, err2 := redis.Strings(c.Do("HKeys", collection))
|
||||
@@ -102,7 +81,7 @@ func (r *Redis) HKeys(collection string) ([]string, error) {
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func GetRedisConn() (redis.Conn, error) {
|
||||
func NewRedisPool() *redis.Pool {
|
||||
var address = viper.GetString("redis.address")
|
||||
var port = viper.GetString("redis.port")
|
||||
var database = viper.GetString("redis.database")
|
||||
@@ -114,14 +93,30 @@ func GetRedisConn() (redis.Conn, error) {
|
||||
} else {
|
||||
url = "redis://x:" + password + "@" + address + ":" + port + "/" + database
|
||||
}
|
||||
c, err := redis.DialURL(url)
|
||||
if err != nil {
|
||||
debug.PrintStack()
|
||||
return c, err
|
||||
return &redis.Pool{
|
||||
Dial: func() (conn redis.Conn, e error) {
|
||||
return redis.DialURL(url,
|
||||
redis.DialConnectTimeout(time.Second*10),
|
||||
redis.DialReadTimeout(time.Second*10),
|
||||
redis.DialWriteTimeout(time.Second*10),
|
||||
)
|
||||
},
|
||||
TestOnBorrow: func(c redis.Conn, t time.Time) error {
|
||||
if time.Since(t) < time.Minute {
|
||||
return nil
|
||||
}
|
||||
_, err := c.Do("PING")
|
||||
return err
|
||||
},
|
||||
MaxIdle: 10,
|
||||
MaxActive: 0,
|
||||
IdleTimeout: 300 * time.Second,
|
||||
Wait: false,
|
||||
MaxConnLifetime: 0,
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func InitRedis() error {
|
||||
RedisClient = NewRedisClient()
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user