-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbexec.go
More file actions
117 lines (102 loc) · 3.28 KB
/
dbexec.go
File metadata and controls
117 lines (102 loc) · 3.28 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package norm
import (
"context"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
// dbExecuter abstracts pgxpool.Pool and pgx.Tx
type dbExecuter interface {
Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error)
Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
}
// breakerExecuter wraps a dbExecuter with circuit breaker checks
type breakerExecuter struct {
kn *KintsNorm
exec dbExecuter
}
func (b breakerExecuter) Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error) {
if br := b.kn.breaker; br != nil {
if err := br.before(); err != nil {
return pgconn.CommandTag{}, err
}
tag, err := b.exec.Exec(ctx, sql, arguments...)
br.after(err)
return tag, err
}
return b.exec.Exec(ctx, sql, arguments...)
}
func (b breakerExecuter) Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error) {
if br := b.kn.breaker; br != nil {
if err := br.before(); err != nil {
return nil, err
}
rows, err := b.exec.Query(ctx, sql, args...)
br.after(err)
return rows, err
}
return b.exec.Query(ctx, sql, args...)
}
func (b breakerExecuter) QueryRow(ctx context.Context, sql string, args ...any) pgx.Row {
if br := b.kn.breaker; br != nil {
if err := br.before(); err != nil {
// emulate a Row with immediate error; pgx.Row is interface with Scan method
return errorRow{err: err}
}
row := b.exec.QueryRow(ctx, sql, args...)
return rowWithAfter{Row: row, after: func(err error) { br.after(err) }}
}
return b.exec.QueryRow(ctx, sql, args...)
}
// errorRow implements pgx.Row that always returns error on Scan
type errorRow struct{ err error }
func (e errorRow) Scan(dest ...any) error { return e.err }
// rowWithAfter wraps a Row to call a callback after Scan
type rowWithAfter struct {
pgx.Row
after func(error)
}
func (r rowWithAfter) Scan(dest ...any) error {
err := r.Row.Scan(dest...)
if r.after != nil {
r.after(err)
}
return err
}
// routingExecuter routes read operations (Query/QueryRow) to readPool when available, writes (Exec) to primary pool
type routingExecuter struct{ kn *KintsNorm }
func (r routingExecuter) Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error) {
exec := dbExecuter(r.kn.pool)
if br := r.kn.breaker; br != nil {
if err := br.before(); err != nil {
return pgconn.CommandTag{}, err
}
tag, err := exec.Exec(ctx, sql, arguments...)
br.after(err)
return tag, err
}
return exec.Exec(ctx, sql, arguments...)
}
func (r routingExecuter) Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error) {
exec := dbExecuter(r.kn.ReadPool())
if br := r.kn.breaker; br != nil {
if err := br.before(); err != nil {
return nil, err
}
rows, err := exec.Query(ctx, sql, args...)
br.after(err)
return rows, err
}
return exec.Query(ctx, sql, args...)
}
func (r routingExecuter) QueryRow(ctx context.Context, sql string, args ...any) pgx.Row {
exec := dbExecuter(r.kn.ReadPool())
if br := r.kn.breaker; br != nil {
if err := br.before(); err != nil {
return errorRow{err: err}
}
row := exec.QueryRow(ctx, sql, args...)
return rowWithAfter{Row: row, after: func(err error) { br.after(err) }}
}
return exec.QueryRow(ctx, sql, args...)
}