Release / update-version (push) Has been cancelled
Release / build-frontend (push) Has been cancelled
Release / release (push) Has been cancelled
Release / sync-version-file (push) Has been cancelled
CI / shell (push) Canceled after 0s
CI / test (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
Security Scan / backend-security (push) Canceled after 0s
Security Scan / frontend-security (push) Canceled after 0s
53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"hash/fnv"
|
|
"time"
|
|
)
|
|
|
|
func hashAdvisoryLockID(key string) int64 {
|
|
h := fnv.New64a()
|
|
_, _ = h.Write([]byte(key))
|
|
return int64(h.Sum64())
|
|
}
|
|
|
|
func tryAcquireDBAdvisoryLock(ctx context.Context, db *sql.DB, lockID int64) (func(), bool) {
|
|
release, acquired, _ := tryAcquireDBAdvisoryLockWithError(ctx, db, lockID)
|
|
return release, acquired
|
|
}
|
|
|
|
func tryAcquireDBAdvisoryLockWithError(ctx context.Context, db *sql.DB, lockID int64) (func(), bool, error) {
|
|
if db == nil {
|
|
return nil, false, nil
|
|
}
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
|
|
conn, err := db.Conn(ctx)
|
|
if err != nil {
|
|
return nil, false, fmt.Errorf("open advisory-lock connection: %w", err)
|
|
}
|
|
|
|
acquired := false
|
|
if err := conn.QueryRowContext(ctx, "SELECT pg_try_advisory_lock($1)", lockID).Scan(&acquired); err != nil {
|
|
_ = conn.Close()
|
|
return nil, false, fmt.Errorf("query advisory lock: %w", err)
|
|
}
|
|
if !acquired {
|
|
_ = conn.Close()
|
|
return nil, false, nil
|
|
}
|
|
|
|
release := func() {
|
|
unlockCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
defer cancel()
|
|
_, _ = conn.ExecContext(unlockCtx, "SELECT pg_advisory_unlock($1)", lockID)
|
|
_ = conn.Close()
|
|
}
|
|
return release, true, nil
|
|
}
|