(ctx context.Context, uri string, timeout time.Duration)
| 162 | } |
| 163 | |
| 164 | func waitForMySQL(ctx context.Context, uri string, timeout time.Duration) error { |
| 165 | deadline := time.Now().Add(timeout) |
| 166 | ticker := time.NewTicker(500 * time.Millisecond) |
| 167 | defer ticker.Stop() |
| 168 | |
| 169 | // Make an immediate first attempt before waiting for the ticker |
| 170 | if err := tryMySQLConnection(ctx, uri); err == nil { |
| 171 | return nil |
| 172 | } |
| 173 | |
| 174 | var lastErr error |
| 175 | for { |
| 176 | select { |
| 177 | case <-ctx.Done(): |
| 178 | return fmt.Errorf("context cancelled: %w (last error: %v)", ctx.Err(), lastErr) |
| 179 | case <-ticker.C: |
| 180 | if time.Now().After(deadline) { |
| 181 | return fmt.Errorf("timeout waiting for MySQL (last error: %v)", lastErr) |
| 182 | } |
| 183 | if err := tryMySQLConnection(ctx, uri); err != nil { |
| 184 | lastErr = err |
| 185 | continue |
| 186 | } |
| 187 | return nil |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | func tryMySQLConnection(ctx context.Context, uri string) error { |
| 193 | db, err := sql.Open("mysql", uri) |
no test coverage detected