1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
<!DOCTYPE html>
<html>
<head>
<title>backoff.js</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
<link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
<div id="container">
<div id="background"></div>
<ul id="jump_to">
<li>
<a class="large" href="javascript:void(0);">Jump To …</a>
<a class="small" href="javascript:void(0);">+</a>
<div id="jump_wrapper">
<div id="jump_page">
<a class="source" href="index.html">
index.js
</a>
<a class="source" href="backoff.html">
backoff.js
</a>
<a class="source" href="function_call.html">
function_call.js
</a>
<a class="source" href="exponential.html">
exponential.js
</a>
<a class="source" href="fibonacci.html">
fibonacci.js
</a>
<a class="source" href="strategy.html">
strategy.js
</a>
</div>
</li>
</ul>
<ul class="sections">
<li id="title">
<div class="annotation">
<h1>backoff.js</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
<pre><code> Copyright (c) <span class="hljs-number">2012</span> Mathieu Turcotte
Licensed under the MIT license.
</code></pre>
</div>
<div class="content"><div class='highlight'><pre>
<span class="hljs-keyword">var</span> events = <span class="hljs-built_in">require</span>(<span class="hljs-string">'events'</span>);
<span class="hljs-keyword">var</span> precond = <span class="hljs-built_in">require</span>(<span class="hljs-string">'precond'</span>);
<span class="hljs-keyword">var</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);</pre></div></div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<p>A class to hold the state of a backoff operation. Accepts a backoff strategy
to generate the backoff delays.</p>
</div>
<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>
events.EventEmitter.call(<span class="hljs-keyword">this</span>);
<span class="hljs-keyword">this</span>.backoffStrategy_ = backoffStrategy;
<span class="hljs-keyword">this</span>.maxNumberOfRetry_ = -<span class="hljs-number">1</span>;
<span class="hljs-keyword">this</span>.backoffNumber_ = <span class="hljs-number">0</span>;
<span class="hljs-keyword">this</span>.backoffDelay_ = <span class="hljs-number">0</span>;
<span class="hljs-keyword">this</span>.timeoutID_ = -<span class="hljs-number">1</span>;
<span class="hljs-keyword">this</span>.handlers = {
backoff: <span class="hljs-keyword">this</span>.onBackoff_.bind(<span class="hljs-keyword">this</span>)
};
}
util.inherits(Backoff, events.EventEmitter);</pre></div></div>
</li>
<li id="section-3">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-3">¶</a>
</div>
<p>Sets a limit, greater than 0, on the maximum number of backoffs. A ‘fail’
event will be emitted when the limit is reached.</p>
</div>
<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>
precond.checkArgument(maxNumberOfRetry > <span class="hljs-number">0</span>,
<span class="hljs-string">'Expected a maximum number of retry greater than 0 but got %s.'</span>,
maxNumberOfRetry);
<span class="hljs-keyword">this</span>.maxNumberOfRetry_ = maxNumberOfRetry;
};</pre></div></div>
</li>
<li id="section-4">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<p>Starts a backoff operation. Accepts an optional parameter to let the
listeners know why the backoff operation was started.</p>
</div>
<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>
precond.checkState(<span class="hljs-keyword">this</span>.timeoutID_ === -<span class="hljs-number">1</span>, <span class="hljs-string">'Backoff in progress.'</span>);
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.backoffNumber_ === <span class="hljs-keyword">this</span>.maxNumberOfRetry_) {
<span class="hljs-keyword">this</span>.emit(<span class="hljs-string">'fail'</span>, err);
<span class="hljs-keyword">this</span>.reset();
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">this</span>.backoffDelay_ = <span class="hljs-keyword">this</span>.backoffStrategy_.next();
<span class="hljs-keyword">this</span>.timeoutID_ = setTimeout(<span class="hljs-keyword">this</span>.handlers.backoff, <span class="hljs-keyword">this</span>.backoffDelay_);
<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);
}
};</pre></div></div>
</li>
<li id="section-5">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<p>Handles the backoff timeout completion.</p>
</div>
<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>
<span class="hljs-keyword">this</span>.timeoutID_ = -<span class="hljs-number">1</span>;
<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_);
<span class="hljs-keyword">this</span>.backoffNumber_++;
};</pre></div></div>
</li>
<li id="section-6">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-6">¶</a>
</div>
<p>Stops any backoff operation and resets the backoff delay to its inital value.</p>
</div>
<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>
<span class="hljs-keyword">this</span>.backoffNumber_ = <span class="hljs-number">0</span>;
<span class="hljs-keyword">this</span>.backoffStrategy_.reset();
clearTimeout(<span class="hljs-keyword">this</span>.timeoutID_);
<span class="hljs-keyword">this</span>.timeoutID_ = -<span class="hljs-number">1</span>;
};
module.exports = Backoff;</pre></div></div>
</li>
</ul>
</div>
</body>
</html>
|