-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathread_replica_set.go
More file actions
169 lines (132 loc) · 3.78 KB
/
read_replica_set.go
File metadata and controls
169 lines (132 loc) · 3.78 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package sequel
import (
"context"
"database/sql"
"errors"
"fmt"
"sync"
"github.com/go-sqlx/sqlx"
)
var ErrNoReadReplicaConnections = errors.New("no read replica connections available")
type readReplica struct {
db *sqlx.DB
next *readReplica
}
// ReadReplicaSet contains a set of DB connections. It is intended to give fair round robin access
// through a circular singly linked list.
//
// Replicas are appended after the current one. The intended use is to build the replica set before
// querying, but all operations are concurrent-safe.
type ReadReplicaSet struct {
m sync.Mutex
current *readReplica
}
// add adds a DB to the collection of read replicas
func (rr *ReadReplicaSet) add(db *sqlx.DB) {
rr.m.Lock()
defer rr.m.Unlock()
r := readReplica{
db: db,
}
// Empty ring, add new DB
if rr.current == nil {
r.next = &r
rr.current = &r
return
}
// Insert new db after current
n := rr.current.next
r.next = n
rr.current.next = &r
}
// next returns the current DB. The current pointer is advanced.
func (rr *ReadReplicaSet) next() (*sqlx.DB, error) {
rr.m.Lock()
defer rr.m.Unlock()
if rr.current == nil {
return nil, ErrNoReadReplicaConnections
}
c := rr.current
rr.current = rr.current.next
return c.db, nil
}
// Close closes all read replica connections
func (rr *ReadReplicaSet) Close() {
if rr == nil {
return
}
rr.m.Lock()
defer rr.m.Unlock()
// If this instance has no replicas, current would be nil
if rr.current == nil {
return
}
first := rr.current
for c := first; ; c = c.next {
c.db.Close()
if c.next == first {
break
}
}
}
// Query executes a query against a read replica. Queries that are not SELECTs may not work.
// The args are for any placeholder parameters in the query.
func (rr *ReadReplicaSet) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error) {
if rr == nil {
return nil, ErrNoReadReplicaConnections
}
db, err := rr.next()
if err != nil {
return nil, fmt.Errorf("did not get read replica connection: %w", err)
}
return db.QueryContext(ctx, query, args...)
}
// QueryRow executes a query that is expected to return at most one row against a read replica.
// QueryRowContext always returns a non-nil value. Errors are deferred until
// Row's Scan method is called.
//
// If the query selects no rows, the *Row's Scan will return ErrNoRows.
// Otherwise, the *Row's Scan scans the first selected row and discards the
// rest.
func (rr *ReadReplicaSet) QueryRow(ctx context.Context, query string, args ...any) (*sql.Row, error) {
if rr == nil {
return nil, ErrNoReadReplicaConnections
}
db, err := rr.next()
if err != nil {
return nil, fmt.Errorf("did not get read replica connection: %w", err)
}
return db.QueryRowContext(ctx, query, args...), nil
}
// Get populates the given model for the result of the given select query against a read replica.
func (rr *ReadReplicaSet) Get(ctx context.Context, dest Model, query string, args ...any) error {
if rr == nil {
return ErrNoReadReplicaConnections
}
db, err := rr.next()
if err != nil {
return fmt.Errorf("did not get read replica connection: %w", err)
}
return db.GetContext(ctx, dest, query, args...)
}
// GetAll populates the given destination with all the results of the given
// select query (from a read replica). The method will fail if the destination is not a pointer to a
// slice.
func (rr *ReadReplicaSet) GetAll(ctx context.Context, dest any, query string, args ...any) error {
if rr == nil {
return ErrNoReadReplicaConnections
}
db, err := rr.next()
if err != nil {
return fmt.Errorf("did not get read replica connection: %w", err)
}
rows, err := db.QueryContext(ctx, query, args...)
if err != nil {
return err
}
defer rows.Close()
if err := rows.Err(); err != nil {
return err
}
return sqlx.StructScan(rows, dest)
}