123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- goog.provide('goog.math.ExponentialBackoff');
- goog.require('goog.asserts');
- goog.math.ExponentialBackoff = function(
- initialValue, maxValue, opt_randomFactor, opt_backoffFactor,
- opt_decayFactor) {
- goog.asserts.assert(
- initialValue > 0, 'Initial value must be greater than zero.');
- goog.asserts.assert(
- maxValue >= initialValue,
- 'Max value should be at least as large as initial value.');
- if (goog.isDef(opt_randomFactor)) {
- goog.asserts.assert(
- opt_randomFactor >= 0 && opt_randomFactor <= 1,
- 'Randomness factor should be between 0 and 1.');
- }
- if (goog.isDef(opt_backoffFactor)) {
- goog.asserts.assert(
- opt_backoffFactor > 1, 'Backoff factor should be greater than 1');
- }
- if (goog.isDef(opt_decayFactor)) {
- goog.asserts.assert(
- opt_decayFactor >= 1, 'Decay factor should be greater than 1');
- }
-
- this.initialValue_ = initialValue;
-
- this.maxValue_ = maxValue;
-
- this.currValue_ = initialValue;
-
- this.currBaseValue_ = initialValue;
-
- this.randomFactor_ = opt_randomFactor || 0;
-
- this.backoffFactor_ = opt_backoffFactor || 2;
-
- this.decayFactor_ = opt_decayFactor || 2;
- };
- goog.math.ExponentialBackoff.prototype.currBackoffCount_ = 0;
- goog.math.ExponentialBackoff.prototype.currDecayCount_ = 0;
- goog.math.ExponentialBackoff.prototype.reset = function() {
- this.currValue_ = this.initialValue_;
- this.currBaseValue_ = this.initialValue_;
- this.currBackoffCount_ = 0;
- this.currDecayCount_ = 0;
- };
- goog.math.ExponentialBackoff.prototype.getValue = function() {
- return this.currValue_;
- };
- goog.math.ExponentialBackoff.prototype.getBackoffCount = function() {
- return this.currBackoffCount_;
- };
- goog.math.ExponentialBackoff.prototype.getDecayCount = function() {
- return this.currDecayCount_;
- };
- goog.math.ExponentialBackoff.prototype.backoff = function() {
-
- this.currBaseValue_ =
- Math.min(this.maxValue_, this.currBaseValue_ * this.backoffFactor_);
- var randomWait = this.randomFactor_ ?
- Math.round(
- this.randomFactor_ * (Math.random() - 0.5) * 2 *
- this.currBaseValue_) :
- 0;
- this.currValue_ = Math.min(this.maxValue_, this.currBaseValue_ + randomWait);
- this.currBackoffCount_++;
- };
- goog.math.ExponentialBackoff.prototype.decay = function() {
-
- this.currBaseValue_ =
- Math.max(this.initialValue_, this.currBaseValue_ / this.decayFactor_);
- var randomWait = this.randomFactor_ ?
- Math.round(
- this.randomFactor_ * (Math.random() - 0.5) * 2 *
- this.currBaseValue_) :
- 0;
- this.currValue_ =
- Math.max(this.initialValue_, this.currBaseValue_ + randomWait);
- this.currDecayCount_++;
- };
|