-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpgx_pool.go
More file actions
318 lines (255 loc) · 7.06 KB
/
Copy pathpgx_pool.go
File metadata and controls
318 lines (255 loc) · 7.06 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package serialkey
import (
"context"
"errors"
"fmt"
"sync"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
// NewPgxPool returns the serialkeys keychain based on the pgx pool.
func NewPgxPool(pool *pgxpool.Pool, opts ...PgxPoolOption) *PgxPool {
cfg := PgxPoolConfiguration{table: Table}
for _, opt := range opts {
opt(&cfg)
}
return &PgxPool{
start: cfg.start,
table: cfg.table,
pool: pool,
}
}
// PgxPool is the serialkeys keychain based on the pgx pool.
type PgxPool struct {
sync.RWMutex
start int64
table string
pool *pgxpool.Pool
nextQuery string
nextNQuery string
lastQuery string
forwardQuery string
created bool
closed bool
}
// Next for the passed key name returns an value guaranteed to be greater
// than the value returned for the same key name passed at the time
// of previous call of the next method or the forward method.
// The next method is thread safe.
func (chain *PgxPool) Next(ctx context.Context, key string) (int64, error) {
chain.RLock()
if chain.nextQuery != "" {
value, err := chain.next(ctx, key)
chain.RUnlock()
return value, err
}
chain.RUnlock()
chain.Lock()
defer chain.Unlock()
if chain.nextQuery == "" {
q, err := PostgreSQL{Table: chain.table}.next()
if err != nil {
return 0, fmt.Errorf("generate the next value fetching query: %w", err)
}
chain.nextQuery = q
}
return chain.next(ctx, key)
}
func (chain *PgxPool) next(ctx context.Context, key string) (int64, error) {
conn, err := chain.conn(ctx)
if err != nil {
return 0, err
}
defer conn.Release()
var value int64
err = conn.QueryRow(ctx, chain.nextQuery, key, chain.start).Scan(&value)
if err != nil {
return 0, fmt.Errorf("fetch next value %s: %w", key, err)
}
return value, nil
}
// NextN for the passed key name returns an value guaranteed
// to be greater than the returned value for the same key name
// passed at the time of the previous method call.
// The next method is thread safe.
//
// TODO: Replace NextN method by CopyNext method.
func (chain *PgxPool) NextN(ctx context.Context, key string, count int64) (int64, error) {
chain.RLock()
if chain.nextNQuery != "" {
value, err := chain.nextN(ctx, key, count)
chain.RUnlock()
return value, err
}
chain.RUnlock()
chain.Lock()
defer chain.Unlock()
if chain.nextNQuery == "" {
q, err := PostgreSQL{Table: chain.table}.nextN()
if err != nil {
return 0, fmt.Errorf("generate the next N values fetching query: %w", err)
}
chain.nextNQuery = q
}
return chain.nextN(ctx, key, count)
}
func (chain *PgxPool) nextN(ctx context.Context, key string, count int64) (int64, error) {
conn, err := chain.conn(ctx)
if err != nil {
return 0, err
}
defer conn.Release()
var value int64
err = conn.QueryRow(ctx, chain.nextNQuery, key, count).Scan(&value)
if err != nil {
return 0, fmt.Errorf("fetch next values %s: %w", key, err)
}
return value, nil
}
// Last for the passed key name returns the value returned for
// the same key name passed at the time of previous call
// of the next method or the forward method.
// Last method must be thread safe.
func (chain *PgxPool) Last(ctx context.Context, key string) (int64, error) {
chain.RLock()
if chain.lastQuery != "" {
value, err := chain.last(ctx, key)
chain.RUnlock()
return value, err
}
chain.RUnlock()
chain.Lock()
defer chain.Unlock()
if chain.lastQuery == "" {
q, err := PostgreSQL{Table: chain.table}.last()
if err != nil {
return 0, fmt.Errorf("generate the last value fetching query: %w", err)
}
chain.lastQuery = q
}
return chain.last(ctx, key)
}
func (chain *PgxPool) last(ctx context.Context, key string) (int64, error) {
conn, err := chain.conn(ctx)
if err != nil {
return 0, err
}
defer conn.Release()
var value int64
err = conn.QueryRow(ctx, chain.lastQuery, key).Scan(&value)
if errors.Is(err, pgx.ErrNoRows) {
return chain.start - 1, nil
} else if err != nil {
return 0, fmt.Errorf("fetch last value %s: %w", key, err)
}
return value, nil
}
// Forward for the passed key name returns an value guaranteed
// to be greater or equal to the target value and guaranteed to be greater
// than the value returned for the same key name passed at the time
// of previous call of the forward method or the next method.
// Forward method is thread safe.
func (chain *PgxPool) Forward(ctx context.Context, key string, target int64) (int64, error) {
chain.RLock()
if chain.forwardQuery != "" {
value, err := chain.forward(ctx, key, target)
chain.RUnlock()
return value, err
}
chain.RUnlock()
chain.Lock()
defer chain.Unlock()
if chain.forwardQuery == "" {
q, err := PostgreSQL{Table: chain.table}.forward()
if err != nil {
return 0, fmt.Errorf("generate the forward value query: %w", err)
}
chain.forwardQuery = q
}
return chain.forward(ctx, key, target)
}
func (chain *PgxPool) forward(ctx context.Context, key string, target int64) (int64, error) {
conn, err := chain.conn(ctx)
if err != nil {
return 0, err
}
defer conn.Release()
var value int64
err = conn.QueryRow(ctx, chain.forwardQuery, key, target).Scan(&value)
if err != nil {
return 0, fmt.Errorf("forward value %s to %d: %w", key, target, err)
}
return value, nil
}
// CreateTable creates the PostgreSQL table if not exists.
// The create table method is thread safe.
func (chain *PgxPool) CreateTable(ctx context.Context) error {
chain.RLock()
if chain.created {
chain.RUnlock()
return nil
}
chain.RUnlock()
chain.Lock()
defer chain.Unlock()
if !chain.created {
q, err := PostgreSQL{Table: chain.table}.createTable()
if err != nil {
return fmt.Errorf("generate the table creation query: %w", err)
}
conn, err := chain.conn(ctx)
if err != nil {
return err
}
defer conn.Release()
_, err = conn.Exec(ctx, q)
if err != nil {
return fmt.Errorf("execute the table creation query: %w", err)
}
chain.created = true
}
return nil
}
func (chain *PgxPool) conn(ctx context.Context) (*pgxpool.Conn, error) {
conn, err := chain.pool.Acquire(ctx)
if err != nil {
return nil, fmt.Errorf("acquire connection: %w", err)
}
return conn, nil
}
// Close closes pgx pool.
// The close method is thread safe.
func (chain *PgxPool) Close() error {
chain.RLock()
if chain.closed {
chain.RUnlock()
return nil
}
chain.RUnlock()
chain.Lock()
defer chain.Unlock()
if chain.closed {
return nil
}
chain.pool.Close()
chain.closed = true
return nil
}
// PgxPoolOption changes configuration.
type PgxPoolOption func(*PgxPoolConfiguration)
// PgxPoolConfiguration holds values changeable by options.
type PgxPoolConfiguration struct {
start int64
table string
}
// PgxPoolWithStart sets the start number.
func PgxPoolWithStart(start int64) PgxPoolOption {
return func(cfg *PgxPoolConfiguration) { cfg.start = start }
}
// PgxPoolWithTable sets the table name.
func PgxPoolWithTable(table string) PgxPoolOption {
return func(cfg *PgxPoolConfiguration) { cfg.table = table }
}