| 1 |
<!DOCTYPE html> |
| 2 |
|
| 3 |
<html> |
| 4 |
<head> |
| 5 |
<title>backoff.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>backoff.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> precond = <span class="hljs-built_in">require</span>(<span class="hljs-string">'precond'</span>); |
| 79 |
<span class="hljs-keyword">var</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);</pre></div></div> |
| 80 |
|
| 81 |
</li> |
| 82 |
|
| 83 |
|
| 84 |
<li id="section-2"> |
| 85 |
<div class="annotation"> |
| 86 |
|
| 87 |
<div class="pilwrap "> |
| 88 |
<a class="pilcrow" href="#section-2">¶</a> |
| 89 |
</div> |
| 90 |
<p>A class to hold the state of a backoff operation. Accepts a backoff strategy |
| 91 |
to generate the backoff delays.</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">Backoff</span><span class="hljs-params">(backoffStrategy)</span> {</span> |
| 96 |
events.EventEmitter.call(<span class="hljs-keyword">this</span>); |
| 97 |
|
| 98 |
<span class="hljs-keyword">this</span>.backoffStrategy_ = backoffStrategy; |
| 99 |
<span class="hljs-keyword">this</span>.maxNumberOfRetry_ = -<span class="hljs-number">1</span>; |
| 100 |
<span class="hljs-keyword">this</span>.backoffNumber_ = <span class="hljs-number">0</span>; |
| 101 |
<span class="hljs-keyword">this</span>.backoffDelay_ = <span class="hljs-number">0</span>; |
| 102 |
<span class="hljs-keyword">this</span>.timeoutID_ = -<span class="hljs-number">1</span>; |
| 103 |
|
| 104 |
<span class="hljs-keyword">this</span>.handlers = { |
| 105 |
backoff: <span class="hljs-keyword">this</span>.onBackoff_.bind(<span class="hljs-keyword">this</span>) |
| 106 |
}; |
| 107 |
} |
| 108 |
util.inherits(Backoff, events.EventEmitter);</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>Sets a limit, greater than 0, on the maximum number of backoffs. A ‘fail’ |
| 120 |
event will be emitted when the limit is reached.</p> |
| 121 |
|
| 122 |
</div> |
| 123 |
|
| 124 |
<div class="content"><div class='highlight'><pre>Backoff.prototype.failAfter = <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(maxNumberOfRetry)</span> {</span> |
| 125 |
precond.checkArgument(maxNumberOfRetry > <span class="hljs-number">0</span>, |
| 126 |
<span class="hljs-string">'Expected a maximum number of retry greater than 0 but got %s.'</span>, |
| 127 |
maxNumberOfRetry); |
| 128 |
|
| 129 |
<span class="hljs-keyword">this</span>.maxNumberOfRetry_ = maxNumberOfRetry; |
| 130 |
};</pre></div></div> |
| 131 |
|
| 132 |
</li> |
| 133 |
|
| 134 |
|
| 135 |
<li id="section-4"> |
| 136 |
<div class="annotation"> |
| 137 |
|
| 138 |
<div class="pilwrap "> |
| 139 |
<a class="pilcrow" href="#section-4">¶</a> |
| 140 |
</div> |
| 141 |
<p>Starts a backoff operation. Accepts an optional parameter to let the |
| 142 |
listeners know why the backoff operation was started.</p> |
| 143 |
|
| 144 |
</div> |
| 145 |
|
| 146 |
<div class="content"><div class='highlight'><pre>Backoff.prototype.backoff = <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(err)</span> {</span> |
| 147 |
precond.checkState(<span class="hljs-keyword">this</span>.timeoutID_ === -<span class="hljs-number">1</span>, <span class="hljs-string">'Backoff in progress.'</span>); |
| 148 |
|
| 149 |
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.backoffNumber_ === <span class="hljs-keyword">this</span>.maxNumberOfRetry_) { |
| 150 |
<span class="hljs-keyword">this</span>.emit(<span class="hljs-string">'fail'</span>, err); |
| 151 |
<span class="hljs-keyword">this</span>.reset(); |
| 152 |
} <span class="hljs-keyword">else</span> { |
| 153 |
<span class="hljs-keyword">this</span>.backoffDelay_ = <span class="hljs-keyword">this</span>.backoffStrategy_.next(); |
| 154 |
<span class="hljs-keyword">this</span>.timeoutID_ = setTimeout(<span class="hljs-keyword">this</span>.handlers.backoff, <span class="hljs-keyword">this</span>.backoffDelay_); |
| 155 |
<span class="hljs-keyword">this</span>.emit(<span class="hljs-string">'backoff'</span>, <span class="hljs-keyword">this</span>.backoffNumber_, <span class="hljs-keyword">this</span>.backoffDelay_, err); |
| 156 |
} |
| 157 |
};</pre></div></div> |
| 158 |
|
| 159 |
</li> |
| 160 |
|
| 161 |
|
| 162 |
<li id="section-5"> |
| 163 |
<div class="annotation"> |
| 164 |
|
| 165 |
<div class="pilwrap "> |
| 166 |
<a class="pilcrow" href="#section-5">¶</a> |
| 167 |
</div> |
| 168 |
<p>Handles the backoff timeout completion.</p> |
| 169 |
|
| 170 |
</div> |
| 171 |
|
| 172 |
<div class="content"><div class='highlight'><pre>Backoff.prototype.onBackoff_ = <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">()</span> {</span> |
| 173 |
<span class="hljs-keyword">this</span>.timeoutID_ = -<span class="hljs-number">1</span>; |
| 174 |
<span class="hljs-keyword">this</span>.emit(<span class="hljs-string">'ready'</span>, <span class="hljs-keyword">this</span>.backoffNumber_, <span class="hljs-keyword">this</span>.backoffDelay_); |
| 175 |
<span class="hljs-keyword">this</span>.backoffNumber_++; |
| 176 |
};</pre></div></div> |
| 177 |
|
| 178 |
</li> |
| 179 |
|
| 180 |
|
| 181 |
<li id="section-6"> |
| 182 |
<div class="annotation"> |
| 183 |
|
| 184 |
<div class="pilwrap "> |
| 185 |
<a class="pilcrow" href="#section-6">¶</a> |
| 186 |
</div> |
| 187 |
<p>Stops any backoff operation and resets the backoff delay to its inital value.</p> |
| 188 |
|
| 189 |
</div> |
| 190 |
|
| 191 |
<div class="content"><div class='highlight'><pre>Backoff.prototype.reset = <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">()</span> {</span> |
| 192 |
<span class="hljs-keyword">this</span>.backoffNumber_ = <span class="hljs-number">0</span>; |
| 193 |
<span class="hljs-keyword">this</span>.backoffStrategy_.reset(); |
| 194 |
clearTimeout(<span class="hljs-keyword">this</span>.timeoutID_); |
| 195 |
<span class="hljs-keyword">this</span>.timeoutID_ = -<span class="hljs-number">1</span>; |
| 196 |
}; |
| 197 |
|
| 198 |
module.exports = Backoff;</pre></div></div> |
| 199 |
|
| 200 |
</li> |
| 201 |
|
| 202 |
</ul> |
| 203 |
</div> |
| 204 |
</body> |
| 205 |
</html> |