move backoff into codebase

This commit is contained in:
Fred K. Schott
2020-01-08 14:50:17 -08:00
parent 0a22697e5d
commit 5f208801ee
5 changed files with 82 additions and 7 deletions

View File

@@ -19,7 +19,6 @@
"dependencies": { "dependencies": {
"@types/lodash": "^4.14.136", "@types/lodash": "^4.14.136",
"@types/ws": "^6.0.3", "@types/ws": "^6.0.3",
"backo": "^1.1.0",
"bignumber.js": "^9.0.0", "bignumber.js": "^9.0.0",
"https-proxy-agent": "^3.0.0", "https-proxy-agent": "^3.0.0",
"jsonschema": "1.2.2", "jsonschema": "1.2.2",

44
src/common/backoff.ts Normal file
View File

@@ -0,0 +1,44 @@
/*
* Original code based on "backo" - https://github.com/segmentio/backo
* MIT License - Copyright 2014 Segment.io
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* A Back off strategy that increases exponentionally. Useful with repeated
* setTimeout calls over a network (where the destination may be down).
*/
export class ExponentialBackoff {
private readonly ms: number
private readonly max: number
private readonly factor: number = 2
private readonly jitter: number = 0
attempts: number = 0
constructor(opts: {min?: number; max?: number} = {}) {
this.ms = opts.min || 100
this.max = opts.max || 10000
}
/**
* Return the backoff duration.
*/
duration() {
var ms = this.ms * Math.pow(this.factor, this.attempts++)
if (this.jitter) {
var rand = Math.random()
var deviation = Math.floor(rand * this.jitter * ms)
ms = (Math.floor(rand * 10) & 1) == 0 ? ms - deviation : ms + deviation
}
return Math.min(ms, this.max) | 0
}
/**
* Reset the number of attempts.
*/
reset() {
this.attempts = 0
}
}

View File

@@ -13,7 +13,7 @@ import {
RippledNotInitializedError, RippledNotInitializedError,
RippleError RippleError
} from './errors' } from './errors'
const ExponentialBackoff = require('backo') import {ExponentialBackoff} from './backoff';
/** /**
* ConnectionOptions is the configuration for the Connection class. * ConnectionOptions is the configuration for the Connection class.

37
test/backoff-test.ts Normal file
View File

@@ -0,0 +1,37 @@
import assert from 'assert-diff'
import {ExponentialBackoff} from '../src/common/backoff'
describe('ExponentialBackoff', function() {
it('duration() return value starts with the min value', function() {
// default: 100ms
assert(new ExponentialBackoff().duration(), 100)
assert(new ExponentialBackoff({min: 100}).duration(), 100)
assert(new ExponentialBackoff({min: 123}).duration(), 123)
})
it('duration() return value increases when called multiple times', function() {
const backoff = new ExponentialBackoff({min: 100, max: 1000})
assert.strictEqual(backoff.duration(), 100)
assert.strictEqual(backoff.duration(), 200)
assert.strictEqual(backoff.duration(), 400)
assert.strictEqual(backoff.duration(), 800)
})
it('duration() never returns greater than the max value', function() {
const backoff = new ExponentialBackoff({min: 300, max: 1000})
assert.strictEqual(backoff.duration(), 300)
assert.strictEqual(backoff.duration(), 600)
assert.strictEqual(backoff.duration(), 1000)
assert.strictEqual(backoff.duration(), 1000)
})
it('reset() will reset the duration() value', function() {
const backoff = new ExponentialBackoff({min: 100, max: 1000})
assert.strictEqual(backoff.duration(), 100)
assert.strictEqual(backoff.duration(), 200)
assert.strictEqual(backoff.duration(), 400)
backoff.reset()
assert.strictEqual(backoff.duration(), 100)
assert.strictEqual(backoff.duration(), 200)
})
})

View File

@@ -561,11 +561,6 @@ babel-runtime@^6.6.1:
core-js "^2.4.0" core-js "^2.4.0"
regenerator-runtime "^0.11.0" regenerator-runtime "^0.11.0"
backo@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/backo/-/backo-1.1.0.tgz#a36c4468923f2d265c9e8a709ea56ecdaff807e6"
integrity sha1-o2xEaJI/LSZcnopwnqVuza/4B+Y=
balanced-match@^1.0.0: balanced-match@^1.0.0:
version "1.0.0" version "1.0.0"
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767" resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"