/
config.go
68 lines (51 loc) · 1.74 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
package sql
import "time"
// Config indicates database connection configuration.
type Config struct {
// ConnMaxLifetime indicates the maximum amount of time a connection may be reused.
ConnMaxLifetime time.Duration
// ConnMaxIdleTime indicates the maximum amount of time a connection may be idle.
ConnMaxIdleTime time.Duration
// DriverName indicates the SQL driver to use, currently only supports:
// - mysql
// - postgres
DriverName DriverName
// MaxIdleConns indicates the maximum number of connections in the idle connection pool.
MaxIdleConns int
// MaxOpenConns indicates the maximum number of open connections to the database.
MaxOpenConns int
// SchemaSearchPath indicates the schema search path which is only used with "postgres".
SchemaSearchPath string
// SchemaMigrationsTable indicates the table name for storing the schema migration versions.
SchemaMigrationsTable string
// URI indicates the database connection string to connect.
//
// URI connection string documentation:
// - mysql: https://dev.mysql.com/doc/refman/8.0/en/connecting-using-uri-or-key-value-pairs.html#connecting-using-uri
// - postgres: https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING
URI string
}
func defaultConfig(c *Config) *Config {
if c.ConnMaxIdleTime == 0 {
c.ConnMaxIdleTime = 5 * time.Minute
}
if c.ConnMaxLifetime == 0 {
c.ConnMaxLifetime = 5 * time.Minute
}
if c.MaxIdleConns == 0 {
c.MaxIdleConns = 16
}
if c.MaxOpenConns == 0 {
c.MaxOpenConns = 16
}
if c.MaxIdleConns > c.MaxOpenConns {
c.MaxIdleConns = c.MaxOpenConns
}
if c.SchemaSearchPath == "" {
c.SchemaSearchPath = "public"
}
if c.SchemaMigrationsTable == "" {
c.SchemaMigrationsTable = "schema_migrations"
}
return c
}