|
1
|
|
|
package postgres |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"time" |
|
5
|
|
|
) |
|
6
|
|
|
|
|
7
|
|
|
// Option - Option type |
|
8
|
|
|
type Option func(*Postgres) |
|
9
|
|
|
|
|
10
|
|
|
// MaxOpenConnections - Deprecated: use MaxConns instead for consistency with pgxpool. |
|
11
|
|
|
// Kept for backward compatibility and internally forwards to MaxConns. |
|
12
|
|
|
func MaxOpenConnections(size int) Option { |
|
13
|
|
|
return MaxConns(size) |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
// MaxConns - Defines maximum number of connections in the pool (maps to pgxpool MaxConns) |
|
17
|
|
|
func MaxConns(size int) Option { |
|
18
|
|
|
return func(c *Postgres) { |
|
19
|
|
|
c.maxConns = size |
|
20
|
|
|
} |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
// MaxIdleConnections - Deprecated: use MinConns instead. |
|
24
|
|
|
// Kept for backward compatibility and only used as a fallback for MinConns when |
|
25
|
|
|
// MinConns is not set (0). MinIdleConns is only honored when explicitly configured. |
|
26
|
|
|
func MaxIdleConnections(c int) Option { |
|
27
|
|
|
return func(p *Postgres) { |
|
28
|
|
|
p.maxIdleConnections = c |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
// MinConns - Defines minimum number of connections in the pool |
|
33
|
|
|
// If not set (0) and MaxIdleConnections is set, MaxIdleConnections will be used for backward compatibility (old behavior). |
|
34
|
|
|
func MinConns(c int) Option { |
|
35
|
|
|
return func(p *Postgres) { |
|
36
|
|
|
p.minConns = c |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
// MinIdleConns - Defines minimum number of idle connections in the pool. |
|
41
|
|
|
// This is superior to MinConns for ensuring idle connections are always available. |
|
42
|
|
|
// Note: MaxIdleConnections only affects MinConns when MinConns is not set; it does |
|
43
|
|
|
// not implicitly set MinIdleConns. |
|
44
|
|
|
func MinIdleConns(c int) Option { |
|
45
|
|
|
return func(p *Postgres) { |
|
46
|
|
|
p.minIdleConns = c |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// MaxConnectionIdleTime - Defines maximum connection idle for postgresql db |
|
51
|
|
|
func MaxConnectionIdleTime(d time.Duration) Option { |
|
52
|
|
|
return func(p *Postgres) { |
|
53
|
|
|
p.maxConnectionIdleTime = d |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// MaxConnectionLifeTime - Defines maximum connection lifetime for postgresql db |
|
58
|
|
|
func MaxConnectionLifeTime(d time.Duration) Option { |
|
59
|
|
|
return func(p *Postgres) { |
|
60
|
|
|
p.maxConnectionLifeTime = d |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// HealthCheckPeriod - Defines the period between health checks on idle connections |
|
65
|
|
|
func HealthCheckPeriod(d time.Duration) Option { |
|
66
|
|
|
return func(p *Postgres) { |
|
67
|
|
|
p.healthCheckPeriod = d |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// MaxConnLifetimeJitter - Defines the jitter added to MaxConnLifetime to prevent all connections from expiring at once |
|
72
|
|
|
func MaxConnLifetimeJitter(d time.Duration) Option { |
|
73
|
|
|
return func(p *Postgres) { |
|
74
|
|
|
p.maxConnLifetimeJitter = d |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// ConnectTimeout - Defines the maximum time to wait when establishing a new connection |
|
79
|
|
|
func ConnectTimeout(d time.Duration) Option { |
|
80
|
|
|
return func(p *Postgres) { |
|
81
|
|
|
p.connectTimeout = d |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
func MaxDataPerWrite(v int) Option { |
|
86
|
|
|
return func(c *Postgres) { |
|
87
|
|
|
c.maxDataPerWrite = v |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
func WatchBufferSize(v int) Option { |
|
92
|
|
|
return func(c *Postgres) { |
|
93
|
|
|
c.watchBufferSize = v |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
func MaxRetries(v int) Option { |
|
98
|
|
|
return func(c *Postgres) { |
|
99
|
|
|
c.maxRetries = v |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|