mirror of
https://github.com/Icinga/icingadb.git
synced 2026-05-28 04:35:54 -04:00
Add WithRetry() to DBWrapper
This commit is contained in:
parent
b3afe0385a
commit
daff13165d
1 changed files with 23 additions and 0 deletions
23
mysql.go
23
mysql.go
|
|
@ -635,6 +635,29 @@ func (dbw *DBWrapper) SqlExecQuiet(opDescription string, sql string, args ...int
|
|||
}
|
||||
}
|
||||
|
||||
return res, err
|
||||
}
|
||||
}
|
||||
|
||||
func IsRetryableError(err error) bool {
|
||||
if strings.Contains(err.Error(), "Deadlock found when trying to get lock") {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (dbw *DBWrapper) WithRetry(f func() (sql.Result, error)) (sql.Result, error) {
|
||||
for {
|
||||
res, err := f()
|
||||
|
||||
if err != nil {
|
||||
if IsRetryableError(err) {
|
||||
continue
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return res, err
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue