-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgres_endpoint.go
149 lines (125 loc) · 2.96 KB
/
postgres_endpoint.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
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
package confpostgres
import (
"context"
"fmt"
"os"
"time"
"github.com/go-courier/envconf"
"github.com/kunlun-qilian/sqlx/v3"
"github.com/kunlun-qilian/sqlx/v3/postgresqlconnector"
)
type PostgresEndpoint struct {
Endpoint envconf.Endpoint `env:""`
SlaveEndpoint envconf.Endpoint `env:""`
DBName string `env:",opt"`
Database *sqlx.Database `env:"-"`
Extensions []string
PoolSize int
ConnMaxLifetime envconf.Duration
*sqlx.DB `env:"-"`
slaveDB *sqlx.DB `env:"-"`
}
func (m *PostgresEndpoint) LivenessCheck() map[string]string {
s := map[string]string{}
_, err := m.DB.ExecContext(context.Background(), "SELECT 1")
if err != nil {
s[m.Endpoint.Host()] = err.Error()
} else {
s[m.Endpoint.Host()] = "ok"
}
if m.slaveDB != nil {
_, err := m.slaveDB.ExecContext(context.Background(), "SELECT 1")
if err != nil {
s[m.SlaveEndpoint.Host()] = err.Error()
} else {
s[m.SlaveEndpoint.Host()] = "ok"
}
}
return s
}
func (m *PostgresEndpoint) SetDefaults() {
if m.PoolSize == 0 {
m.PoolSize = 10
}
if m.ConnMaxLifetime == 0 {
m.ConnMaxLifetime = envconf.Duration(1 * time.Hour)
}
if m.Endpoint.IsZero() {
m.Endpoint.Hostname = "127.0.0.1"
m.Endpoint.Port = 5432
}
if m.Database.Name == "" {
if len(m.Endpoint.Base) > 0 {
m.Database.Name = m.Endpoint.Base
}
if m.DBName != "" {
m.Database.Name = m.DBName
}
}
}
func (m *PostgresEndpoint) url(host string) string {
password := m.Endpoint.Password
if password != "" {
password = ":" + password
}
return fmt.Sprintf("postgres://%s%s@%s", m.Endpoint.Username, password, host)
}
func (m *PostgresEndpoint) conn(host string, readonly bool) (*sqlx.DB, error) {
connector := &postgresqlconnector.PostgreSQLConnector{
Host: m.url(host),
Extra: m.Endpoint.Extra.Encode(),
}
if !readonly {
connector.Extensions = m.Extensions
}
db := m.Database.OpenDB(connector)
db.SetMaxOpenConns(m.PoolSize)
db.SetMaxIdleConns(m.PoolSize / 2)
db.SetConnMaxLifetime(time.Duration(m.ConnMaxLifetime))
_, err := db.ExecContext(context.Background(), "SELECT 1")
if err != nil {
return nil, err
}
return db, nil
}
func (m *PostgresEndpoint) UseSlave() sqlx.DBExecutor {
if m.slaveDB != nil {
return m.slaveDB
}
return m.DB
}
func (m *PostgresEndpoint) Init() {
// 执行 dockerize 时不连接数据库
if len(os.Args) > 1 && os.Args[1] == "dockerize" {
return
}
// 若配置中指定库名,覆盖默认值
if len(m.Endpoint.Base) > 0 {
m.Database.Name = m.Endpoint.Base
}
r := Retry{Repeats: 5, Interval: envconf.Duration(1 * time.Second)}
err := r.Do(func() error {
db, err := m.conn(m.Endpoint.Host(), false)
if err != nil {
return err
}
m.DB = db
return nil
})
if err != nil {
panic(err)
}
if !m.SlaveEndpoint.IsZero() {
err := r.Do(func() error {
db, err := m.conn(m.SlaveEndpoint.Host(), true)
if err != nil {
return err
}
m.slaveDB = db
return nil
})
if err != nil {
panic(err)
}
}
}