From a5eb7b54436156531ab8ca65f83f0687918b3755 Mon Sep 17 00:00:00 2001 From: keyan Date: Fri, 11 Aug 2023 17:53:21 -0500 Subject: [PATCH] invoice check backoff --- components/invoice.js | 2 +- lib/time.js | 10 ++++++---- worker/wallet.js | 11 ++++++++--- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/components/invoice.js b/components/invoice.js index 45440aa1..bfd9095c 100644 --- a/components/invoice.js +++ b/components/invoice.js @@ -165,7 +165,7 @@ export const useInvoiceable = (fn, options = defaultOptions) => { const onConfirmation = useCallback( (onClose, hmac) => { return async ({ id, satsReceived, hash }) => { - await sleep(2000) + await sleep(500) const repeat = () => fn(satsReceived, ...fnArgs, hash, hmac) .then(onClose) diff --git a/lib/time.js b/lib/time.js index ff0984bb..3573d4b7 100644 --- a/lib/time.js +++ b/lib/time.js @@ -1,4 +1,4 @@ -export function timeSince (timeStamp) { +function timeSince (timeStamp) { const now = new Date() const secondsPast = (now.getTime() - timeStamp) / 1000 if (secondsPast < 60) { @@ -20,7 +20,7 @@ export function timeSince (timeStamp) { return 'now' } -export function datePivot (date, +function datePivot (date, { years = 0, months = 0, days = 0, hours = 0, minutes = 0, seconds = 0, milliseconds = 0 }) { return new Date( date.getFullYear() + years, @@ -33,7 +33,7 @@ export function datePivot (date, ) } -export function timeLeft (timeStamp) { +function timeLeft (timeStamp) { const now = new Date() const secondsPast = (timeStamp - now.getTime()) / 1000 @@ -55,4 +55,6 @@ export function timeLeft (timeStamp) { } } -export const sleep = (ms) => new Promise((resolve, reject) => setTimeout(resolve, ms)) +const sleep = (ms) => new Promise((resolve, reject) => setTimeout(resolve, ms)) + +module.exports = { timeSince, datePivot, timeLeft, sleep } diff --git a/worker/wallet.js b/worker/wallet.js index bcd7c3dc..74576034 100644 --- a/worker/wallet.js +++ b/worker/wallet.js @@ -1,8 +1,10 @@ const serialize = require('../api/resolvers/serial') const { getInvoice, getPayment } = require('ln-service') +const { datePivot } = require('../lib/time') const walletOptions = { startAfter: 5, retryLimit: 21, retryBackoff: true } +// TODO this should all be done via websockets function checkInvoice ({ boss, models, lnd }) { return async function ({ data: { hash } }) { let inv @@ -32,8 +34,10 @@ function checkInvoice ({ boss, models, lnd }) { } })) } else if (new Date(inv.expires_at) > new Date()) { - // not expired, recheck in 5 seconds - await boss.send('checkInvoice', { hash }, walletOptions) + // not expired, recheck in 5 seconds if the invoice is younger than 5 minutes + // otherwise recheck in 60 seconds + const startAfter = new Date(inv.created_at) > datePivot(new Date(), { minutes: -5 }) ? 5 : 60 + await boss.send('checkInvoice', { hash }, { ...walletOptions, startAfter }) } } } @@ -76,7 +80,8 @@ function checkWithdrawal ({ boss, models, lnd }) { SELECT reverse_withdrawl(${id}::INTEGER, ${status}::"WithdrawlStatus")`) } else { // we need to requeue to check again in 5 seconds - await boss.send('checkWithdrawal', { id, hash }, walletOptions) + const startAfter = new Date(wdrwl.created_at) > datePivot(new Date(), { minutes: -5 }) ? 5 : 60 + await boss.send('checkWithdrawal', { id, hash }, { ...walletOptions, startAfter }) } } }