I got "dial tcp 127.0.0.1:3306: connect: cannot assign requested address" after some time, when using multiple concurrent goroutines :
Looks like the sql driver can't obtain a new port to establish a new connection, but if so I don't understand why it doesn't reuse an existing connection from the pool ?
I have the following DBStats when the error happens:
(sql.DBStats) {
MaxOpenConnections: (int) 0,
OpenConnections: (int) 64,
InUse: (int) 64,
Idle: (int) 0,
WaitCount: (int64) 0,
WaitDuration: (time.Duration) 0s,
MaxIdleClosed: (int64) 28252,
MaxLifetimeClosed: (int64) 0
}
I use go-sql-driver V1.4.1 with go 1.11.2, to reach a mariaDB running locally as a docker container.
I the example below I added the "panic" only to not be flooded by a bunch of similar errors.
// the below code is called repetitly from a configurable number of worker fed using channels (fan-out)
// Mark visa_card entry as migrated
query := fmt.Sprintf(UPDATE visa_card SET migrated=TRUE WHERE id_card=%s, this.idCard)
_, err = conf.MySql.ExecContext(ctx, query)
if err != nil {
log.Error(ctx, log.EventTypeBusiness, "", err)
spew.Dump(conf.MySql.Stats())
panic("the END")
}
return err
{"level":"info","dateTime":"2019-01-17T14:58:04.095+0100","msg":"","appVersion":"0.0.0","component":"VISA-MIGRATOR","country":"bw","eventType":"incoming request","requestId":"d25e58f8-8228-4a7f-9cc4-1fd9ea2230b4","useCase":"","parameters":{"request_headers":{},"request_http_method":"POST","request_uri":"/startImportActiveCards"},"caller":"common/utils/log/ginLog.go:70"}
{"level":"error","dateTime":"2019-01-17T14:58:20.562+0100","msg":"","appVersion":"0.0.0","component":"VISA-MIGRATOR","country":"bw","eventType":"internal","requestId":"d25e58f8-8228-4a7f-9cc4-1fd9ea2230b4","useCase":"startMigration","peer":"internal","caller":"migrator/migrate/migrateActiveCards.go:211","errorDetails":"dial tcp 127.0.0.1:3306: connect: cannot assign requested address"}
(sql.DBStats) {
MaxOpenConnections: (int) 0,
OpenConnections: (int) 50,
InUse: (int) 50,
Idle: (int) 0,
WaitCount: (int64) 0,
WaitDuration: (time.Duration) 0s,
MaxIdleClosed: (int64) 28258,
MaxLifetimeClosed: (int64) 0
}
panic: the END
*go-sql-driver V1.4.1
*Go version: go version go1.11.2 linux/amd64
*MariaDB 10.4.1 in docker container
Server OS: E.g. Debian 8.1 (Jessie), Windows 10
Try this.
db.SetMaxIdleConns(64)
db.SetMaxOpenConns(64)
db.SetConnMaxLifetime(time.Minure)
Thanks a lot @methane for your very quick response, trial is successful !
I'm still a bit surprised that the default values don't work correctly and I don't understand why but indeed you solved my problem.
See this article.
http://techblog.en.klab-blogs.com/archives/31093990.html
All images in the article are disappeared. They are alive in the Japanese version of the article.
http://dsas.blog.klab.org/archives/2018-02/configure-sql-db.html
Most helpful comment
Try this.
db.SetMaxIdleConns(64)
db.SetMaxOpenConns(64)
db.SetConnMaxLifetime(time.Minure)