forked from vitessio/vitess
/
connection_killer.go
50 lines (42 loc) · 1.37 KB
/
connection_killer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package tabletserver
import (
"fmt"
"time"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/vt/dbconnpool"
)
// ConnectionKiller is used for killing MySQL connections
type ConnectionKiller struct {
connPool *dbconnpool.ConnectionPool
}
// NewConnectionKiller creates a new ConnectionKiller
func NewConnectionKiller(poolSize int, idleTimeout time.Duration) *ConnectionKiller {
return &ConnectionKiller{
connPool: dbconnpool.NewConnectionPool("ConnKiller", poolSize, idleTimeout),
}
}
// Open opens the connection pool used to kill other connections
func (ck *ConnectionKiller) Open(ConnFactory dbconnpool.CreateConnectionFunc) {
ck.connPool.Open(ConnFactory)
}
// Close closes the underlying connection pool
func (ck *ConnectionKiller) Close() {
ck.connPool.Close()
}
// Kill kills the specified connection
func (ck *ConnectionKiller) Kill(connID int64) error {
killStats.Add("Queries", 1)
log.Infof("killing query %d", connID)
killConn := getOrPanic(ck.connPool)
defer killConn.Recycle()
sql := fmt.Sprintf("kill %d", connID)
var err error
if _, err = killConn.ExecuteFetch(sql, 10000, false); err != nil {
log.Errorf("Could not kill query %d: %v", connID, err)
}
return err
}
// SetIdleTimeout sets the idle timeout for the underlying connection pool
func (ck *ConnectionKiller) SetIdleTimeout(idleTimeout time.Duration) {
ck.connPool.SetIdleTimeout(idleTimeout)
}