Skip to content

Commit

Permalink
set wait_timeout #1052-2
Browse files Browse the repository at this point in the history
  • Loading branch information
ffffwh committed Apr 11, 2023
1 parent f97d764 commit 87c9565
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 4 additions & 0 deletions driver/mysql/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1284,6 +1284,10 @@ func (e *Extractor) mysqlDump() (retErr error) {
}
e.logger.Debug("got gtid")
}

if originVal, err := sql.GetSetWaitTimeout(tx); err != nil {
e.logger.Warn("GetSetWaitTimeout. failed. error ignored", "err", err, "originVal", originVal)
}
step++

// ------
Expand Down
25 changes: 23 additions & 2 deletions driver/mysql/sql/sqlutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

const (
ConnMaxLifetime = 300 * time.Second // #376
WaitTimeout = 300 * 60 // #376
)

// RowMap represents one row in a result set. Its objective is to allow
Expand Down Expand Up @@ -140,7 +140,7 @@ func CreateDB(mysql_uri string) (*gosql.DB, error) {
if err != nil {
return nil, err
}
db.SetConnMaxLifetime(ConnMaxLifetime)
db.SetConnMaxLifetime(WaitTimeout * time.Second)

return db, nil
}
Expand All @@ -153,6 +153,11 @@ func CreateConns(ctx context.Context, db *gosql.DB, count int) ([]*Conn, error)
return nil, err
}

originVal, err := GetSetWaitTimeout(conn)
if err != nil {
g.Logger.Warn("CreateConns. GetSetWaitTimeout. failed. error ignored", "err", err, "originVal", originVal)
}

_, err = conn.ExecContext(ctx, "SET @@session.foreign_key_checks = 0")
if err != nil {
return nil, err
Expand Down Expand Up @@ -405,3 +410,19 @@ func CloseConns(dbs ...*Conn) error {
}
return nil
}

// GetSetWaitTimeout sets session wait_timeout to `WaitTimeout` unless it is larger.
func GetSetWaitTimeout(db QueryAble) (originVal int, err error) {
row := db.QueryRowContext(context.TODO(), "select @@wait_timeout")
err = row.Scan(&originVal)
if err != nil {
return originVal, err
}
if originVal < WaitTimeout {
_, err = db.ExecContext(context.TODO(), fmt.Sprintf("set wait_timeout = %v", WaitTimeout))
if err != nil {
return originVal, err
}
}
return originVal, nil
}

0 comments on commit 87c9565

Please sign in to comment.