Files
steampipe/pkg/db/db_common/wait_connection.go

36 lines
793 B
Go

package db_common
import (
"context"
"database/sql"
"time"
"github.com/turbot/steampipe/pkg/constants"
"github.com/turbot/steampipe/pkg/utils"
)
// WaitForConnection waits for the db to start accepting connections and returns true
// returns false if the dbClient does not start within a stipulated time,
func WaitForConnection(ctx context.Context, db *sql.DB) (err error) {
utils.LogTime("db.waitForConnection start")
defer utils.LogTime("db.waitForConnection end")
pingTimer := time.NewTicker(constants.ServicePingInterval)
timeoutAt := time.After(constants.ServiceStartTimeout)
defer pingTimer.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-pingTimer.C:
err = db.Ping()
if err == nil {
return
}
case <-timeoutAt:
return
}
}
}