invoice check backoff

This commit is contained in:
keyan 2023-08-11 17:53:21 -05:00
parent 9e4f9aa558
commit a5eb7b5443
3 changed files with 15 additions and 8 deletions

View File

@ -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)

View File

@ -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 }

View File

@ -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 })
}
}
}