Skip to content

bad connection #1096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
LeeDF opened this issue May 11, 2020 · 3 comments
Closed

bad connection #1096

LeeDF opened this issue May 11, 2020 · 3 comments

Comments

@LeeDF
Copy link

LeeDF commented May 11, 2020

Issue description

bad connection
$ wrk -c300 -d10m -t8 url
SetMaxOpenConns(100)
When wrk just started, Occasional bad connection error, And the number of mysql PROCESSLIST is less than 100, around 70
I modified the file

database/sql/sql.go 

func OpenDB(c driver.Connector) *DB {
	ctx, cancel := context.WithCancel(context.Background())
	db := &DB{
		connector:    c,
		openerCh:     make(chan struct{}, connectionRequestQueueSize),
		resetterCh:   make(chan *driverConn, 50),
		lastPut:      make(map[*driverConn]string),
		connRequests: make(map[uint64]chan connRequest),
		stop:         cancel,
	}

	go db.connectionOpener(ctx)
	go db.connectionResetter(ctx)

	return db
}

Changed the buffer length of resetterCh

resetterCh:   make(chan *driverConn, 100),

Solved the bad connection problem, and the number of mysql PROCESSLIST instantly reached 100

Example code

If possible, please enter some example code here to reproduce the issue.

Error log

bad connection

Configuration

Driver version (or git SHA):

Go version: run go version in your console
1.14
Server version: E.g. MySQL 5.6, MariaDB 10.0.20

Server OS: E.g. Debian 8.1 (Jessie), Windows 10

@methane
Copy link
Member

methane commented May 12, 2020

Duplicate of golang/go#31480
Use Go 1.15.

@herry-go
Copy link

herry-go commented Jun 2, 2022

When the server resource consumption is small, a small number of errors are reported during the pressure test: the error rate is lower than 0.01%, but this error will occur every time;

qps:5000/s

image

I found that when I get a connection from the connection pool, I must judge whether the connection is expired, resulting in bad connection

in func (db *DB) conn() {
conn.inUse = true
if conn.expired(lifetime) {
db.maxLifetimeClosed++
db.mu.Unlock()
conn.Close()
return nil, driver.ErrBadConn
}
}

@fauzan05
Copy link

You must close your connection after you used. use Close() method to close your connection after you used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants