-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoptions_datastore.go
More file actions
129 lines (110 loc) · 3.98 KB
/
options_datastore.go
File metadata and controls
129 lines (110 loc) · 3.98 KB
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
package frame
import (
"context"
"github.com/pitabwire/frame/config"
"github.com/pitabwire/frame/datastore"
"github.com/pitabwire/frame/datastore/manager"
"github.com/pitabwire/frame/datastore/pool"
)
// WithDatastoreManager creates and initializes a datastore manager with the given options.
// This is the low-level function that should rarely be called directly - use WithDatastore instead.
func WithDatastoreManager() Option {
return func(ctx context.Context, s *Service) {
s.registerPlugin("datastore")
if s.datastoreManager == nil {
var err error
s.datastoreManager, err = manager.NewManager(ctx)
if err != nil {
s.AddStartupError(err)
return
}
// Register cleanup method
s.AddCleanupMethod(func(_ context.Context) {
if s.datastoreManager != nil {
s.datastoreManager.Close(ctx)
}
})
}
}
}
// WithDatastoreConnection Option method to store a connection that will be utilized when connecting to the database.
func WithDatastoreConnection(postgresqlConnection string, readOnly bool) Option {
return WithDatastoreConnectionWithName(datastore.DefaultPoolName, postgresqlConnection, readOnly)
}
func WithDatastoreConnectionWithName(
name string,
postgresqlConnection string,
readOnly bool,
opts ...pool.Option,
) Option {
return WithDatastoreConnectionWithOptions(
name,
append(opts, pool.WithConnection(postgresqlConnection, readOnly))...)
}
func WithDatastoreConnectionWithOptions(name string, opts ...pool.Option) Option {
return func(ctx context.Context, s *Service) {
// First ensure the manager exists
dbManager := WithDatastoreManager()
dbManager(ctx, s)
dbPool := s.datastoreManager.GetPool(ctx, name)
if dbPool == nil {
dbPool = pool.NewPool(ctx)
s.datastoreManager.AddPool(ctx, name, dbPool)
}
err := dbPool.AddConnection(ctx, opts...)
if err != nil {
s.AddStartupError(err)
}
}
}
// datastoreOptsFromConfig reads datastore-related configuration from the service
// and returns enriched pool options and whether migration should run.
func datastoreOptsFromConfig(s *Service, opts []pool.Option) ([]pool.Option, bool) {
var connectionSlice []pool.Connection
doMigrate := false
traceCfg, ok := s.Config().(config.ConfigurationDatabaseTracing)
if ok {
opts = append(opts, pool.WithTraceConfig(traceCfg))
}
cfg, ok := s.Config().(config.ConfigurationDatabase)
if ok {
for _, primaryDBURL := range cfg.GetDatabasePrimaryHostURL() {
connectionSlice = append(connectionSlice, pool.Connection{DSN: primaryDBURL, ReadOnly: false})
}
for _, replicaDBURL := range cfg.GetDatabaseReplicaHostURL() {
connectionSlice = append(connectionSlice, pool.Connection{DSN: replicaDBURL, ReadOnly: true})
}
if cfg.GetMaxOpenConnections() > 0 {
opts = append(opts, pool.WithMaxOpen(cfg.GetMaxOpenConnections()))
}
if cfg.GetMaxIdleConnections() > 0 {
opts = append(opts, pool.WithMaxIdle(cfg.GetMaxIdleConnections()))
}
if cfg.GetMaxConnectionLifeTimeInSeconds() > 0 {
opts = append(opts, pool.WithMaxLifetime(cfg.GetMaxConnectionLifeTimeInSeconds()))
}
doMigrate = cfg.DoDatabaseMigrate()
}
opts = append(opts, pool.WithConnections(connectionSlice))
return opts, doMigrate
}
func WithDatastore(opts ...pool.Option) Option {
return func(ctx context.Context, s *Service) {
enrichedOpts, doMigrate := datastoreOptsFromConfig(s, opts)
// Create the manager if it doesn't exist
dbManager := WithDatastoreManager()
dbManager(ctx, s)
dbConnectionOpts := WithDatastoreConnectionWithOptions(datastore.DefaultPoolName, enrichedOpts...)
dbConnectionOpts(ctx, s)
if doMigrate {
// minor feature to automatically make a pool that can be used for db migrations
enrichedOpts = append(enrichedOpts, pool.WithPreparedStatements(false))
migrationOpts := WithDatastoreConnectionWithOptions(datastore.DefaultMigrationPoolName, enrichedOpts...)
migrationOpts(ctx, s)
}
}
}
// DatastoreManager returns the service's datastore manager.
func (s *Service) DatastoreManager() datastore.Manager {
return s.datastoreManager
}