Skip to content

kill queries on timeout #791

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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@ type mysqlConn struct {
finished chan<- struct{}
canceled atomicError // set non-nil if conn is canceled
closed atomicBool // set when conn is closed, before closech is closed

// for killing query after timeout
id int
d MySQLDriver
}

func (mc *mysqlConn) kill() error {
t := 50 * time.Millisecond
killCfg := *mc.cfg
killCfg.Timeout = t
killCfg.ReadTimeout = t
killCfg.WriteTimeout = t

conn, err := mc.d.Open(killCfg.FormatDSN())
if err != nil {
return err
}
defer conn.Close()
query := "KILL QUERY " + strconv.Itoa(mc.id)
_, err = conn.(*mysqlConn).Exec(query, []driver.Value{})
return err
}

// Handles parameters set in DSN after the connection is established
Expand Down Expand Up @@ -445,6 +466,10 @@ func (mc *mysqlConn) getSystemVar(name string) ([]byte, error) {
// finish is called when the query has canceled.
func (mc *mysqlConn) cancel(err error) {
mc.canceled.Set(err)
if mc.cfg.KillQueryOnTimeout {
// do not put kill to cleanup to prevent cyclic kills
mc.kill()
}
mc.cleanup()
}

Expand Down
1 change: 1 addition & 0 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
return nil, err
}

mc.d = d
return mc, nil
}

Expand Down
Loading