Skip to content

Instantly share code, notes, and snippets.

@heyimalex
Created May 31, 2016 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heyimalex/23592157563765bd7bbd571b09008ad7 to your computer and use it in GitHub Desktop.
Save heyimalex/23592157563765bd7bbd571b09008ad7 to your computer and use it in GitHub Desktop.
Ping database with timeout in go
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