golang db.ping()

db.ping() function takes about 2s to return a error if db gone offline while execution of programe. is there any way to reduce that time taken

Really no,

If you are waiting for a successful ping to happen before executing operations against the db then that's the price you have to pay!

It entirely depends on which db, which library, what's the network like etc.

From a top level, you could re-architect so that db healthchecks are not performed during user requests and then it won't really matter that the healthcheck is slow!

Or a more aggressive approach would be to add timeouts. If you know that a success ping takes 20ms then add a 40ms or 50ms timeout to the operation, and consider that to be a failure! Fail fast!

I used go-sql-driver/mysql library: https://github.com/go-sql-driver/mysql

has this configuration options

  • TimeOut
  • ReadTimeOut
  • WriteTimeOut

by changing its values actually in my case TimeOut. I could reduce timeOut time to user defined time