aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-12-02 12:57:15 -0800
committerAdam Soltys <[email protected]>2024-12-02 13:13:26 -0800
commita25cd41e1c38b91465ea95e4bcba66c4a089091e (patch)
tree6a976755a9110d3764d3c87079adfbd6583adfe0
parent501e2ba1942911a98278a95121a7553da19795d2 (diff)
downloadcoinos-server-a25cd41e1c38b91465ea95e4bcba66c4a089091e.tar.gz
coinos-server-a25cd41e1c38b91465ea95e4bcba66c4a089091e.zip
prevent double reverse
-rw-r--r--lib/db.ts27
1 files changed, 17 insertions, 10 deletions
diff --git a/lib/db.ts b/lib/db.ts
index 13c06344..09e559bb 100644
--- a/lib/db.ts
+++ b/lib/db.ts
@@ -28,6 +28,7 @@ return ourfee
`;
const REVERSE = `
+-- Keys
local paymentKey = KEYS[1]
local balanceKey = KEYS[2]
local creditKey = KEYS[3]
@@ -35,22 +36,28 @@ local hashKey = KEYS[4]
local paymentsKey = KEYS[5]
local pid = KEYS[6]
+-- Arguments
local total = tonumber(ARGV[1])
local credit = tonumber(ARGV[2])
local hash = ARGV[3]
-local payment = redis.call('get', paymentKey)
+-- Hardcoded TTL for the processing state
+local processingTTL = 5
-if payment ~= nil then
- redis.call('incrby', balanceKey, total)
- redis.call('incrby', creditKey, credit)
- redis.call('del', paymentKey)
- redis.call('del', hashKey)
- redis.call('lrem', paymentsKey, 0, pid);
- redis.call('srem', 'pending', hash);
+-- Attempt to atomically claim the payment with a timeout
+if redis.call('setex', paymentKey, processingTTL, 'processing') then
+ -- Only process if successfully claimed
+ redis.call('incrby', balanceKey, total)
+ redis.call('incrby', creditKey, credit)
+ redis.call('del', paymentKey)
+ redis.call('del', hashKey)
+ redis.call('lrem', paymentsKey, 0, pid)
+ redis.call('srem', 'pending', hash)
+ return pid
+else
+ -- Payment already being processed or processed
+ return nil
end
-
-return pid
`;
const debit = defineScript({