This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
locks.go
37 lines (32 loc) · 1.63 KB
/
locks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package bux
import (
"context"
"github.com/mrz1836/go-cachestore"
)
const (
lockKeyMonitorLockID = "monitor-lock-id-%s" // + Lock ID
lockKeyProcessBroadcastTx = "process-broadcast-transaction-%s" // + Tx ID
lockKeyProcessIncomingTx = "process-incoming-transaction-%s" // + Tx ID
lockKeyProcessP2PTx = "process-p2p-transaction-%s" // + Tx ID
lockKeyProcessSyncTx = "process-sync-transaction-%s" // + Tx ID
lockKeyProcessXpub = "action-xpub-id-%s" // + Xpub ID
lockKeyRecordBlockHeader = "action-record-block-header-%s" // + Hash id
lockKeyRecordTx = "action-record-transaction-%s" // + Tx ID
lockKeyReserveUtxo = "utxo-reserve-xpub-id-%s" // + Xpub ID
)
// newWriteLock will take care of creating a lock and defer
func newWriteLock(ctx context.Context, lockKey string, cacheStore cachestore.LockService) (func(), error) {
secret, err := cacheStore.WriteLock(ctx, lockKey, defaultCacheLockTTL)
return func() {
// context is not set, since the req could be canceled, but unlocking should never be stopped
_, _ = cacheStore.ReleaseLock(context.Background(), lockKey, secret)
}, err
}
// newWaitWriteLock will take care of creating a lock and defer
func newWaitWriteLock(ctx context.Context, lockKey string, cacheStore cachestore.LockService) (func(), error) {
secret, err := cacheStore.WaitWriteLock(ctx, lockKey, defaultCacheLockTTL, defaultCacheLockTTW)
return func() {
// context is not set, since the req could be canceled, but unlocking should never be stopped
_, _ = cacheStore.ReleaseLock(context.Background(), lockKey, secret)
}, err
}