forked from heroiclabs/nakama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
96 lines (89 loc) · 3.36 KB
/
db.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2018 The Nakama Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package server
import (
"context"
"database/sql"
"github.com/jackc/pgx"
)
// Tx is used to permit clients to implement custom transaction logic.
type Tx interface {
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
Commit() error
Rollback() error
}
// Interface to help utility functions accept either *sql.Row or *sql.Rows for scanning one row at a time.
type Scannable interface {
Scan(dest ...interface{}) error
}
// Retry functions that perform non-transactional database operations.
func ExecuteRetryable(fn func() error) error {
if err := fn(); err != nil {
if dbErr, ok := err.(pgx.PgError); ok && (dbErr.Code == "CR000" || dbErr.Code == "40001") {
// A recognised error type that can be retried.
return ExecuteRetryable(fn)
}
return err
}
return nil
}
// ExecuteInTx runs fn inside tx which should already have begun.
// *WARNING*: Do not execute any statements on the supplied tx before calling this function.
// ExecuteInTx will only retry statements that are performed within the supplied
// closure (fn). Any statements performed on the tx before ExecuteInTx is invoked will *not*
// be re-run if the transaction needs to be retried.
//
// fn is subject to the same restrictions as the fn passed to ExecuteTx.
func ExecuteInTx(ctx context.Context, tx Tx, fn func() error) (err error) {
defer func() {
if err == nil {
// Ignore commit errors. The tx has already been committed by RELEASE.
_ = tx.Commit()
} else {
// We always need to execute a Rollback() so sql.DB releases the
// connection.
_ = tx.Rollback()
}
}()
// Specify that we intend to retry this txn in case of database retryable errors.
if _, err = tx.ExecContext(ctx, "SAVEPOINT cockroach_restart"); err != nil {
return err
}
for {
released := false
err = fn()
if err == nil {
// RELEASE acts like COMMIT in CockroachDB. We use it since it gives us an
// opportunity to react to retryable errors, whereas tx.Commit() doesn't.
released = true
if _, err = tx.ExecContext(ctx, "RELEASE SAVEPOINT cockroach_restart"); err == nil {
return nil
}
}
// We got an error; let's see if it's a retryable one and, if so, restart. We look
// for either the standard PG errcode SerializationFailureError:40001 or the Cockroach extension
// errcode RetriableError:CR000. The Cockroach extension has been removed server-side, but support
// for it has been left here for now to maintain backwards compatibility.
dbErr, ok := errorCause(err).(pgx.PgError)
if retryable := ok && (dbErr.Code == "CR000" || dbErr.Code == "40001"); !retryable {
if released {
err = newAmbiguousCommitError(err)
}
return err
}
if _, retryErr := tx.ExecContext(ctx, "ROLLBACK TO SAVEPOINT cockroach_restart"); retryErr != nil {
return newTxnRestartError(retryErr, err)
}
}
}