Skip to content

Instantly share code, notes, and snippets.

@agonzalezro
Created September 4, 2014 14:18
Show Gist options
  • Select an option

  • Save agonzalezro/fd70ad028c7ffbb75a72 to your computer and use it in GitHub Desktop.

Select an option

Save agonzalezro/fd70ad028c7ffbb75a72 to your computer and use it in GitHub Desktop.
func timeoutDialer() (redis.Conn, error) {
type res struct {
conn redis.Conn
err error
}
resChan := make(chan res, 1)
config := settings.Config.Redis
address := fmt.Sprintf("%s:%d", config.Server.Host, config.Server.Port)
// Be pretty sure that even if the dial hangs, we properly release the socket
timer := time.NewTimer(config.ConnectTimeout)
defer timer.Stop()
go func() {
conn, err := redis.DialTimeout(
"tcp", address, config.ConnectTimeout, config.ReadTimeout, config.WriteTimeout)
resChan <- res{conn, err}
}()
select {
case <-timer.C:
return nil, errors.New("Timer timeout while contacting with Redis")
case r := <-resChan:
return r.conn, r.err
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment