retry on serialization errors

This commit is contained in:
keyan 2021-05-20 12:21:11 -05:00
parent 0eabe1463d
commit 57e96ac02b
3 changed files with 28 additions and 17 deletions

View File

@ -1,22 +1,32 @@
const { UserInputError } = require('apollo-server-micro')
const retry = require('async-retry')
async function serialize (models, call) {
try {
const [, result] = await models.$transaction([
models.$executeRaw(SERIALIZE),
call
])
return result
} catch (error) {
console.log(error)
if (error.message.includes('SN_INSUFFICIENT_FUNDS')) {
throw new UserInputError('insufficient funds')
await retry(async bail => {
try {
const [, result] = await models.$transaction([
models.$executeRaw(SERIALIZE),
call
])
return result
} catch (error) {
console.log(error)
if (error.message.includes('SN_INSUFFICIENT_FUNDS')) {
bail(new UserInputError('insufficient funds'))
}
if (error.message.includes('SN_NOT_SERIALIZABLE')) {
bail(new Error('wallet balance transaction is not serializable'))
}
if (error.message.includes('40001')) {
throw new Error('wallet balance serialization failure - retry again')
}
bail(error)
}
if (error.message.includes('SN_NOT_SERIALIZABLE')) {
throw new Error('wallet transaction isolation level is not serializable')
}
throw error
}
}, {
minTimeout: 100,
factor: 1.1,
retries: 5
})
}
const SERIALIZE = 'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE'

View File

@ -11,6 +11,7 @@
"@apollo/client": "^3.3.13",
"@prisma/client": "^2.22.1",
"apollo-server-micro": "^2.21.2",
"async-retry": "^1.3.1",
"bootstrap": "^4.6.0",
"clipboard-copy": "^4.0.1",
"formik": "^2.2.6",
@ -46,4 +47,4 @@
"prisma": "^2.22.1",
"standard": "^16.0.3"
}
}
}

View File

@ -1075,7 +1075,7 @@ async-limiter@~1.0.0:
resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.1.tgz#dd379e94f0db8310b08291f9d64c3209766617fd"
integrity sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ==
async-retry@^1.2.1:
async-retry@^1.2.1, async-retry@^1.3.1:
version "1.3.1"
resolved "https://registry.yarnpkg.com/async-retry/-/async-retry-1.3.1.tgz#139f31f8ddce50c0870b0ba558a6079684aaed55"
integrity sha512-aiieFW/7h3hY0Bq5d+ktDBejxuwR78vRu9hDUdR8rNhSaQ29VzPL4AoIRG7D/c7tdenwOcKvgPM6tIxB3cB6HA==