From 5f208801ee78a8517ac152e1a15a2f2c8840e391 Mon Sep 17 00:00:00 2001 From: "Fred K. Schott" Date: Wed, 8 Jan 2020 14:50:17 -0800 Subject: [PATCH] move backoff into codebase --- package.json | 1 - src/common/backoff.ts | 44 ++++++++++++++++++++++++++++++++++++++++ src/common/connection.ts | 2 +- test/backoff-test.ts | 37 +++++++++++++++++++++++++++++++++ yarn.lock | 5 ----- 5 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 src/common/backoff.ts create mode 100644 test/backoff-test.ts diff --git a/package.json b/package.json index 656d4007..b24c03ae 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,6 @@ "dependencies": { "@types/lodash": "^4.14.136", "@types/ws": "^6.0.3", - "backo": "^1.1.0", "bignumber.js": "^9.0.0", "https-proxy-agent": "^3.0.0", "jsonschema": "1.2.2", diff --git a/src/common/backoff.ts b/src/common/backoff.ts new file mode 100644 index 00000000..fb8da586 --- /dev/null +++ b/src/common/backoff.ts @@ -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 + } +} diff --git a/src/common/connection.ts b/src/common/connection.ts index f8192f37..43fc047e 100644 --- a/src/common/connection.ts +++ b/src/common/connection.ts @@ -13,7 +13,7 @@ import { RippledNotInitializedError, RippleError } from './errors' -const ExponentialBackoff = require('backo') +import {ExponentialBackoff} from './backoff'; /** * ConnectionOptions is the configuration for the Connection class. diff --git a/test/backoff-test.ts b/test/backoff-test.ts new file mode 100644 index 00000000..a5121d7b --- /dev/null +++ b/test/backoff-test.ts @@ -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) + }) +}) diff --git a/yarn.lock b/yarn.lock index 6a5ba502..f528fb75 100644 --- a/yarn.lock +++ b/yarn.lock @@ -561,11 +561,6 @@ babel-runtime@^6.6.1: core-js "^2.4.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: version "1.0.0" resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"