Adding initial retry layer version (#14954)

* Adding initial retry layer version

* Some simplification around the generated code

* Generating retry layer again

* Improving naming generation in store generated layers

* Address PR review comments

* Updating store layers

* Addressing PR review comments

* fixing lint errors

* Updating store layers

* Adding license header

* Applying the retry layer to the reaction_store

* Regenerating retry layer
Этот коммит содержится в:
Jesús Espino
2020-08-12 20:05:16 +02:00
коммит произвёл GitHub
родитель c89c56ab2e
Коммит 1b141678fe
12 изменённых файлов: 9780 добавлений и 3549 удалений

Просмотреть файл

@@ -9,11 +9,7 @@ import (
"context"
"time"
"github.com/mattermost/mattermost-server/v5/mlog"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/go-sql-driver/mysql"
"github.com/pkg/errors"
)
type StoreResult struct {
@@ -775,31 +771,3 @@ type UserGetByIdsOpts struct {
// Since filters the users based on their UpdateAt timestamp.
Since int64
}
const mySQLDeadlockCode = uint16(1213)
// WithDeadlockRetry retries a given f if it throws a deadlock error.
// It breaks after a threshold and propagates the error upwards.
// TODO: This can be a separate retry layer in itself where transaction retries
// are automatically applied.
func WithDeadlockRetry(f func() error) error {
var err error
for i := 0; i < 3; i++ {
err = f()
if err == nil {
// No error, return nil.
return nil
}
// XXX: Possibly add check for postgres deadlocks later.
// But deadlocks are very rarely seen in postgres.
var mysqlErr *mysql.MySQLError
if errors.As(err, &mysqlErr) && mysqlErr.Number == mySQLDeadlockCode {
mlog.Warn("A deadlock happened. Retrying.", mlog.Err(err))
// This is a deadlock, retry.
continue
}
// Some other error, return as-is.
return err
}
return errors.Wrap(err, "giving up after 3 consecutive deadlocks")
}