From b9d562d29d284ceec2ee7b990cc688a9b34092ea Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Mon, 13 Nov 2017 20:12:34 -0500 Subject: [PATCH] Remove underscore from retry package. --- packages/retry/package.js | 5 ++- packages/retry/retry.js | 82 ++++++++++++++++++++------------------- 2 files changed, 45 insertions(+), 42 deletions(-) diff --git a/packages/retry/package.js b/packages/retry/package.js index dfb443fe47..01b06a6391 100644 --- a/packages/retry/package.js +++ b/packages/retry/package.js @@ -4,7 +4,8 @@ Package.describe({ }); Package.onUse(function (api) { - api.use(['underscore', 'random'], ['client', 'server']); + api.use('ecmascript'); + api.use('random'); + api.mainModule('retry.js'); api.export('Retry'); - api.addFiles('retry.js', ['client', 'server']); }); diff --git a/packages/retry/retry.js b/packages/retry/retry.js index 4a377bca4f..48e62adc4a 100644 --- a/packages/retry/retry.js +++ b/packages/retry/retry.js @@ -8,57 +8,59 @@ // minTimeout: time to wait for the first `minCount` retries (ms). // fuzz: factor to randomize retry times by (to avoid retry storms). -Retry = function (options) { - var self = this; - _.extend(self, _.defaults(_.clone(options || {}), { - baseTimeout: 1000, // 1 second - exponent: 2.2, +export class Retry { + constructor({ + baseTimeout = 1000, + exponent = 2.2, // The default is high-ish to ensure a server can recover from a // failure caused by load. - maxTimeout: 5 * 60000, // 5 minutes - minTimeout: 10, - minCount: 2, - fuzz: 0.5 // +- 25% - })); - self.retryTimer = null; -}; - -_.extend(Retry.prototype, { + maxTimeout = 5 * 60 * 1000, + minTimeout = 10, + minCount = 2, + fuzz = 0.5, + } = {}) { + this.baseTimeout = baseTimeout; + this.exponent = exponent; + this.maxTimeout = maxTimeout; + this.minTimeout = minTimeout; + this.minCount = minCount; + this.fuzz = fuzz; + this.retryTimer = null; + } // Reset a pending retry, if any. - clear: function () { - var self = this; - if (self.retryTimer) - clearTimeout(self.retryTimer); - self.retryTimer = null; - }, + clear() { + if (this.retryTimer) { + clearTimeout(this.retryTimer); + } + this.retryTimer = null; + } // Calculate how long to wait in milliseconds to retry, based on the // `count` of which retry this is. - _timeout: function (count) { - var self = this; + _timeout(count) { + if (count < this.minCount) { + return this.minTimeout; + } - if (count < self.minCount) - return self.minTimeout; - - var timeout = Math.min( - self.maxTimeout, - self.baseTimeout * Math.pow(self.exponent, count)); // fuzz the timeout randomly, to avoid reconnect storms when a // server goes down. - timeout = timeout * ((Random.fraction() * self.fuzz) + - (1 - self.fuzz/2)); - return timeout; - }, + var timeout = Math.min( + this.maxTimeout, + this.baseTimeout * Math.pow(this.exponent, count) + ) * ( + Random.fraction() * this.fuzz + (1 - this.fuzz / 2) + ); - // Call `fn` after a delay, based on the `count` of which retry this is. - retryLater: function (count, fn) { - var self = this; - var timeout = self._timeout(count); - if (self.retryTimer) - clearTimeout(self.retryTimer); - self.retryTimer = Meteor.setTimeout(fn, timeout); return timeout; } -}); + // Call `fn` after a delay, based on the `count` of which retry this is. + retryLater(count, fn) { + var timeout = this._timeout(count); + if (this.retryTimer) + clearTimeout(this.retryTimer); + this.retryTimer = Meteor.setTimeout(fn, timeout); + return timeout; + } +}