Skip to content

feat: allow setting a timeout for mysql try lock#1200

Open
vgarvardt wants to merge 2 commits intogolang-migrate:masterfrom
vgarvardt:feat/try-lock-timeout
Open

feat: allow setting a timeout for mysql try lock#1200
vgarvardt wants to merge 2 commits intogolang-migrate:masterfrom
vgarvardt:feat/try-lock-timeout

Conversation

@vgarvardt
Copy link

Timeout for GET_LOCK() for mysql driver is currently hardcoded to 10 seconds. This PR allows setting a custom value for the timeout.

@vgarvardt
Copy link
Author

Tests are failing with

=== NAME  TestMigrate/cockroachdb/cockroach:v1.1.9
    dktest.go:185: Failed: error pulling image: cockroachdb/cockroach:v1.1.9 error: Error response from daemon: manifest for cockroachdb/cockroach:v1.1.9 not found: manifest unknown: manifest unknown
=== NAME  TestMigrate/cockroachdb/cockroach:v2.0.7
    dktest.go:185: Failed: error pulling image: cockroachdb/cockroach:v2.0.7 error: Error response from daemon: manifest for cockroachdb/cockroach:v2.0.7 not found: manifest unknown: manifest unknown
=== NAME  TestMigrate/cockroachdb/cockroach:v1.0.7
    dktest.go:185: Failed: error pulling image: cockroachdb/cockroach:v1.0.7 error: Error response from daemon: manifest for cockroachdb/cockroach:v1.0.7 not found: manifest unknown: manifest unknown
=== NAME  TestMigrate/cockroachdb/cockroach:v2.1.3
    dktest.go:185: Failed: error pulling image: cockroachdb/cockroach:v2.1.3 error: Error response from daemon: manifest for cockroachdb/cockroach:v2.1.3 not found: manifest unknown: manifest unknown
--- FAIL: TestMigrate (0.00s)
    --- FAIL: TestMigrate/cockroachdb/cockroach:v1.1.9 (0.12s)
    --- FAIL: TestMigrate/cockroachdb/cockroach:v2.0.7 (0.13s)
    --- FAIL: TestMigrate/cockroachdb/cockroach:v1.0.7 (0.13s)
    --- FAIL: TestMigrate/cockroachdb/cockroach:v2.1.3 (0.14s)

that has nothing to do with the PR

@vgarvardt vgarvardt force-pushed the feat/try-lock-timeout branch from b58a1ac to 6834b7b Compare January 23, 2026 10:06
Copilot AI review requested due to automatic review settings January 23, 2026 10:06
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR makes the MySQL driver’s advisory lock timeout configurable instead of using a hardcoded 10-second value.

Changes:

  • Introduces DefaultTryLockTimeoutSec and a new TryLockTimeoutSec field on Config.
  • Wires a new x-try-lock-timeout URL parameter through Open into the driver configuration and uses it in Lock when calling GET_LOCK.
  • Documents the new x-try-lock-timeout parameter in database/mysql/README.md.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
database/mysql/mysql.go Adds configurable try-lock timeout (TryLockTimeoutSec), parses x-try-lock-timeout from the DSN, and uses it in Lock for GET_LOCK.
database/mysql/README.md Documents the new x-try-lock-timeout DSN parameter and its semantics.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 309 to 313
tryLockTimeout := DefaultTryLockTimeoutSec
if m.config.TryLockTimeoutSec != 0 {
tryLockTimeout = m.config.TryLockTimeoutSec
}

Copy link

Copilot AI Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Lock, treating TryLockTimeoutSec == 0 as a signal to fall back to DefaultTryLockTimeoutSec means that a configured timeout of 0 seconds (e.g. via x-try-lock-timeout=0) is effectively ignored and still results in a 10 second wait, which diverges from MySQL GET_LOCK semantics where 0 is a valid "no wait" value. To make x-try-lock-timeout fully configurable and consistent with the README and MySQL behavior, this logic should distinguish between "unset" and an explicit 0 value (for example by always trusting m.config.TryLockTimeoutSec when set via Open, or by adding an explicit "use default" flag in the config).

Suggested change
tryLockTimeout := DefaultTryLockTimeoutSec
if m.config.TryLockTimeoutSec != 0 {
tryLockTimeout = m.config.TryLockTimeoutSec
}
tryLockTimeout := m.config.TryLockTimeoutSec

Copilot uses AI. Check for mistakes.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 257 to 277
tryLockTimeoutParam := customParams["x-try-lock-timeout"]
tryLockTimeout := DefaultTryLockTimeoutSec
if tryLockTimeoutParam != "" {
tryLockTimeout, err = strconv.Atoi(tryLockTimeoutParam)
if err != nil {
return nil, fmt.Errorf("could not parse x-try-lock-timeout as int: %w", err)
}
}

db, err := sql.Open("mysql", config.FormatDSN())
if err != nil {
return nil, err
}

mx, err := WithInstance(db, &Config{
DatabaseName: config.DBName,
MigrationsTable: customParams["x-migrations-table"],
NoLock: noLock,
StatementTimeout: time.Duration(statementTimeout) * time.Millisecond,
DatabaseName: config.DBName,
MigrationsTable: customParams["x-migrations-table"],
NoLock: noLock,
StatementTimeout: time.Duration(statementTimeout) * time.Millisecond,
TryLockTimeoutSec: tryLockTimeout,
})
Copy link

Copilot AI Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no tests covering the new x-try-lock-timeout behavior (parsing, defaulting, and how it affects GET_LOCK), even though this file already has integration and parameter-validation tests (e.g. for x-no-lock). Please add tests that at least verify successful parsing of valid values (including negative and zero), handling of invalid values, and that the configured timeout is actually used when acquiring the advisory lock.

Copilot uses AI. Check for mistakes.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

partially done @ 618e0c9

@coveralls
Copy link

coveralls commented Jan 23, 2026

Coverage Status

coverage: 54.469% (+0.04%) from 54.432%
when pulling 618e0c9 on vgarvardt:feat/try-lock-timeout
into 257fa84 on golang-migrate:master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants