database - Setting tcp timeout for sql connection in Go -


when connect database (using standard go sql library) using vpn , vpn interface goes down, there's 75 seconds timeout when try sql query, no matter if interface goes meanwhile. i'd decrease timeout reasonable time, application won't frozen 75 seconds in such case.

db, err := sql.open(drivername, datasourcename) 

is possible set somehow via db variable?

starting go 1.8, sql.db abstraction accepts context.context, can used time out connections faster.

func (c *client) dolookup(ctx context.context, id int) (string, error) {   var name string   // create child context timeout   newctx, cancel := context.withtimeout(ctx, 1*time.second)   // release resources used in `newctx` if   // db operation finishes faster timeout   defer cancel()    row := c.db.queryrowcontext(newctx, "select name items id = ?", id)    err := row.scan(&name)   if err != nil {     return "", err   }    return name, nil } 

if dolookup function doesn't yet take context.context (and should!) can create parent 1 calling context.todo().


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -