This repository has been archived by the owner on Dec 12, 2022. It is now read-only.
/
pool_manager.go
145 lines (125 loc) · 4.24 KB
/
pool_manager.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
package server
import (
"context"
"encoding/base64"
"fmt"
"log"
"sync"
"time"
"github.com/agnivade/perseus/config"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/aws/aws-sdk-go/service/kms/kmsiface"
"github.com/jackc/pgx/v5/pgconn"
)
type PoolManager struct {
mut sync.RWMutex
pools map[string]*Pool
cfg config.Config
logger *log.Logger
kms kmsiface.KMSAPI
}
func NewPoolManager(cfg config.Config, logger *log.Logger) (*PoolManager, error) {
creds := credentials.NewStaticCredentials(cfg.AWSSettings.AccessKeyId, cfg.AWSSettings.SecretAccessKey, "")
sess, err := session.NewSession(&aws.Config{
Region: aws.String(cfg.AWSSettings.Region),
Endpoint: aws.String(cfg.AWSSettings.Endpoint),
Credentials: creds,
})
if err != nil {
return nil, fmt.Errorf("error initializing AWS session: %w", err)
}
svc := kms.New(sess)
return &PoolManager{
pools: make(map[string]*Pool),
cfg: cfg,
logger: logger,
kms: svc,
}, nil
}
func (pm *PoolManager) GetOrCreatePool(row AuthRow) (pool *Pool, err error) {
// Fast path once the pool is created
pm.mut.RLock()
pool = pm.pools[row.dest_host+row.dest_db]
pm.mut.RUnlock()
if pool != nil {
return pool, nil
}
decPass, err := base64.StdEncoding.DecodeString(row.dest_pass_enc)
if err != nil {
return nil, fmt.Errorf("error decoding from base64: %w", err)
}
dec, err := pm.kms.Decrypt(&kms.DecryptInput{
CiphertextBlob: decPass,
KeyId: aws.String(pm.cfg.AWSSettings.KMSKeyARN),
})
if err != nil {
return nil, fmt.Errorf("error decrypting pass: %w", err)
}
row.dest_pass_enc = string(dec.Plaintext)
spawnConn := func(ctx context.Context) (Conner, error) {
var cancel func()
ctx, cancel = context.WithTimeout(ctx, time.Second*time.Duration(pm.cfg.PoolSettings.ConnCreateTimeoutSecs))
defer cancel()
pgConn, err := pgconn.Connect(ctx, createDSN(row))
if err != nil {
return nil, fmt.Errorf("pgconn failed to connect: %w", err)
}
// We don't hijack the connection here
// because we do need to use pgConn.Close to gracefully
// send the Terminate signal to PG. It would be cumbersome
// to wrap the hijacked connection again just to gracefully close.
// Instead we trust the code not to misuse the pgconn.
return pgConn, nil
}
pool, err = NewPool(PoolConfig{
SpawnConn: spawnConn,
Logger: pm.logger,
MaxIdle: pm.cfg.PoolSettings.MaxIdle,
MaxOpen: pm.cfg.PoolSettings.MaxOpen,
MaxLifetime: time.Second * time.Duration(pm.cfg.PoolSettings.MaxLifetimeSecs),
MaxIdleTime: time.Second * time.Duration(pm.cfg.PoolSettings.MaxIdletimeSecs),
ConnCreateTimeout: time.Second * time.Duration(pm.cfg.PoolSettings.ConnCreateTimeoutSecs),
ConnCloseTimeout: time.Second * time.Duration(pm.cfg.PoolSettings.ConnCloseTimeoutSecs),
SchemaExecTimeout: time.Second * time.Duration(pm.cfg.PoolSettings.SchemaExecTimeoutSecs),
})
if err != nil {
return nil, err
}
// Place it in the map
pm.mut.Lock()
pm.pools[row.dest_host+row.dest_db] = pool
pm.mut.Unlock()
return pool, nil
}
func (pm *PoolManager) Reload(cfg config.Config) {
pm.mut.RLock()
defer pm.mut.RUnlock()
for _, p := range pm.pools {
p.Reload(PoolConfig{
MaxIdle: cfg.PoolSettings.MaxIdle,
MaxOpen: cfg.PoolSettings.MaxOpen,
MaxLifetime: time.Second * time.Duration(cfg.PoolSettings.MaxLifetimeSecs),
MaxIdleTime: time.Second * time.Duration(cfg.PoolSettings.MaxIdletimeSecs),
ConnCreateTimeout: time.Second * time.Duration(cfg.PoolSettings.ConnCreateTimeoutSecs),
ConnCloseTimeout: time.Second * time.Duration(cfg.PoolSettings.ConnCloseTimeoutSecs),
SchemaExecTimeout: time.Second * time.Duration(cfg.PoolSettings.SchemaExecTimeoutSecs),
})
}
}
// Close closes all pools
func (pm *PoolManager) Close() error {
pm.mut.Lock()
defer pm.mut.Unlock()
var err error
for _, p := range pm.pools {
err = p.Close()
}
return err
}
func createDSN(row AuthRow) string {
// postgres://mmuser:mostest@localhost:5433/loadtest?sslmode=disable
return fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable", row.dest_user, row.dest_pass_enc, row.dest_host, row.dest_db)
}