This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
/
connection.go
104 lines (89 loc) · 3.04 KB
/
connection.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
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package database is a facade over the data storage layer.
package database
import (
"context"
"fmt"
"strings"
"time"
"github.com/google/exposure-notifications-server/pkg/logging"
"github.com/jackc/pgx/v4/pgxpool"
)
type DB struct {
Pool *pgxpool.Pool
}
// NewFromEnv sets up the database connections using the configuration in the
// process's environment variables. This should be called just once per server
// instance.
func NewFromEnv(ctx context.Context, config *Config) (*DB, error) {
logger := logging.FromContext(ctx)
logger.Infof("Creating connection pool.")
connStr := dbConnectionString(config)
pool, err := pgxpool.Connect(ctx, connStr)
if err != nil {
return nil, fmt.Errorf("creating connection pool: %v", err)
}
return &DB{Pool: pool}, nil
}
// Close releases database connections.
func (db *DB) Close(ctx context.Context) {
logger := logging.FromContext(ctx)
logger.Infof("Closing connection pool.")
db.Pool.Close()
}
// dbConnectionString builds a connection string suitable for the pgx Postgres driver, using the
// values of vars.
func dbConnectionString(config *Config) string {
vals := dbValues(config)
var p []string
for k, v := range vals {
p = append(p, fmt.Sprintf("%s=%s", k, v))
}
return strings.Join(p, " ")
}
func setIfNotEmpty(m map[string]string, key, val string) {
if val != "" {
m[key] = val
}
}
func setIfPositive(m map[string]string, key string, val int) {
if val > 0 {
m[key] = fmt.Sprintf("%d", val)
}
}
func setIfPositiveDuration(m map[string]string, key string, d time.Duration) {
if d > 0 {
m[key] = d.String()
}
}
func dbValues(config *Config) map[string]string {
p := map[string]string{}
setIfNotEmpty(p, "dbname", config.Name)
setIfNotEmpty(p, "user", config.User)
setIfNotEmpty(p, "host", config.Host)
setIfNotEmpty(p, "port", config.Port)
setIfNotEmpty(p, "sslmode", config.SSLMode)
setIfPositive(p, "connect_timeout", config.ConnectionTimeout)
setIfNotEmpty(p, "password", config.Password)
setIfNotEmpty(p, "sslcert", config.SSLCertPath)
setIfNotEmpty(p, "sslkey", config.SSLKeyPath)
setIfNotEmpty(p, "sslrootcert", config.SSLRootCertPath)
setIfNotEmpty(p, "pool_min_conns", config.PoolMinConnections)
setIfNotEmpty(p, "pool_max_conns", config.PoolMaxConnections)
setIfPositiveDuration(p, "pool_max_conn_lifetime", config.PoolMaxConnLife)
setIfPositiveDuration(p, "pool_max_conn_idle_time", config.PoolMaxConnIdle)
setIfPositiveDuration(p, "pool_health_check_period", config.PoolHealthCheck)
return p
}