Skip to content

Commit 2c780b2

Browse files
author
Artem Klyukvin
committed
kill queries on timeout
1 parent 3287d94 commit 2c780b2

File tree

5 files changed

+212
-80
lines changed

5 files changed

+212
-80
lines changed

connection.go

+18
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@ type mysqlConn struct {
4848
finished chan<- struct{}
4949
canceled atomicError // set non-nil if conn is canceled
5050
closed atomicBool // set when conn is closed, before closech is closed
51+
52+
// for killing query after timeout
53+
id int
54+
d MySQLDriver
55+
dsn string
56+
}
57+
58+
func (mc *mysqlConn) kill() error {
59+
conn, err := mc.d.Open(mc.dsn)
60+
if err != nil {
61+
return err
62+
}
63+
defer conn.Close()
64+
query := "KILL QUERY " + strconv.Itoa(mc.id)
65+
_, err = conn.(*mysqlConn).Exec(query, []driver.Value{})
66+
return err
5167
}
5268

5369
// Handles parameters set in DSN after the connection is established
@@ -445,6 +461,8 @@ func (mc *mysqlConn) getSystemVar(name string) ([]byte, error) {
445461
// finish is called when the query has canceled.
446462
func (mc *mysqlConn) cancel(err error) {
447463
mc.canceled.Set(err)
464+
// do not put kill to cleanup to prevent cyclic kills
465+
mc.kill()
448466
mc.cleanup()
449467
}
450468

connection_go18.go

+4
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ func (mc *mysqlConn) watchCancel(ctx context.Context) error {
160160
select {
161161
default:
162162
case <-ctx.Done():
163+
killErr := mc.kill()
164+
if killErr != nil {
165+
errLog.Print("failed to kill query: ", killErr)
166+
}
163167
return ctx.Err()
164168
}
165169
if mc.watcher == nil {

driver.go

+3
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
150150
return nil, err
151151
}
152152

153+
mc.d = d
154+
mc.dsn = dsn
155+
153156
return mc, nil
154157
}
155158

0 commit comments

Comments
 (0)