Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Added locking keys in one location
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Apr 5, 2022
1 parent 9a05e40 commit fd53b7d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
5 changes: 3 additions & 2 deletions action_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bux

import (
"context"
"fmt"

"github.com/BuxOrg/bux/utils"
)
Expand Down Expand Up @@ -37,7 +38,7 @@ func (c *Client) RecordTransaction(ctx context.Context, xPubKey, txHex, draftID

// Create the lock and set the release for after the function completes
unlock, err := newWriteLock(
ctx, "action-record-transaction-"+id, c.Cachestore(),
ctx, fmt.Sprintf(lockKeyRecordTx, id), c.Cachestore(),
)
defer unlock()
if err != nil {
Expand Down Expand Up @@ -99,7 +100,7 @@ func (c *Client) NewTransaction(ctx context.Context, rawXpubKey string, config *

// Create the lock and set the release for after the function completes
unlock, err := newWaitWriteLock(
ctx, "action-xpub-"+utils.Hash(rawXpubKey), c.Cachestore(),
ctx, fmt.Sprintf(lockKeyProcessXpub, utils.Hash(rawXpubKey)), c.Cachestore(),
)
defer unlock()
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions locks.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ import (
"github.com/BuxOrg/bux/cachestore"
)

const (
lockKeyProcessIncomingTx = "process-incoming-transaction-%s" // + Tx ID
lockKeyProcessSyncTx = "process-sync-transaction-%s" // + Tx ID
lockKeyProcessXpub = "action-xpub-id-%s" // + Xpub 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)
Expand Down
3 changes: 2 additions & 1 deletion model_incoming_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bux
import (
"context"
"errors"
"fmt"
"time"

"github.com/BuxOrg/bux/chainstate"
Expand Down Expand Up @@ -238,7 +239,7 @@ func processIncomingTransaction(ctx context.Context, incomingTx *IncomingTransac

// Create the lock and set the release for after the function completes
unlock, err := newWriteLock(
ctx, "process-incoming-transaction-"+incomingTx.GetID(), incomingTx.Client().Cachestore(),
ctx, fmt.Sprintf(lockKeyProcessIncomingTx, incomingTx.GetID()), incomingTx.Client().Cachestore(),
)
defer unlock()
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions model_sync_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"errors"
"fmt"
"time"

"github.com/BuxOrg/bux/chainstate"
Expand Down Expand Up @@ -278,7 +279,7 @@ func processBroadcastTransaction(ctx context.Context, syncTx *SyncTransaction) e

// Create the lock and set the release for after the function completes
unlock, err := newWriteLock(
ctx, "process-sync-transaction-"+syncTx.GetID(), syncTx.Client().Cachestore(),
ctx, fmt.Sprintf(lockKeyProcessSyncTx, syncTx.GetID()), syncTx.Client().Cachestore(),
)
defer unlock()
if err != nil {
Expand Down Expand Up @@ -328,7 +329,7 @@ func processSyncTransaction(ctx context.Context, syncTx *SyncTransaction) error

// Create the lock and set the release for after the function completes
unlock, err := newWriteLock(
ctx, "process-sync-transaction-"+syncTx.GetID(), syncTx.Client().Cachestore(),
ctx, fmt.Sprintf(lockKeyProcessSyncTx, syncTx.GetID()), syncTx.Client().Cachestore(),
)
defer unlock()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion model_utxos.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func ReserveUtxos(ctx context.Context, xPubID, draftID string,

// Create the lock and set the release for after the function completes
unlock, err := newWaitWriteLock(
ctx, "model-utxos-reserve-utxos-"+xPubID, m.Client().Cachestore(),
ctx, fmt.Sprintf(lockKeyReserveUtxo, xPubID), m.Client().Cachestore(),
)
defer unlock()
if err != nil {
Expand Down

0 comments on commit fd53b7d

Please sign in to comment.