Skip to content

Commit 0ca33d1

Browse files
committed
fix(native): make MySQL connection check immediate on first attempt
The waitForMySQL function was timing out before making any connection attempt when using short timeouts (500ms). The ticker waits 500ms before the first tick, at which point a 500ms timeout would already be expired. This fix adds an immediate connection attempt before entering the ticker loop, ensuring that at least one connection attempt is made even with short timeouts. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 67e865b commit 0ca33d1

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

internal/sqltest/native/mysql.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ func waitForMySQL(ctx context.Context, uri string, timeout time.Duration) error
166166
ticker := time.NewTicker(500 * time.Millisecond)
167167
defer ticker.Stop()
168168

169+
// Make an immediate first attempt before waiting for the ticker
170+
if err := tryMySQLConnection(ctx, uri); err == nil {
171+
return nil
172+
}
173+
169174
var lastErr error
170175
for {
171176
select {
@@ -175,23 +180,24 @@ func waitForMySQL(ctx context.Context, uri string, timeout time.Duration) error
175180
if time.Now().After(deadline) {
176181
return fmt.Errorf("timeout waiting for MySQL (last error: %v)", lastErr)
177182
}
178-
db, err := sql.Open("mysql", uri)
179-
if err != nil {
180-
lastErr = err
181-
slog.Debug("native/mysql", "open-attempt", err)
182-
continue
183-
}
184-
// Use a short timeout for ping to avoid hanging
185-
pingCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
186-
err = db.PingContext(pingCtx)
187-
cancel()
188-
if err != nil {
183+
if err := tryMySQLConnection(ctx, uri); err != nil {
189184
lastErr = err
190-
db.Close()
191185
continue
192186
}
193-
db.Close()
194187
return nil
195188
}
196189
}
197190
}
191+
192+
func tryMySQLConnection(ctx context.Context, uri string) error {
193+
db, err := sql.Open("mysql", uri)
194+
if err != nil {
195+
slog.Debug("native/mysql", "open-attempt", err)
196+
return err
197+
}
198+
defer db.Close()
199+
// Use a short timeout for ping to avoid hanging
200+
pingCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
201+
defer cancel()
202+
return db.PingContext(pingCtx)
203+
}

0 commit comments

Comments
 (0)