From 971f8a2f9a5beb0473f82d7299613c86d2b4a5b9 Mon Sep 17 00:00:00 2001 From: Daniel Theophanes Date: Fri, 26 Apr 2019 14:45:46 -0700 Subject: database/sql: process all Session Resets synchronously Adds a new interface, driver.ConnectionValidator, to allow drivers to signal they should not be used again, separatly from the session resetter interface. This is done now that the session reset is done after the connection is put into the connection pool. Previous behavior attempted to run Session Resets in a background worker. This implementation had two problems: untested performance gains for additional complexity, and failures when the pool size exceeded the connection reset channel buffer size. Fixes #31480 Change-Id: I7d483b883c24a362c292471e87a88db5b204d1d0 Reviewed-on: https://go-review.googlesource.com/c/go/+/174122 Reviewed-by: Emmanuel Odeke Run-TryBot: Emmanuel Odeke TryBot-Result: Gobot Gobot --- src/database/sql/driver/driver.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'src/database/sql/driver') diff --git a/src/database/sql/driver/driver.go b/src/database/sql/driver/driver.go index 316e7cea37..a2b844d71f 100644 --- a/src/database/sql/driver/driver.go +++ b/src/database/sql/driver/driver.go @@ -255,15 +255,23 @@ type ConnBeginTx interface { // SessionResetter may be implemented by Conn to allow drivers to reset the // session state associated with the connection and to signal a bad connection. type SessionResetter interface { - // ResetSession is called while a connection is in the connection - // pool. No queries will run on this connection until this method returns. - // - // If the connection is bad this should return driver.ErrBadConn to prevent - // the connection from being returned to the connection pool. Any other - // error will be discarded. + // ResetSession is called prior to executing a query on the connection + // if the connection has been used before. If the driver returns ErrBadConn + // the connection is discarded. ResetSession(ctx context.Context) error } +// ConnectionValidator may be implemented by Conn to allow drivers to +// signal if a connection is valid or if it should be discarded. +// +// If implemented, drivers may return the underlying error from queries, +// even if the connection should be discarded by the connection pool. +type ConnectionValidator interface { + // ValidConnection is called prior to placing the connection into the + // connection pool. The connection will be discarded if false is returned. + ValidConnection() bool +} + // Result is the result of a query execution. type Result interface { // LastInsertId returns the database's auto-generated ID -- cgit v1.3