-
Notifications
You must be signed in to change notification settings - Fork 1
/
normalize_context_cancellation_connection_pool.go
55 lines (46 loc) · 1.97 KB
/
normalize_context_cancellation_connection_pool.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
package sqldb
import (
"context"
"fmt"
"strings"
)
type NormalizeContextCancellationConnectionPool struct {
inner ConnectionPool
}
func NewNormalizeContextCancellationConnectionPool(inner ConnectionPool) *NormalizeContextCancellationConnectionPool {
return &NormalizeContextCancellationConnectionPool{inner: inner}
}
func (this *NormalizeContextCancellationConnectionPool) Ping(ctx context.Context) error {
return this.normalizeContextCancellationError(this.inner.Ping(ctx))
}
func (this *NormalizeContextCancellationConnectionPool) BeginTransaction(ctx context.Context) (Transaction, error) {
if tx, err := this.inner.BeginTransaction(ctx); err == nil {
return NewStackTraceTransaction(tx), nil
} else {
return nil, this.normalizeContextCancellationError(err)
}
}
func (this *NormalizeContextCancellationConnectionPool) Close() error {
return this.normalizeContextCancellationError(this.inner.Close())
}
func (this *NormalizeContextCancellationConnectionPool) Execute(ctx context.Context, statement string, parameters ...interface{}) (uint64, error) {
affected, err := this.inner.Execute(ctx, statement, parameters...)
return affected, this.normalizeContextCancellationError(err)
}
func (this *NormalizeContextCancellationConnectionPool) Select(ctx context.Context, query string, parameters ...interface{}) (SelectResult, error) {
result, err := this.inner.Select(ctx, query, parameters...)
return result, this.normalizeContextCancellationError(err)
}
// TODO remove manual check of "use of closed network connection" with release of https://github.com/go-sql-driver/mysql/pull/1615
func (this *NormalizeContextCancellationConnectionPool) normalizeContextCancellationError(err error) error {
if err == nil {
return nil
}
if strings.Contains(err.Error(), "operation was canceled") {
return fmt.Errorf("%w: %w", context.Canceled, err)
}
if strings.Contains(err.Error(), "use of closed network connection") {
return fmt.Errorf("%w: %w", context.Canceled, err)
}
return err
}