Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions pkg/db/db_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"sync"
"sync/atomic"
"time"

"github.com/rs/zerolog"
Expand All @@ -14,7 +15,7 @@ const databaseDriver = "postgres"
// instance holds a single instance of the database
var instance *ReaderWriter

var ready bool
var ready atomic.Bool

// once is used to ensure that there is only a single instance of the database
var once sync.Once
Expand All @@ -23,7 +24,7 @@ var once sync.Once
type Store struct {
db func() *sql.DB
dbs *ReaderWriter
ready *bool
ready *atomic.Bool
}

// NewDbConnectionFromSettings sets up a db connection from the settings, only once
Expand Down Expand Up @@ -60,7 +61,7 @@ func NewDbConnectionFromSettings(ctx context.Context, settings *Settings, withSe

// IsReady returns if db is ready to connect to
func (store *Store) IsReady() bool {
return *store.ready
return store.ready.Load()
}

// DBS returns the reader and writer databases to connect to
Expand All @@ -70,7 +71,7 @@ func (store *Store) DBS() *ReaderWriter {

// NewDbConnectionForTest use this for tests as we have multiple sessions in parallel and don't want the synced one
func NewDbConnectionForTest(ctx context.Context, settings *Settings, withSearchPath bool) Store {
localReady := false
localReady := atomic.Bool{}
dbConnection := NewDbConnection(
ctx,
&localReady,
Expand Down
7 changes: 4 additions & 3 deletions pkg/db/db_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"strings"
"sync"
"sync/atomic"
"time"

_ "github.com/lib/pq" // concrete implementation of database
Expand Down Expand Up @@ -43,10 +44,10 @@ type ReaderWriter struct {
var dbs *ReaderWriter

// NewDbConnection connects to the reader and writer per passed in options, with retries, returning a DBReaderWriter object that contains sql.DB connection
func NewDbConnection(ctx context.Context, ready *bool, ro ConnectOptions, wo ConnectOptions) *ReaderWriter {
func NewDbConnection(ctx context.Context, ready *atomic.Bool, ro ConnectOptions, wo ConnectOptions) *ReaderWriter {
dbs = &ReaderWriter{Reader: &DB{}, Writer: &DB{}}

go func(ctx context.Context, ready *bool, dbs *ReaderWriter) {
go func(ctx context.Context, ready *atomic.Bool, dbs *ReaderWriter) {
errCh := make(chan error)
defer close(errCh)

Expand Down Expand Up @@ -74,7 +75,7 @@ func NewDbConnection(ctx context.Context, ready *bool, ro ConnectOptions, wo Con
rCancel()
wCancel()
wg.Wait()
*ready = r
ready.Store(r)
}

rCount := 0
Expand Down
16 changes: 8 additions & 8 deletions pkg/db/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ const (

// Settings connection settings to postgres db
type Settings struct {
User string `yaml:"USER"`
Password string `yaml:"PASSWORD"`
Port string `yaml:"PORT"`
Host string `yaml:"HOST"`
Name string `yaml:"NAME"`
MaxOpenConnections int `yaml:"MAX_OPEN_CONNECTIONS"`
MaxIdleConnections int `yaml:"MAX_IDLE_CONNECTIONS"`
SSLMode string `yaml:"SSL_MODE"`
User string `env:"USER" yaml:"USER"`
Password string `env:"PASSWORD" yaml:"PASSWORD"`
Port string `env:"PORT" yaml:"PORT"`
Host string `env:"HOST" yaml:"HOST"`
Name string `env:"NAME" yaml:"NAME"`
MaxOpenConnections int `env:"MAX_OPEN_CONNECTIONS" yaml:"MAX_OPEN_CONNECTIONS"`
MaxIdleConnections int `env:"MAX_IDLE_CONNECTIONS" yaml:"MAX_IDLE_CONNECTIONS"`
SSLMode string `env:"SSL_MODE" yaml:"SSL_MODE"`
}

// BuildConnectionString builds the connection string to the database - for now same as reader
Expand Down
Loading