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 1 commit
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
18 changes: 18 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ 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
dsn string
}

func (mc *mysqlConn) kill() error {
conn, err := mc.d.Open(mc.dsn)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't creating new connection pool! Create mysqlConn directly.
Addtionally, opening new connection should be have short timeout.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree about timeout but didn't catch your idea about connection pool. Here I'm using MySQLDriver.Open method, which returns driver.Conn and error (underlying type of driver.Conn is *mysqlConn)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry, I was wrong about the pool.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#705 should be used instead of Driver.Open.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, will use after it merged to master

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 +461,8 @@ 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)
// do not put kill to cleanup to prevent cyclic kills
mc.kill()
mc.cleanup()
}

Expand Down
4 changes: 4 additions & 0 deletions connection_go18.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ func (mc *mysqlConn) watchCancel(ctx context.Context) error {
select {
default:
case <-ctx.Done():
killErr := mc.kill()
if killErr != nil {
errLog.Print("failed to kill query: ", killErr)
}
return ctx.Err()
}
if mc.watcher == nil {
Expand Down
3 changes: 3 additions & 0 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
return nil, err
}

mc.d = d
mc.dsn = dsn

return mc, nil
}

Expand Down
Loading