| 1 |
<!DOCTYPE html> |
| 2 |
|
| 3 |
<html> |
| 4 |
<head> |
| 5 |
<title>exponential.js</title> |
| 6 |
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> |
| 7 |
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"> |
| 8 |
<link rel="stylesheet" media="all" href="docco.css" /> |
| 9 |
</head> |
| 10 |
<body> |
| 11 |
<div id="container"> |
| 12 |
<div id="background"></div> |
| 13 |
|
| 14 |
<ul id="jump_to"> |
| 15 |
<li> |
| 16 |
<a class="large" href="javascript:void(0);">Jump To …</a> |
| 17 |
<a class="small" href="javascript:void(0);">+</a> |
| 18 |
<div id="jump_wrapper"> |
| 19 |
<div id="jump_page"> |
| 20 |
|
| 21 |
|
| 22 |
<a class="source" href="index.html"> |
| 23 |
index.js |
| 24 |
</a> |
| 25 |
|
| 26 |
|
| 27 |
<a class="source" href="backoff.html"> |
| 28 |
backoff.js |
| 29 |
</a> |
| 30 |
|
| 31 |
|
| 32 |
<a class="source" href="function_call.html"> |
| 33 |
function_call.js |
| 34 |
</a> |
| 35 |
|
| 36 |
|
| 37 |
<a class="source" href="exponential.html"> |
| 38 |
exponential.js |
| 39 |
</a> |
| 40 |
|
| 41 |
|
| 42 |
<a class="source" href="fibonacci.html"> |
| 43 |
fibonacci.js |
| 44 |
</a> |
| 45 |
|
| 46 |
|
| 47 |
<a class="source" href="strategy.html"> |
| 48 |
strategy.js |
| 49 |
</a> |
| 50 |
|
| 51 |
</div> |
| 52 |
</li> |
| 53 |
</ul> |
| 54 |
|
| 55 |
<ul class="sections"> |
| 56 |
|
| 57 |
<li id="title"> |
| 58 |
<div class="annotation"> |
| 59 |
<h1>exponential.js</h1> |
| 60 |
</div> |
| 61 |
</li> |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
<li id="section-1"> |
| 66 |
<div class="annotation"> |
| 67 |
|
| 68 |
<div class="pilwrap "> |
| 69 |
<a class="pilcrow" href="#section-1">¶</a> |
| 70 |
</div> |
| 71 |
<pre><code> Copyright (c) <span class="hljs-number">2012</span> Mathieu Turcotte |
| 72 |
Licensed under the MIT license. |
| 73 |
</code></pre> |
| 74 |
</div> |
| 75 |
|
| 76 |
<div class="content"><div class='highlight'><pre> |
| 77 |
<span class="hljs-keyword">var</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>); |
| 78 |
<span class="hljs-keyword">var</span> precond = <span class="hljs-built_in">require</span>(<span class="hljs-string">'precond'</span>); |
| 79 |
|
| 80 |
<span class="hljs-keyword">var</span> BackoffStrategy = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./strategy'</span>);</pre></div></div> |
| 81 |
|
| 82 |
</li> |
| 83 |
|
| 84 |
|
| 85 |
<li id="section-2"> |
| 86 |
<div class="annotation"> |
| 87 |
|
| 88 |
<div class="pilwrap "> |
| 89 |
<a class="pilcrow" href="#section-2">¶</a> |
| 90 |
</div> |
| 91 |
<p>Exponential backoff strategy.</p> |
| 92 |
|
| 93 |
</div> |
| 94 |
|
| 95 |
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">ExponentialBackoffStrategy</span><span class="hljs-params">(options)</span> {</span> |
| 96 |
BackoffStrategy.call(<span class="hljs-keyword">this</span>, options); |
| 97 |
<span class="hljs-keyword">this</span>.backoffDelay_ = <span class="hljs-number">0</span>; |
| 98 |
<span class="hljs-keyword">this</span>.nextBackoffDelay_ = <span class="hljs-keyword">this</span>.getInitialDelay(); |
| 99 |
<span class="hljs-keyword">this</span>.factor_ = ExponentialBackoffStrategy.DEFAULT_FACTOR; |
| 100 |
|
| 101 |
<span class="hljs-keyword">if</span> (options && options.factor !== <span class="hljs-literal">undefined</span>) { |
| 102 |
precond.checkArgument(options.factor > <span class="hljs-number">1</span>, |
| 103 |
<span class="hljs-string">'Exponential factor should be greater than 1 but got %s.'</span>, |
| 104 |
options.factor); |
| 105 |
<span class="hljs-keyword">this</span>.factor_ = options.factor; |
| 106 |
} |
| 107 |
} |
| 108 |
util.inherits(ExponentialBackoffStrategy, BackoffStrategy);</pre></div></div> |
| 109 |
|
| 110 |
</li> |
| 111 |
|
| 112 |
|
| 113 |
<li id="section-3"> |
| 114 |
<div class="annotation"> |
| 115 |
|
| 116 |
<div class="pilwrap "> |
| 117 |
<a class="pilcrow" href="#section-3">¶</a> |
| 118 |
</div> |
| 119 |
<p>Default multiplication factor used to compute the next backoff delay from |
| 120 |
the current one. The value can be overridden by passing a custom factor as |
| 121 |
part of the options.</p> |
| 122 |
|
| 123 |
</div> |
| 124 |
|
| 125 |
<div class="content"><div class='highlight'><pre>ExponentialBackoffStrategy.DEFAULT_FACTOR = <span class="hljs-number">2</span>; |
| 126 |
|
| 127 |
ExponentialBackoffStrategy.prototype.next_ = <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">()</span> {</span> |
| 128 |
<span class="hljs-keyword">this</span>.backoffDelay_ = <span class="hljs-built_in">Math</span>.min(<span class="hljs-keyword">this</span>.nextBackoffDelay_, <span class="hljs-keyword">this</span>.getMaxDelay()); |
| 129 |
<span class="hljs-keyword">this</span>.nextBackoffDelay_ = <span class="hljs-keyword">this</span>.backoffDelay_ * <span class="hljs-keyword">this</span>.factor_; |
| 130 |
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.backoffDelay_; |
| 131 |
}; |
| 132 |
|
| 133 |
ExponentialBackoffStrategy.prototype.reset_ = <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">()</span> {</span> |
| 134 |
<span class="hljs-keyword">this</span>.backoffDelay_ = <span class="hljs-number">0</span>; |
| 135 |
<span class="hljs-keyword">this</span>.nextBackoffDelay_ = <span class="hljs-keyword">this</span>.getInitialDelay(); |
| 136 |
}; |
| 137 |
|
| 138 |
module.exports = ExponentialBackoffStrategy;</pre></div></div> |
| 139 |
|
| 140 |
</li> |
| 141 |
|
| 142 |
</ul> |
| 143 |
</div> |
| 144 |
</body> |
| 145 |
</html> |