Skip to content
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

Add retryable errors for deadlock and lock wait timeout #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 13 additions & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ var (
// ErrDupeKey is returned when a unique index prevents a value from being
// inserted or updated. CanRetry returns false on this error.
ErrDupeKey = errors.New("duplicate key value")

// ErrDeadlock is returned when a transaction is aborted and rolled back
// due to a deadlock. CanRetry returns true on this error.
ErrDeadlock = errors.New("transaction deadlock")

// ErrLockWaitTimeout is returned when a statement is rolled back
// after waiting too long to acquire a lock. CanRetry returns true on this error.
ErrLockWaitTimeout = errors.New("lock wait timeout expired")
)

// Error returns an error in this package if possible. The boolean return
Expand All @@ -107,6 +115,10 @@ func Error(err error) (bool, error) {
return false, err // not a MySQL error
}
switch errCode {
case 1205: // ER_LOCK_WAIT_TIMEOUT
return true, ErrLockWaitTimeout
case 1213: // ER_LOCK_DEADLOCK
return true, ErrDeadlock
case 1317: // ER_QUERY_INTERRUPTED
return true, ErrQueryKilled
case 1290, 1836: // ER_OPTION_PREVENTS_STATEMENT, ER_READ_ONLY_MODE
Expand Down Expand Up @@ -160,7 +172,7 @@ func MySQLErrorCode(err error) uint16 {
// It returns false for all other errors, including nil.
func CanRetry(err error) bool {
switch err {
case ErrCannotConnect, ErrConnLost, ErrReadOnly, ErrQueryKilled:
case ErrCannotConnect, ErrConnLost, ErrReadOnly, ErrQueryKilled, ErrLockWaitTimeout, ErrDeadlock:
return true
}
return false
Expand Down