Created
May 31, 2016 19:31
-
-
Save heyimalex/23592157563765bd7bbd571b09008ad7 to your computer and use it in GitHub Desktop.
Ping database with timeout in go
This file contains 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 pingDatabase(db *sql.DB, seconds int) { | |
errc := make(chan error, 1) | |
go func() { | |
errc <- db.Ping() | |
}() | |
select { | |
case err := <-errc: | |
return err | |
case <-time.After(time.Second * seconds): | |
return fmt.Errorf("Attempt to ping database timed out after %d seconds", seconds) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment