diff --git a/pkg/db/db_store.go b/pkg/db/db_store.go index ad0cab9..16345e1 100644 --- a/pkg/db/db_store.go +++ b/pkg/db/db_store.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "sync" + "sync/atomic" "time" "github.com/rs/zerolog" @@ -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 @@ -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 @@ -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 @@ -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, diff --git a/pkg/db/db_wrapper.go b/pkg/db/db_wrapper.go index e30c436..1e913b8 100644 --- a/pkg/db/db_wrapper.go +++ b/pkg/db/db_wrapper.go @@ -6,6 +6,7 @@ import ( "fmt" "strings" "sync" + "sync/atomic" "time" _ "github.com/lib/pq" // concrete implementation of database @@ -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) @@ -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 diff --git a/pkg/db/settings.go b/pkg/db/settings.go index b4d7e18..ffd41df 100644 --- a/pkg/db/settings.go +++ b/pkg/db/settings.go @@ -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