Skip to content

Commit

Permalink
configurable max attempts (kolide#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
marpaia authored and groob committed Jan 11, 2018
1 parent 8609451 commit 5de39fa
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions dbutil/dbutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
)

type dbConfig struct {
logger log.Logger
logger log.Logger
maxAttempts int
}

// WithLogger configures a logger Option.
Expand All @@ -22,6 +23,13 @@ func WithLogger(logger log.Logger) Option {
}
}

// WithMaxAttempts configures the number of maximum attempts to make
func WithMaxAttempts(maxAttempts int) Option {
return func(c *dbConfig) {
c.maxAttempts = maxAttempts
}
}

// Option provides optional configuration for managing DB connections.
type Option func(*dbConfig)

Expand All @@ -31,7 +39,8 @@ type Option func(*dbConfig)
// the maxAttempts value(defaults to 15 attempts).
func OpenDB(driver, dsn string, opts ...Option) (*sql.DB, error) {
config := &dbConfig{
logger: log.NewNopLogger(),
logger: log.NewNopLogger(),
maxAttempts: 15,
}

for _, opt := range opts {
Expand All @@ -43,9 +52,8 @@ func OpenDB(driver, dsn string, opts ...Option) (*sql.DB, error) {
return nil, errors.Wrapf(err, "opening %s connection, dsn=%s", driver, dsn)
}

const maxAttempts = 15
var dbError error
for attempt := 0; attempt < maxAttempts; attempt++ {
for attempt := 0; attempt < config.maxAttempts; attempt++ {
dbError = db.Ping()
if dbError == nil {
// we're connected!
Expand Down

0 comments on commit 5de39fa

Please sign in to comment.