From 922f8dc8f6a0e22db42899a1ca11a55be2aa449f Mon Sep 17 00:00:00 2001 From: Juerd Waalboer Date: Mon, 31 Oct 2022 19:00:20 +0100 Subject: [PATCH] Ensure unique transaction IDs Long overdue :) --- lib/RevBank/Cart.pm | 6 +++--- lib/RevBank/FileIO.pm | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/RevBank/Cart.pm b/lib/RevBank/Cart.pm index e06ce31..d563bc8 100644 --- a/lib/RevBank/Cart.pm +++ b/lib/RevBank/Cart.pm @@ -83,9 +83,8 @@ sub checkout($self, $user) { for $entry, $entry->contras; } - my $transaction_id = time() - 1300000000; - RevBank::FileIO::with_lock { + my $transaction_id = time() - 1300000000; RevBank::Plugins::call_hooks("checkout", $self, $user, $transaction_id); for my $account (reverse sort keys %deltas) { @@ -96,11 +95,12 @@ sub checkout($self, $user) { } RevBank::Plugins::call_hooks("checkout_done", $self, $user, $transaction_id); + + sleep 1; # look busy (and ensure new id for next transaction :)) }; $self->empty; - sleep 1; # Ensure new timestamp/id for new transaction return 1; } diff --git a/lib/RevBank/FileIO.pm b/lib/RevBank/FileIO.pm index e4947d5..32b4f1a 100644 --- a/lib/RevBank/FileIO.pm +++ b/lib/RevBank/FileIO.pm @@ -24,13 +24,15 @@ sub get_lock() { open $lockfh, ">", $lockfn; my $attempt = 1; + + my $debug = !!$ENV{REVBANK_DEBUG}; FLOCK: { if (flock $lockfh, LOCK_EX | LOCK_NB) { syswrite $lockfh, $$; return ++$lockcount; } - if (($attempt % 50) == 0) { + if (($attempt % 50) == 0 or $debug) { warn "Another revbank instance has the global lock. Waiting for it to finish...\n" } sleep .1;