-
Notifications
You must be signed in to change notification settings - Fork 7
/
config.go
102 lines (85 loc) · 2.82 KB
/
config.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
90
91
92
93
94
95
96
97
98
99
100
101
102
package sqlconn
import (
"database/sql"
"fmt"
"strings"
"sync"
"time"
)
const (
defaultConnMaxIdleCount = 2 // Maximum number of idle connections (0 = unlimited)
defaultConnMaxIdleTime = 1 * time.Minute // Maximum amount of time a connection may be idle before being closed
defaultConnMaxLifetime = 1 * time.Hour // Maximum amount of time a connection may be reused (0 = unlimited reuse)
defaultConnMaxOpenCount = 5 // Maximum number of open connections (0 = unlimited connections)
defaultPingTimeout = 5 * time.Second // Healthcheck ping timeout
)
type config struct {
checkConnectionFunc CheckConnectionFunc
sqlOpenFunc SQLOpenFunc
connectFunc ConnectFunc
connMaxIdleTime time.Duration
connMaxLifetime time.Duration
connMaxIdleCount int
connMaxOpenCount int
driver string
dsn string
pingTimeout time.Duration
shutdownWaitGroup *sync.WaitGroup
shutdownSignalChan chan struct{}
}
func defaultConfig(driver, dsn string) *config {
return &config{
checkConnectionFunc: checkConnection,
sqlOpenFunc: sql.Open,
connectFunc: connectWithBackoff,
connMaxIdleCount: defaultConnMaxIdleCount,
connMaxIdleTime: defaultConnMaxIdleTime,
connMaxLifetime: defaultConnMaxLifetime,
connMaxOpenCount: defaultConnMaxOpenCount,
driver: driver,
dsn: dsn,
pingTimeout: defaultPingTimeout,
shutdownWaitGroup: &sync.WaitGroup{},
shutdownSignalChan: make(chan struct{}),
}
}
//nolint:gocyclo,gocognit
func (c *config) validate() error {
if strings.TrimSpace(c.driver) == "" {
return fmt.Errorf("database driver must be set")
}
if strings.TrimSpace(c.dsn) == "" {
return fmt.Errorf("database DSN must be set")
}
if c.connectFunc == nil {
return fmt.Errorf("database connect function must be set")
}
if c.checkConnectionFunc == nil {
return fmt.Errorf("check connection function must be set")
}
if c.sqlOpenFunc == nil {
return fmt.Errorf("sql open function must be set")
}
if c.connMaxIdleCount < 1 {
return fmt.Errorf("database pool max idle connections must be greater than 0")
}
if c.connMaxIdleTime < 1*time.Second {
return fmt.Errorf("database connect retry interval must be at least 1 second")
}
if c.connMaxLifetime < 1*time.Second {
return fmt.Errorf("database connection max lifetime must be at least 1 second")
}
if c.connMaxOpenCount < 1 {
return fmt.Errorf("database pool max open connections must be greater than 0")
}
if c.pingTimeout < 1*time.Second {
return fmt.Errorf("database ping timeout must be at least 1 second")
}
if c.shutdownWaitGroup == nil {
return fmt.Errorf("shutdownWaitGroup is required")
}
if c.shutdownSignalChan == nil {
return fmt.Errorf("shutdownSignalChan is required")
}
return nil
}