Mysql: Got "dial tcp 127.0.0.1:3306: connect: cannot assign requested address" when using multiple concurrent goroutines

Created on 17 Jan 2019  ·  3Comments  ·  Source: go-sql-driver/mysql

Issue description

I got "dial tcp 127.0.0.1:3306: connect: cannot assign requested address" after some time, when using multiple concurrent goroutines :

  • When using 8 concurrent goroutines => everything is OK
  • When using more goroutines => after around 15s, every request fails with err="dial tcp 127.0.0.1:3306: connect: cannot assign requested address". Around 35 000 requests have been successfully performed before the first failure.

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.

Example code

// 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

Error log

{"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

Configuration

*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

Most helpful comment

Try this.

db.SetMaxIdleConns(64)
db.SetMaxOpenConns(64)
db.SetConnMaxLifetime(time.Minure)

All 3 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AlekSi picture AlekSi  ·  4Comments

pmoosman picture pmoosman  ·  9Comments

BSick7 picture BSick7  ·  8Comments

tbdingqi picture tbdingqi  ·  7Comments

albrow picture albrow  ·  7Comments