Skip to content

Commit f67c1a6

Browse files
committed
[locker] add Close method
1 parent fe7fb63 commit f67c1a6

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

internal/worker/locker/locker.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ type Locker interface {
1515
// ReleaseLock releases a previously acquired lock for the given key.
1616
// Returns an error if the lock was not held by this instance.
1717
ReleaseLock(ctx context.Context, key string) error
18+
19+
// Close releases any held locks.
20+
Close() error
1821
}

internal/worker/locker/module.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package locker
22

33
import (
4+
"context"
45
"database/sql"
56

67
"github.com/go-core-fx/logger"
@@ -16,5 +17,15 @@ func Module() fx.Option {
1617
fx.Provide(func(db *sql.DB) Locker {
1718
return NewMySQLLocker(db, "worker:", timeoutSeconds)
1819
}),
20+
fx.Invoke(func(locker Locker, lc fx.Lifecycle) {
21+
lc.Append(fx.Hook{
22+
OnStart: func(_ context.Context) error {
23+
return nil
24+
},
25+
OnStop: func(_ context.Context) error {
26+
return locker.Close()
27+
},
28+
})
29+
}),
1930
)
2031
}

internal/worker/locker/mysql.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,18 @@ func (m *mySQLLocker) ReleaseLock(ctx context.Context, key string) error {
8787
return nil
8888
}
8989

90+
// Close closes all remaining pinned connections.
91+
// Should be called during shutdown to clean up resources.
92+
func (m *mySQLLocker) Close() error {
93+
m.mu.Lock()
94+
defer m.mu.Unlock()
95+
for key, conn := range m.conns {
96+
if conn != nil {
97+
_ = conn.Close()
98+
}
99+
delete(m.conns, key)
100+
}
101+
return nil
102+
}
103+
90104
var _ Locker = (*mySQLLocker)(nil)

0 commit comments

Comments
 (0)