mna / redisc

A Go redis cluster client built on top of redigo.
BSD 3-Clause "New" or "Revised" License
227 stars 35 forks source link

redisc: too many attempts #20

Closed wadechen2003 closed 5 years ago

wadechen2003 commented 5 years ago

Dear Sir My redis is Azure redis service. Redis version was 3.2.7, clustered 3 maters and 3 slaves

I tried to get the connection and do command redisConnection.Do("GET", key) sometimes works, sometimes return redisc: too many attempts

hear are some related codes, which i used to create the connection. where I setup error ? thank you very much

func redisClusterNewClient() redis.Conn {
    //return cluster.Get()
    retryConn, err := redisc.RetryConn(cluster.Get(), 3, 1*time.Millisecond)
    if err != nil {
        logger.New().Error(err.Error())
    }
    return retryConn
}

func initRedisCluter() {
    cluster = &redisc.Cluster{
        StartupNodes: []string{"my_azure_redis_service"},
        DialOptions:  initRedisDialOptions(),
        CreatePool:   createPool,
    }
    // initialize its mapping
    if err := cluster.Refresh(); err != nil {
        logger.New().Error("initial cluster refresh error")
    }
}

func createPool(addr string, opts ...redis.DialOption) (*redis.Pool, error) {
    return &redis.Pool{
        MaxIdle:     16,
        MaxActive:  32,
        IdleTimeout: 100 * time.Second, 
        Dial: func() (redis.Conn, error) {
            c, err := redis.Dial("tcp", configs.Get(confGroupName, confNodeAddresses).String(""), opts...)
            if err != nil {
                return nil, err
            }
            return c, nil
        },
        TestOnBorrow: func(c redis.Conn, t time.Time) error {
            _, err := c.Do("PING")
            return err
        },
    }, nil
}
wadechen2003 commented 5 years ago

Oops, I just updated the code

from

Dial: func() (redis.Conn, error) {
            c, err := redis.Dial("tcp", configs.Get(confGroupName, confNodeAddresses).String(""), opts...)
            if err != nil {
                return nil, err
            }
            return c, nil
        },

to

Dial: func() (redis.Conn, error) {
            c, err := redis.Dial("tcp",  addr , opts...)
            if err != nil {
                return nil, err
            }
            return c, nil
        },

then it works really well...

still wonder why, thanks