-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.go
More file actions
118 lines (110 loc) · 2.66 KB
/
connection.go
File metadata and controls
118 lines (110 loc) · 2.66 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
package norm
import (
"context"
"errors"
"math/rand/v2"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
func newPool(ctx context.Context, cfg *Config) (*pgxpool.Pool, error) {
if cfg == nil {
return nil, errors.New("nil config")
}
conf, err := pgxpool.ParseConfig(cfg.ConnString())
if err != nil {
return nil, err
}
if cfg.MaxConnections > 0 {
conf.MaxConns = cfg.MaxConnections
}
if cfg.MinConnections > 0 {
conf.MinConns = cfg.MinConnections
}
if cfg.MaxConnLifetime > 0 {
conf.MaxConnLifetime = cfg.MaxConnLifetime
}
if cfg.MaxConnIdleTime > 0 {
conf.MaxConnIdleTime = cfg.MaxConnIdleTime
}
if cfg.HealthCheckPeriod > 0 {
conf.HealthCheckPeriod = cfg.HealthCheckPeriod
}
if cfg.StatementCacheCapacity > 0 {
conf.ConnConfig.DefaultQueryExecMode = pgx.QueryExecModeCacheStatement
conf.ConnConfig.StatementCacheCapacity = cfg.StatementCacheCapacity
}
pool, err := pgxpool.NewWithConfig(ctx, conf)
if err != nil {
return nil, err
}
return pool, nil
}
func newPoolFromConnString(ctx context.Context, connString string) (*pgxpool.Pool, error) {
conf, err := pgxpool.ParseConfig(connString)
if err != nil {
return nil, err
}
pool, err := pgxpool.NewWithConfig(ctx, conf)
if err != nil {
return nil, err
}
return pool, nil
}
func healthCheck(ctx context.Context, pool *pgxpool.Pool) error {
if pool == nil {
return errors.New("nil pool")
}
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()
var one int
if err := pool.QueryRow(ctx, "select 1").Scan(&one); err != nil {
return err
}
if one != 1 {
return errors.New("health check failed")
}
return nil
}
// withRetry executes fn with basic retry on transient errors
func (kn *KintsNorm) withRetry(ctx context.Context, fn func() error) error {
// Circuit check is handled at executor-level; do not duplicate here
attempts := 0
baseBackoff := 0 * time.Millisecond
if kn.config != nil {
attempts = kn.config.RetryAttempts
baseBackoff = kn.config.RetryBackoff
}
if attempts <= 0 {
return fn()
}
var err error
for i := 0; i < attempts; i++ {
// allow external cancellation between attempts
select {
case <-ctx.Done():
return ctx.Err()
default:
}
err = fn()
if err == nil {
return nil
}
if i < attempts-1 && baseBackoff > 0 {
// exponential backoff with jitter
sleep := baseBackoff << i
// cap to 5 seconds
sleep = min(sleep, 5*time.Second)
// random jitter: +/- 25%
jitter := time.Duration(rand.Int64N(int64(sleep) / 2))
delay := sleep - sleep/4 + jitter
// respect context during backoff wait
select {
case <-time.After(delay):
case <-ctx.Done():
return ctx.Err()
}
}
}
return err
}