waitForDB retries connection until DB is available.
(ctx context.Context)
| 43 | |
| 44 | // waitForDB retries connection until DB is available. |
| 45 | func (p *Pool) waitForDB(ctx context.Context) error { |
| 46 | maxAttempts := p.cfg.DBConnMaxAttempts |
| 47 | interval := time.Duration(p.cfg.DBConnWaitInterval) * time.Second |
| 48 | |
| 49 | for attempt := 1; attempt <= maxAttempts; attempt++ { |
| 50 | if err := p.Ping(ctx); err == nil { |
| 51 | return nil |
| 52 | } |
| 53 | |
| 54 | if attempt < maxAttempts { |
| 55 | select { |
| 56 | case <-ctx.Done(): |
| 57 | return ctx.Err() |
| 58 | case <-time.After(interval): |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return fmt.Errorf("database unavailable after %d attempts", maxAttempts) |
| 64 | } |
| 65 | |
| 66 | // Ping checks database connectivity. |
| 67 | func (p *Pool) Ping(ctx context.Context) error { |