mna / redisc

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

Redisc Error - MOVED 15148 127.0.0.1:30003 #3

Closed debraj-manna closed 7 years ago

debraj-manna commented 7 years ago

I am trying to use redisc. I have followed the example given in example_test.go. My code looks like below:-

package main

import (
    "github.com/PuerkitoBio/redisc"
    "github.com/garyburd/redigo/redis"
    "log"
    "time"
)

const script = `if redis.call("EXISTS", KEYS[1]) == 1 then
    local keyvalues = redis.call("HGETALL", KEYS[1])
    local a = {}
    for i=2, table.getn(ARGV) do
      a[i-1] = ARGV[i]
    end
    local res = redis.call("HMSET", KEYS[1], unpack(a))
    redis.call("EXPIRE", KEYS[1], ARGV[1])  
    return keyvalues
else
    return 2 -- "Key doesn't exists"
end`

func main() {
    cluster := redisc.Cluster{
        StartupNodes: []string{":30001", ":30002", ":30003", ":30004", ":30005", ":30006"},
        DialOptions:  []redis.DialOption{redis.DialConnectTimeout(5 * time.Second)},
        CreatePool:   createPool,
    }
    defer cluster.Close()

    // initialize its mapping
    if err := cluster.Refresh(); err != nil {
        log.Fatalf("Refresh failed: %v", err)
    }

    // grab a connection from the pool
    conn := cluster.Get()
    defer cluster.Close()
    rScript := redis.NewScript(1, script)
    argv := make([]string, 5)
    argv[0] = "30000"
    argv[1] = "SSF_lastAccessedDate"
    argv[2] = "1481627386"
    argv[3] = "SSF_expiryDate"
    argv[4] = "2481657386"
    reply, errS := rScript.Do(conn, "JJNb324a680c35d11e6a1123c15c2d271f21481871788G", argv)
    if errS != nil {
        log.Println("Error in executing script " + errS.Error())
    } else {
        log.Printf("Result %+v", reply)
    }
}

func createPool(addr string, opts ...redis.DialOption) (*redis.Pool, error) {
    return &redis.Pool{
        MaxIdle:     100,
        MaxActive:   4000,
        IdleTimeout: time.Minute,
        Dial: func() (redis.Conn, error) {
            return redis.Dial("tcp", addr, opts...)
        },
        TestOnBorrow: func(c redis.Conn, t time.Time) error {
            if time.Since(t) < time.Minute {
                return nil
            }
            _, err := c.Do("PING")
            return err
        },
    }, nil
}

But on running the code it is throwing the below error:-

2016/12/16 12:39:37 Error in executing script MOVED 15148 127.0.0.1:30003

Can someone let me know what I am doing wrong?

debraj-manna commented 7 years ago

Figured it out. I had to use retryConn. On changing my code like below it is working

retryConn, errRe := redisc.RetryConn(conn, 3, 1*time.Millisecond)
    if errRe != nil {
        log.Println("Failed to get retry connection " + errRe.Error())
        return
    }
...

reply, errS := rScript.Do(retryConn, "JJNb324a680c35d11e6a1123c15c2d271f21481871788G", argv)