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