Exemplo n.º 1
1
func (redismqQFactory) get(name, consumerName string) (*redismqQ, error) {
	host, err := config.GetString("redis-queue:host")
	if err != nil {
		host = "localhost"
	}
	port, err := config.GetString("redis-queue:port")
	if err != nil {
		if nport, err := config.GetInt("redis-queue:port"); err != nil {
			port = "6379"
		} else {
			port = fmt.Sprintf("%d", nport)
		}
	}
	password, _ := config.GetString("redis-queue:password")
	db, err := config.GetInt("redis-queue:db")
	if err != nil {
		db = 3
	}
	maxIdle, _ := config.GetInt("redis-queue:pool-max-idle-conn")
	if maxIdle == 0 {
		maxIdle = 20
	}
	idleTimeout, _ := config.GetDuration("redis-queue:pool-idle-timeout")
	if idleTimeout == 0 {
		idleTimeout = 300e9
	}
	pool := redis.NewPool(func() (redis.Conn, error) {
		conn, err := redis.Dial("tcp", host+":"+port)
		if err != nil {
			return nil, err
		}
		if password != "" {
			_, err = conn.Do("AUTH", password)
			if err != nil {
				return nil, err
			}
		}
		_, err = conn.Do("SELECT", db)
		return conn, err
	}, maxIdle)
	pool.IdleTimeout = idleTimeout
	return &redismqQ{name: name, pool: pool}, nil
}
Exemplo n.º 2
0
func (factory *redismqQFactory) getPool() *redis.Pool {
	factory.Lock()
	defer factory.Unlock()
	if factory.pool != nil {
		return factory.pool
	}
	maxIdle, err := config.GetInt("redis-queue:pool-max-idle-conn")
	if err != nil {
		maxIdle = 20
	}
	idleTimeout, err := config.GetDuration("redis-queue:pool-idle-timeout")
	if err != nil {
		idleTimeout = 5 * time.Minute
	}
	factory.pool = &redis.Pool{
		MaxIdle:     maxIdle,
		IdleTimeout: idleTimeout,
		Dial:        factory.dial,
	}
	return factory.pool
}