diff --git a/api/resolvers/serial.js b/api/resolvers/serial.js index 4d0208f2..034ca69e 100644 --- a/api/resolvers/serial.js +++ b/api/resolvers/serial.js @@ -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' diff --git a/package.json b/package.json index 3f724821..fd67e96c 100644 --- a/package.json +++ b/package.json @@ -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" } -} \ No newline at end of file +} diff --git a/yarn.lock b/yarn.lock index c48eab83..4588c2f6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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==