mirror of
https://github.com/turbot/steampipe.git
synced 2026-02-22 14:00:14 -05:00
36 lines
793 B
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
|
|
}
|
|
}
|
|
}
|