duanche4578 2014-09-14 09:25
浏览 51

Heroku上的Golang Redis错误

I am having a hard time on this one because I cannot reproduce it locally. Intention originally was to test if a Redis connection is accessible from a go method if so continue else inform.

In my code:

if os.Getenv("REDISTOGO_URL") != "" {
    host, password = parseRedistogoUrl()
} else {
    host = "tcp:localhost:6379"
    password = ""
}

db := 0

redis = godis.New(host, db, password)

// Try to set a "dummy" value:
err = redis.Set("INIT", time.Now().UTC())

With a Redis server available locally this executes without an error, but fails when on heroku.

What makes the problem more interesting is that if the last line (with err) is commented out then the next redis.Set in the program succeeds, which indicates that the server is accessible later on, but not "just after" the instanciation. Any ideas on how I should resolve this? Is godis.New(...) running asynchronously?

Update: This is the parsing function currently used from https://gist.github.com/TheDudeWithTheThing/6468746:

func ParseRedistogoUrl() (string, string) {
  redisUrl := os.Getenv("REDISTOGO_URL")
  redisInfo, _ := url.Parse(redisUrl)
  server := redisInfo.Host
  password := ""
  if redisInfo.User != nil {
    password, _ = redisInfo.User.Password()
  }
  return server, password
}

Rewriting the same method with redigo and https://github.com/soveran/redisurl:

// Gets redis instance from configuration parameters or environment's REDISTOGO_URL.
func redis_instance() (redis_con redis.Conn, err error) {
    if os.Getenv("REDISTOGO_URL") != "" {
        redis_con, err = redisurl.ConnectToURL(os.Getenv("REDISTOGO_URL"))
    } else {
        redis_con, err = redis.Dial("tcp", ":6379")
    }

    if err != nil {
        return
    }

    // Try to set a "dummy" value, panic if redis is not accessible.
    err = redis_con.Send("Set", "INIT", time.Now().UTC())

    return
}

and from program's main:

redis, err := redis_instance()

redis.Send("Set", "SOMETHING", "ELSE")

if err != nil {
    log.Critical("Cannot access Redis server")
    log.Critical(fmt.Sprintf("%s", err))
    os.Exit(2)
}

On logs I get the following output:

2014-09-16T18:15:10.084908+00:00 app[web.1]: 2014/09/16 18:15:10 CRITICAL003 Cannot access Redis server
2014-09-16T18:15:10.084974+00:00 app[web.1]: 2014/09/16 18:15:10 CRITICAL004 Connection error 10942

About redis access:

~ $ echo $REDISTOGO_URL
redis://redistogo:47███████████@hoki.redistogo.com:10███/
~ $ ping hoki.redistogo.com
bash: ping: command not found
~ $ nc hoki.redistogo.com 10███
flushdb
-NOAUTH Authentication required.
AUTH 47███████████
+OK
get XXX
$-1
set XXX 2
+OK
get XXX
$1
2
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等
    • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
    • ¥15 qt6.6.3 基于百度云的语音识别 不会改
    • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
    • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
    • ¥20 测距传感器数据手册i2c