1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | var events = require('events');
|
7 | var util = require('util');
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | function Backoff(backoffStrategy) {
|
15 | events.EventEmitter.call(this);
|
16 |
|
17 | this.backoffStrategy_ = backoffStrategy;
|
18 | this.maxNumberOfRetry_ = -1;
|
19 | this.backoffNumber_ = 0;
|
20 | this.backoffDelay_ = 0;
|
21 | this.timeoutID_ = -1;
|
22 |
|
23 | this.handlers = {
|
24 | backoff: this.onBackoff_.bind(this)
|
25 | };
|
26 | }
|
27 | util.inherits(Backoff, events.EventEmitter);
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | Backoff.prototype.failAfter = function(maxNumberOfRetry) {
|
35 | if (maxNumberOfRetry < 1) {
|
36 | throw new Error('Maximum number of retry must be greater than 0. ' +
|
37 | 'Actual: ' + maxNumberOfRetry);
|
38 | }
|
39 |
|
40 | this.maxNumberOfRetry_ = maxNumberOfRetry;
|
41 | };
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | Backoff.prototype.backoff = function(err) {
|
49 | if (this.timeoutID_ !== -1) {
|
50 | throw new Error('Backoff in progress.');
|
51 | }
|
52 |
|
53 | if (this.backoffNumber_ === this.maxNumberOfRetry_) {
|
54 | this.emit('fail', err);
|
55 | this.reset();
|
56 | } else {
|
57 | this.backoffDelay_ = this.backoffStrategy_.next();
|
58 | this.timeoutID_ = setTimeout(this.handlers.backoff, this.backoffDelay_);
|
59 | this.emit('backoff', this.backoffNumber_, this.backoffDelay_, err);
|
60 | }
|
61 | };
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | Backoff.prototype.onBackoff_ = function() {
|
68 | this.timeoutID_ = -1;
|
69 | this.emit('ready', this.backoffNumber_, this.backoffDelay_);
|
70 | this.backoffNumber_++;
|
71 | };
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | Backoff.prototype.reset = function() {
|
78 | this.backoffNumber_ = 0;
|
79 | this.backoffStrategy_.reset();
|
80 | clearTimeout(this.timeoutID_);
|
81 | this.timeoutID_ = -1;
|
82 | };
|
83 |
|
84 | module.exports = Backoff;
|