这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict'

const getRandomInt = require('./get-random-int')

/**
* Decorrelated Jitter implementation
* https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
*/
module.exports = (opts) => {
const {
startingDelayMs = 80 * 1_000,
maxDelayMs = 5 * 60 * 1_000,
startingTimeMultiplier = 1.2,
endingTimeMultiplier = 1.5,
prevBackOffDelayMs = 0
} = opts ?? {}

const prevDelayNotLessStarting = Math.max(startingDelayMs, prevBackOffDelayMs)

const calcedStar = prevDelayNotLessStarting * startingTimeMultiplier
const calcedEnd = prevDelayNotLessStarting * endingTimeMultiplier

const jitteredDelay = getRandomInt(calcedStar, calcedEnd)
const limitedDelay = Math.min(maxDelayMs, jitteredDelay)

return limitedDelay
}
33 changes: 33 additions & 0 deletions workers/loc.api/helpers/get-data-from-api/helpers/delay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict'

const Interrupter = require('../../../interrupter')
const isInterrupted = require('./is-interrupted')

module.exports = (mc = 80000, interrupter) => {
if (isInterrupted(interrupter)) {
return Promise.resolve({ isInterrupted: true })
}

return new Promise((resolve) => {
const hasInterrupter = interrupter instanceof Interrupter
const timeout = setTimeout(() => {
if (hasInterrupter) {
interrupter.offInterrupt(onceInterruptHandler)
}

resolve({ isInterrupted: false })
}, mc)
const onceInterruptHandler = () => {
if (!timeout.hasRef()) {
return
}

clearTimeout(timeout)
resolve({ isInterrupted: true })
}

if (hasInterrupter) {
interrupter.onceInterrupt(onceInterruptHandler)
}
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict'

module.exports = () => {
return { jsonrpc: '2.0', result: [], id: null }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict'

module.exports = (min, max) => {
const minCeiled = Math.ceil(min)
const maxFloored = Math.floor(max)

return Math.floor(Math.random() * (maxFloored - minCeiled) + minCeiled)
}
17 changes: 17 additions & 0 deletions workers/loc.api/helpers/get-data-from-api/helpers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict'

const getRandomInt = require('./get-random-int')
const calcBackOffAndJitteredDelay = require(
'./calc-back-off-and-jittered-delay'
)
const delay = require('./delay')
const isInterrupted = require('./is-interrupted')
const getEmptyArrRes = require('./get-empty-arr-res')

module.exports = {
getRandomInt,
calcBackOffAndJitteredDelay,
delay,
isInterrupted,
getEmptyArrRes
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict'

const Interrupter = require('../../../interrupter')

module.exports = (interrupter) => {
return (
interrupter instanceof Interrupter &&
interrupter.hasInterrupted()
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,20 @@

const { cloneDeep } = require('lib-js-util-base')

const Interrupter = require('../interrupter')
const AbstractWSEventEmitter = require('../abstract.ws.event.emitter')
const AbstractWSEventEmitter = require('../../abstract.ws.event.emitter')
const {
isRateLimitError,
isNonceSmallError,
isUserIsNotMerchantError,
isENetError,
isAuthError
} = require('./api-errors-testers')

const _delay = (mc = 80000, interrupter) => {
if (_isInterrupted(interrupter)) {
return Promise.resolve({ isInterrupted: true })
}

return new Promise((resolve) => {
const hasInterrupter = interrupter instanceof Interrupter
const timeout = setTimeout(() => {
if (hasInterrupter) {
interrupter.offInterrupt(onceInterruptHandler)
}

resolve({ isInterrupted: false })
}, mc)
const onceInterruptHandler = () => {
if (!timeout.hasRef()) {
return
}

clearTimeout(timeout)
resolve({ isInterrupted: true })
}

if (hasInterrupter) {
interrupter.onceInterrupt(onceInterruptHandler)
}
})
}

const _isInterrupted = (interrupter) => {
return (
interrupter instanceof Interrupter &&
interrupter.hasInterrupted()
)
}

const _getEmptyArrRes = () => {
return { jsonrpc: '2.0', result: [], id: null }
}
} = require('../api-errors-testers')
const {
calcBackOffAndJitteredDelay,
isInterrupted: _isInterrupted,
delay,
getEmptyArrRes
} = require('./helpers')

module.exports = (
commonInterrupter,
Expand All @@ -62,16 +27,16 @@ module.exports = (
middlewareParams,
callerName,
eNetErrorAttemptsTimeframeMin = 10, // min
eNetErrorAttemptsTimeoutMs = 10000, // ms
eNetErrorAttemptsTimeoutMs = 10_000, // ms
shouldNotInterrupt,
interrupter
interrupter,
backOffOpts
}) => {
const _interrupter = shouldNotInterrupt
? null
: interrupter ?? commonInterrupter

const ms = 80000

let prevBackOffDelayMs = 0
let countNetError = 0
let countRateLimitError = 0
let countNonceSmallError = 0
Expand Down Expand Up @@ -112,7 +77,7 @@ module.exports = (
break
} catch (err) {
if (isUserIsNotMerchantError(err)) {
return _getEmptyArrRes()
return getEmptyArrRes()
}
if (isRateLimitError(err)) {
countRateLimitError += 1
Expand All @@ -121,7 +86,14 @@ module.exports = (
throw err
}

const { isInterrupted } = await _delay(ms, _interrupter)
const delayMs = calcBackOffAndJitteredDelay({
startingDelayMs: 80_000,
maxDelayMs: 5 * 60 * 1_000,
...backOffOpts,
prevBackOffDelayMs
})
prevBackOffDelayMs = delayMs
const { isInterrupted } = await delay(delayMs, _interrupter)

if (isInterrupted) {
return { isInterrupted }
Expand All @@ -136,7 +108,7 @@ module.exports = (
throw err
}

const { isInterrupted } = await _delay(1000, _interrupter)
const { isInterrupted } = await delay(1000, _interrupter)

if (isInterrupted) {
return { isInterrupted }
Expand Down Expand Up @@ -164,7 +136,7 @@ module.exports = (

const {
isInterrupted
} = await _delay(eNetErrorAttemptsTimeoutMs, _interrupter)
} = await delay(eNetErrorAttemptsTimeoutMs, _interrupter)

if (isInterrupted) {
return { isInterrupted }
Expand All @@ -183,7 +155,7 @@ module.exports = (
throw err
}

const { isInterrupted } = await _delay(10000, _interrupter)
const { isInterrupted } = await delay(10000, _interrupter)

if (isInterrupted) {
return { isInterrupted }
Expand Down