Skip to content

Commit

Permalink
rename onNext and onElse
Browse files Browse the repository at this point in the history
  • Loading branch information
flotwig committed Dec 8, 2021
1 parent d627123 commit 2901f91
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 41 deletions.
48 changes: 24 additions & 24 deletions packages/server/lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ const getOriginalHeaders = (req = {}) => {
}

const getDelayForRetry = function (options = {}) {
const { err, opts, delaysRemaining, retryIntervals, onNext, onElse } = options
const { err, opts, delaysRemaining, retryIntervals, retryFn, onFail } = options

let delay = delaysRemaining.shift()

if (!_.isNumber(delay)) {
// no more delays, bailing
debug('exhausted all attempts retrying request %o', merge(opts, { err }))

return onElse()
return onFail()
}

// figure out which attempt we're on...
Expand All @@ -76,7 +76,7 @@ const getDelayForRetry = function (options = {}) {
attempt,
}))

return onNext(delay, attempt)
return retryFn({ delay, attempt })
}

const hasRetriableStatusCodeFailure = (res, retryOnStatusCodeFailure) => {
Expand Down Expand Up @@ -105,8 +105,8 @@ const maybeRetryOnNetworkFailure = function (err, options = {}) {
retryIntervals,
delaysRemaining,
retryOnNetworkFailure,
onNext,
onElse,
retryFn,
onFail,
} = options

debug('received an error making http request %o', merge(opts, { err }))
Expand All @@ -121,12 +121,12 @@ const maybeRetryOnNetworkFailure = function (err, options = {}) {

if (retryIntervals.length === 0) {
// normally, this request would not be retried, but we need to retry in order to support TLSv1
return onNext(0, 1)
return retryFn({ delay: 0, attempt: 1 })
}
}

if (!isTlsVersionError && !isErrEmptyResponseError(err.originalErr || err) && !isRetriableError(err, retryOnNetworkFailure)) {
return onElse()
return onFail()
}

// else see if we have more delays left...
Expand All @@ -135,8 +135,8 @@ const maybeRetryOnNetworkFailure = function (err, options = {}) {
opts,
retryIntervals,
delaysRemaining,
onNext,
onElse,
retryFn,
onFail,
})
}

Expand All @@ -148,8 +148,8 @@ const maybeRetryOnStatusCodeFailure = function (res, options = {}) {
retryIntervals,
delaysRemaining,
retryOnStatusCodeFailure,
onNext,
onElse,
retryFn,
onFail,
} = options

debug('received status code & headers on request %o', {
Expand All @@ -161,7 +161,7 @@ const maybeRetryOnStatusCodeFailure = function (res, options = {}) {
// is this a retryable status code failure?
if (!hasRetriableStatusCodeFailure(res, retryOnStatusCodeFailure)) {
// if not then we're done here
return onElse()
return onFail()
}

// else see if we have more delays left...
Expand All @@ -170,8 +170,8 @@ const maybeRetryOnStatusCodeFailure = function (res, options = {}) {
opts,
retryIntervals,
delaysRemaining,
onNext,
onElse,
retryFn,
onFail,
})
}

Expand Down Expand Up @@ -206,7 +206,7 @@ const createRetryingRequestPromise = function (opts) {
retryOnStatusCodeFailure,
} = opts

const retry = (delay) => {
const retry = ({ delay }) => {
return Promise.delay(delay)
.then(() => {
return createRetryingRequestPromise(opts)
Expand All @@ -221,8 +221,8 @@ const createRetryingRequestPromise = function (opts) {
retryIntervals,
delaysRemaining,
retryOnNetworkFailure,
onNext: retry,
onElse () {
retryFn: retry,
onFail () {
throw err
},
})
Expand All @@ -234,8 +234,8 @@ const createRetryingRequestPromise = function (opts) {
retryIntervals,
delaysRemaining,
retryOnStatusCodeFailure,
onNext: retry,
onElse: _.constant(res),
retryFn: retry,
onFail: _.constant(res),
})
})
}
Expand Down Expand Up @@ -286,7 +286,7 @@ const createRetryingRequestStream = function (opts = {}) {
const reqStream = r(opts)
let didReceiveResponse = false

const retry = function (delay, attempt) {
const retry = function ({ delay, attempt }) {
retryStream.emit('retry', { attempt, delay })

return setTimeout(tryStartStream, delay)
Expand Down Expand Up @@ -342,8 +342,8 @@ const createRetryingRequestStream = function (opts = {}) {
retryIntervals,
delaysRemaining,
retryOnNetworkFailure,
onNext: retry,
onElse () {
retryFn: retry,
onFail () {
return emitError(err)
},
})
Expand All @@ -365,8 +365,8 @@ const createRetryingRequestStream = function (opts = {}) {
delaysRemaining,
retryIntervals,
retryOnStatusCodeFailure,
onNext: retry,
onElse () {
retryFn: retry,
onFail () {
debug('successful response received', { requestId })

cleanup()
Expand Down
34 changes: 17 additions & 17 deletions packages/server/test/unit/request_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,24 @@ describe('lib/request', () => {
code: 'ECONNREFUSED',
}

const onNext = sinon.stub()
const retryFn = sinon.stub()

retryIntervals.forEach(() => {
return request.getDelayForRetry({
err,
onNext,
retryFn,
retryIntervals,
delaysRemaining,
})
})

expect(delaysRemaining).to.be.empty

expect(onNext.args).to.deep.eq([
[0, 1],
[999, 2],
[100, 3],
[200, 4],
expect(retryFn.args).to.deep.eq([
[{ delay: 0, attempt: 1 }],
[{ delay: 999, attempt: 2 }],
[{ delay: 100, attempt: 3 }],
[{ delay: 200, attempt: 4 }],
])
})

Expand All @@ -95,37 +95,37 @@ describe('lib/request', () => {
code: 'ECONNRESET',
}

const onNext = sinon.stub()
const retryFn = sinon.stub()

request.getDelayForRetry({
err,
onNext,
retryFn,
retryIntervals,
delaysRemaining,
})

expect(delaysRemaining).to.have.length(3)

expect(onNext).to.be.calledWith(2000, 1)
expect(retryFn).to.be.calledWith({ delay: 2000, attempt: 1 })
})

it('calls onElse when delaysRemaining is exhausted', () => {
it('calls onFail when delaysRemaining is exhausted', () => {
const retryIntervals = [1, 2, 3, 4]
const delaysRemaining = []

const onNext = sinon.stub()
const onElse = sinon.stub()
const retryFn = sinon.stub()
const onFail = sinon.stub()

request.getDelayForRetry({
onElse,
onNext,
onFail,
retryFn,
retryIntervals,
delaysRemaining,
})

expect(onElse).to.be.calledWithExactly()
expect(onFail).to.be.calledWithExactly()

expect(onNext).not.to.be.called
expect(retryFn).not.to.be.called
})
})

Expand Down

0 comments on commit 2901f91

Please sign in to comment.