| 1 |
<!DOCTYPE html> |
| 2 |
|
| 3 |
<html> |
| 4 |
<head> |
| 5 |
<title>strategy.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>strategy.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> events = <span class="hljs-built_in">require</span>(<span class="hljs-string">'events'</span>); |
| 78 |
<span class="hljs-keyword">var</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>); |
| 79 |
|
| 80 |
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">isDef</span><span class="hljs-params">(value)</span> {</span> |
| 81 |
<span class="hljs-keyword">return</span> value !== <span class="hljs-literal">undefined</span> && value !== <span class="hljs-literal">null</span>; |
| 82 |
}</pre></div></div> |
| 83 |
|
| 84 |
</li> |
| 85 |
|
| 86 |
|
| 87 |
<li id="section-2"> |
| 88 |
<div class="annotation"> |
| 89 |
|
| 90 |
<div class="pilwrap "> |
| 91 |
<a class="pilcrow" href="#section-2">¶</a> |
| 92 |
</div> |
| 93 |
<p>Abstract class defining the skeleton for the backoff strategies. Accepts an |
| 94 |
object holding the options for the backoff strategy:</p> |
| 95 |
<ul> |
| 96 |
<li><code>randomisationFactor</code>: The randomisation factor which must be between 0 |
| 97 |
and 1 where 1 equates to a randomization factor of 100% and 0 to no |
| 98 |
randomization.</li> |
| 99 |
<li><code>initialDelay</code>: The backoff initial delay in milliseconds.</li> |
| 100 |
<li><code>maxDelay</code>: The backoff maximal delay in milliseconds.</li> |
| 101 |
</ul> |
| 102 |
|
| 103 |
</div> |
| 104 |
|
| 105 |
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">BackoffStrategy</span><span class="hljs-params">(options)</span> {</span> |
| 106 |
options = options || {}; |
| 107 |
|
| 108 |
<span class="hljs-keyword">if</span> (isDef(options.initialDelay) && options.initialDelay < <span class="hljs-number">1</span>) { |
| 109 |
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">'The initial timeout must be greater than 0.'</span>); |
| 110 |
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (isDef(options.maxDelay) && options.maxDelay < <span class="hljs-number">1</span>) { |
| 111 |
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">'The maximal timeout must be greater than 0.'</span>); |
| 112 |
} |
| 113 |
|
| 114 |
<span class="hljs-keyword">this</span>.initialDelay_ = options.initialDelay || <span class="hljs-number">100</span>; |
| 115 |
<span class="hljs-keyword">this</span>.maxDelay_ = options.maxDelay || <span class="hljs-number">10000</span>; |
| 116 |
|
| 117 |
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.maxDelay_ <= <span class="hljs-keyword">this</span>.initialDelay_) { |
| 118 |
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">'The maximal backoff delay must be '</span> + |
| 119 |
<span class="hljs-string">'greater than the initial backoff delay.'</span>); |
| 120 |
} |
| 121 |
|
| 122 |
<span class="hljs-keyword">if</span> (isDef(options.randomisationFactor) && |
| 123 |
(options.randomisationFactor < <span class="hljs-number">0</span> || options.randomisationFactor > <span class="hljs-number">1</span>)) { |
| 124 |
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">'The randomisation factor must be between 0 and 1.'</span>); |
| 125 |
} |
| 126 |
|
| 127 |
<span class="hljs-keyword">this</span>.randomisationFactor_ = options.randomisationFactor || <span class="hljs-number">0</span>; |
| 128 |
}</pre></div></div> |
| 129 |
|
| 130 |
</li> |
| 131 |
|
| 132 |
|
| 133 |
<li id="section-3"> |
| 134 |
<div class="annotation"> |
| 135 |
|
| 136 |
<div class="pilwrap "> |
| 137 |
<a class="pilcrow" href="#section-3">¶</a> |
| 138 |
</div> |
| 139 |
<p>Gets the maximal backoff delay.</p> |
| 140 |
|
| 141 |
</div> |
| 142 |
|
| 143 |
<div class="content"><div class='highlight'><pre>BackoffStrategy.prototype.getMaxDelay = <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">()</span> {</span> |
| 144 |
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.maxDelay_; |
| 145 |
};</pre></div></div> |
| 146 |
|
| 147 |
</li> |
| 148 |
|
| 149 |
|
| 150 |
<li id="section-4"> |
| 151 |
<div class="annotation"> |
| 152 |
|
| 153 |
<div class="pilwrap "> |
| 154 |
<a class="pilcrow" href="#section-4">¶</a> |
| 155 |
</div> |
| 156 |
<p>Gets the initial backoff delay.</p> |
| 157 |
|
| 158 |
</div> |
| 159 |
|
| 160 |
<div class="content"><div class='highlight'><pre>BackoffStrategy.prototype.getInitialDelay = <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">()</span> {</span> |
| 161 |
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.initialDelay_; |
| 162 |
};</pre></div></div> |
| 163 |
|
| 164 |
</li> |
| 165 |
|
| 166 |
|
| 167 |
<li id="section-5"> |
| 168 |
<div class="annotation"> |
| 169 |
|
| 170 |
<div class="pilwrap "> |
| 171 |
<a class="pilcrow" href="#section-5">¶</a> |
| 172 |
</div> |
| 173 |
<p>Template method that computes and returns the next backoff delay in |
| 174 |
milliseconds.</p> |
| 175 |
|
| 176 |
</div> |
| 177 |
|
| 178 |
<div class="content"><div class='highlight'><pre>BackoffStrategy.prototype.next = <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">()</span> {</span> |
| 179 |
<span class="hljs-keyword">var</span> backoffDelay = <span class="hljs-keyword">this</span>.next_(); |
| 180 |
<span class="hljs-keyword">var</span> randomisationMultiple = <span class="hljs-number">1</span> + <span class="hljs-built_in">Math</span>.random() * <span class="hljs-keyword">this</span>.randomisationFactor_; |
| 181 |
<span class="hljs-keyword">var</span> randomizedDelay = <span class="hljs-built_in">Math</span>.round(backoffDelay * randomisationMultiple); |
| 182 |
<span class="hljs-keyword">return</span> randomizedDelay; |
| 183 |
};</pre></div></div> |
| 184 |
|
| 185 |
</li> |
| 186 |
|
| 187 |
|
| 188 |
<li id="section-6"> |
| 189 |
<div class="annotation"> |
| 190 |
|
| 191 |
<div class="pilwrap "> |
| 192 |
<a class="pilcrow" href="#section-6">¶</a> |
| 193 |
</div> |
| 194 |
<p>Computes and returns the next backoff delay. Intended to be overridden by |
| 195 |
subclasses.</p> |
| 196 |
|
| 197 |
</div> |
| 198 |
|
| 199 |
<div class="content"><div class='highlight'><pre>BackoffStrategy.prototype.next_ = <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">()</span> {</span> |
| 200 |
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">'BackoffStrategy.next_() unimplemented.'</span>); |
| 201 |
};</pre></div></div> |
| 202 |
|
| 203 |
</li> |
| 204 |
|
| 205 |
|
| 206 |
<li id="section-7"> |
| 207 |
<div class="annotation"> |
| 208 |
|
| 209 |
<div class="pilwrap "> |
| 210 |
<a class="pilcrow" href="#section-7">¶</a> |
| 211 |
</div> |
| 212 |
<p>Template method that resets the backoff delay to its initial value.</p> |
| 213 |
|
| 214 |
</div> |
| 215 |
|
| 216 |
<div class="content"><div class='highlight'><pre>BackoffStrategy.prototype.reset = <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">()</span> {</span> |
| 217 |
<span class="hljs-keyword">this</span>.reset_(); |
| 218 |
};</pre></div></div> |
| 219 |
|
| 220 |
</li> |
| 221 |
|
| 222 |
|
| 223 |
<li id="section-8"> |
| 224 |
<div class="annotation"> |
| 225 |
|
| 226 |
<div class="pilwrap "> |
| 227 |
<a class="pilcrow" href="#section-8">¶</a> |
| 228 |
</div> |
| 229 |
<p>Resets the backoff delay to its initial value. Intended to be overridden by |
| 230 |
subclasses.</p> |
| 231 |
|
| 232 |
</div> |
| 233 |
|
| 234 |
<div class="content"><div class='highlight'><pre>BackoffStrategy.prototype.reset_ = <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">()</span> {</span> |
| 235 |
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">'BackoffStrategy.reset_() unimplemented.'</span>); |
| 236 |
}; |
| 237 |
|
| 238 |
module.exports = BackoffStrategy;</pre></div></div> |
| 239 |
|
| 240 |
</li> |
| 241 |
|
| 242 |
</ul> |
| 243 |
</div> |
| 244 |
</body> |
| 245 |
</html> |