waitForDB retries connection until DB is available.
(ctx context.Context)
| 94 | |
| 95 | // waitForDB retries connection until DB is available. |
| 96 | func (d *DB) waitForDB(ctx context.Context) error { |
| 97 | maxAttempts := d.cfg.DBConnMaxAttempts |
| 98 | interval := time.Duration(d.cfg.DBConnWaitInterval) * time.Second |
| 99 | |
| 100 | for attempt := 1; attempt <= maxAttempts; attempt++ { |
| 101 | if err := d.pool.Ping(ctx); err == nil { |
| 102 | return nil |
| 103 | } |
| 104 | |
| 105 | if attempt < maxAttempts { |
| 106 | select { |
| 107 | case <-ctx.Done(): |
| 108 | return ctx.Err() |
| 109 | case <-time.After(interval): |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return fmt.Errorf("database unavailable after %d attempts", maxAttempts) |
| 115 | } |
| 116 | |
| 117 | // DB returns the receiver, satisfying DBProvider for single-host mode. |
| 118 | func (d *DB) DB(ctx context.Context) *DB { |