Skip to content

database/sql: db.PingContext takes longer to time out on certain database host IPs than on others (postgresql and mysql) #27476

@adavea

Description

@adavea

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go1.10.1 linux/amd64

Does this issue reproduce with the latest release?

Yes, at least on go1.10.4 linux/amd64

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOHOSTARCH="amd64"
GOHOSTOS="linux"

What did you do?

Attempted to use context.WithTimeout to limit the amount of time a PingContext() command takes to try and access a database server.

package main

//Extended timeout will reproduce for both msyql and postgres

import (
	"context"
	"database/sql"
	"log"
	"time"

	//_ "github.com/go-sql-driver/mysql" //MySQL driver produces same issue
	_ "github.com/lib/pq" //Postgresql driver produces same issue
)

func main() {
	log.Print("Trying to ping database!")

	//Prepare a "context" to execute queries in, this will allow us to use timeouts
	var bgCtx = context.Background()
	var ctx2SecondTimeout, cancelFunc2SecondTimeout = context.WithTimeout(bgCtx, time.Second*2)
	defer cancelFunc2SecondTimeout()

	//Open  database connection
	//db, err := sql.Open("mysql", "root:@tcp(172.1.2.3)/testdb?parseTime=true")
	db, err := sql.Open("postgres", "host=172.1.2.3 user=a password=b dbname=c")
	if err != nil {
		log.Printf("Unable to open db, %s", err.Error())
		return
	}
	log.Print("Successfully called open()")

	//Ping database connection with 2 second timeout
	err = db.PingContext(ctx2SecondTimeout)
	if err != nil {
		log.Printf("Can't ping database server in order to use storage, %s", err.Error())
		return
	}
	log.Print("Successfully pinged database!")
}

What did you expect to see?

A timeout of 2- seconds for 172.1.2.3
A timeout of 2- seconds for 541.1.2.3

What did you see instead?

A timeout of 2+ minutes for 172.1.2.3
(An immediate timeout for 541.1.2.3, which is ok)

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions