Created
September 4, 2014 14:18
-
-
Save agonzalezro/fd70ad028c7ffbb75a72 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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