-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialize.go
43 lines (39 loc) · 1.17 KB
/
serialize.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
package pqx
import (
"errors"
"math/rand"
"time"
"github.com/lib/pq"
pkgerrors "github.com/pkg/errors"
)
// IsSerializationFailure returns a boolean indicating whether the error
// is 40001 (serialization_failure).
//
// It tries to unwrap err using github.com/pkg/errors.Cause() or errors.As().
func IsSerializationFailure(err error) bool {
pqErr, ok := pkgerrors.Cause(err).(*pq.Error) //nolint:errorlint // False positive.
if !ok {
ok = errors.As(err, &pqErr)
}
return ok && pqErr.Code.Name() == "serialization_failure"
}
// Serialize executes given func, which is supposed to run single
// transaction and return (possibly wrapped) error if transaction fail.
//
// It will re-execute given func for up to 10 times in case it fails
// with 40001 (serialization_failure) error.
//
// Returns value returned by last doTx call.
func Serialize(doTx func() error) error {
const (
maxTries = 10
maxDelayInMillis = 20
)
err, try := doTx(), 1
for IsSerializationFailure(err) && try < maxTries {
delay := time.Duration(rand.Intn(maxDelayInMillis)) * time.Millisecond //nolint:gosec // No need in crypto/rand..
time.Sleep(delay)
err, try = doTx(), try+1
}
return err
}