mirror of
https://github.com/meteor/meteor.git
synced 2026-05-02 03:01:46 -04:00
Remove underscore from retry package.
This commit is contained in:
@@ -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']);
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user