Skip to content

Commit fe7fb63

Browse files
committed
[locker] add godoc
1 parent f41c2ef commit fe7fb63

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

internal/worker/locker/locker.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ import (
55
"errors"
66
)
77

8+
// ErrLockNotAcquired is returned when a lock cannot be acquired within the configured timeout.
89
var ErrLockNotAcquired = errors.New("lock not acquired")
910

1011
type Locker interface {
12+
// AcquireLock attempts to acquire a lock for the given key.
13+
// Returns ErrLockNotAcquired if the lock cannot be acquired within the configured timeout.
1114
AcquireLock(ctx context.Context, key string) error
15+
// ReleaseLock releases a previously acquired lock for the given key.
16+
// Returns an error if the lock was not held by this instance.
1217
ReleaseLock(ctx context.Context, key string) error
1318
}

internal/worker/locker/mysql.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@ import (
55
"database/sql"
66
"fmt"
77
"sync"
8+
"time"
89
)
910

1011
type mySQLLocker struct {
1112
db *sql.DB
1213

1314
prefix string
14-
timeout uint
15+
timeout time.Duration
1516

1617
mu sync.Mutex
1718
conns map[string]*sql.Conn
1819
}
1920

20-
func NewMySQLLocker(db *sql.DB, prefix string, timeout uint) Locker {
21+
// NewMySQLLocker creates a new MySQL-based distributed locker.
22+
func NewMySQLLocker(db *sql.DB, prefix string, timeout time.Duration) Locker {
2123
return &mySQLLocker{
2224
db: db,
2325

@@ -39,7 +41,7 @@ func (m *mySQLLocker) AcquireLock(ctx context.Context, key string) error {
3941
}
4042

4143
var res sql.NullInt64
42-
if err := conn.QueryRowContext(ctx, "SELECT GET_LOCK(?, ?)", name, m.timeout).Scan(&res); err != nil {
44+
if err := conn.QueryRowContext(ctx, "SELECT GET_LOCK(?, ?)", name, m.timeout.Seconds()).Scan(&res); err != nil {
4345
_ = conn.Close()
4446
return fmt.Errorf("failed to get lock: %w", err)
4547
}

0 commit comments

Comments
 (0)