-
Notifications
You must be signed in to change notification settings - Fork 7
/
options.go
89 lines (76 loc) · 2.26 KB
/
options.go
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
package sqlconn
import (
"sync"
"time"
)
// Option is a type alias for a function that configures the DB connector.
type Option func(*config)
// WithConnectFunc replaces the default connection function.
func WithConnectFunc(fn ConnectFunc) Option {
return func(cfg *config) {
cfg.connectFunc = fn
}
}
// WithCheckConnectionFunc replaces the default connection check function.
func WithCheckConnectionFunc(fn CheckConnectionFunc) Option {
return func(cfg *config) {
cfg.checkConnectionFunc = fn
}
}
// WithSQLOpenFunc replaces the default open database function.
func WithSQLOpenFunc(fn SQLOpenFunc) Option {
return func(cfg *config) {
cfg.sqlOpenFunc = fn
}
}
// WithConnMaxIdleCount sets the maximum number of idle database connections.
func WithConnMaxIdleCount(maxIdle int) Option {
return func(cfg *config) {
cfg.connMaxIdleCount = maxIdle
}
}
// WithConnMaxIdleTime sets the maximum idle time of a database connection.
func WithConnMaxIdleTime(t time.Duration) Option {
return func(cfg *config) {
cfg.connMaxIdleTime = t
}
}
// WithConnMaxLifetime sets the maximum lifetime of a database connection.
func WithConnMaxLifetime(t time.Duration) Option {
return func(cfg *config) {
cfg.connMaxLifetime = t
}
}
// WithConnMaxOpen sets the maximum number of open database connections.
func WithConnMaxOpen(maxOpen int) Option {
return func(cfg *config) {
cfg.connMaxOpenCount = maxOpen
}
}
// WithDefaultDriver sets the default driver to use if not included in the DSN.
func WithDefaultDriver(driver string) Option {
return func(cfg *config) {
if cfg.driver == "" {
cfg.driver = driver
}
}
}
// WithPingTimeout sets the healthcheck ping timeout.
func WithPingTimeout(t time.Duration) Option {
return func(cfg *config) {
cfg.pingTimeout = t
}
}
// WithShutdownWaitGroup sets the shared waiting group to communicate externally when the databse connection is closed.
func WithShutdownWaitGroup(wg *sync.WaitGroup) Option {
return func(cfg *config) {
cfg.shutdownWaitGroup = wg
}
}
// WithShutdownSignalChan sets the shared channel uset to signal a shutdown.
// When the channel signal is received the database connection will be closed.
func WithShutdownSignalChan(ch chan struct{}) Option {
return func(cfg *config) {
cfg.shutdownSignalChan = ch
}
}