This repository was archived by the owner on Feb 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCircuitBreaker.js
104 lines (85 loc) · 2.88 KB
/
CircuitBreaker.js
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
var util = require('util');
var EventEmitter = require('events').EventEmitter;
CircuitBreaker.states = {
open: 1,
closed: 2
};
function CircuitBreaker(options) {
EventEmitter.call(this);
options = options || {};
this.fn = options.fn || null;
this.failure_rate = options.failure_rate != null ? options.failure_rate : null;
this.failure_count = options.failure_count != null ? options.failure_count : null;
this.reset_timeout = options.reset_timeout != null ? options.reset_timeout : null;
this.reset();
}
util.inherits(CircuitBreaker, EventEmitter);
CircuitBreaker.prototype.trip = function() {
this.state = CircuitBreaker.states.open;
this.start = Date.now();
this.emit('open');
};
CircuitBreaker.prototype.reset = function() {
this.passed = 0;
this.failed = 0;
this.state = CircuitBreaker.states.closed;
this.start = Date.now();
this.emit('close');
};
CircuitBreaker.prototype.pass = function() {
this.passed++;
this.update();
};
CircuitBreaker.prototype.fail = function() {
this.failed++;
this.update();
};
CircuitBreaker.prototype.open = function() {
this.update();
return this.state === CircuitBreaker.states.open;
};
CircuitBreaker.prototype.closed = function() {
this.update();
return this.state === CircuitBreaker.states.closed;
};
CircuitBreaker.prototype.monitor = function(cb) {
var self = this;
return function(err) {
if (err) self.fail();
else self.pass();
if (cb) cb.apply(this, arguments);
}
};
CircuitBreaker.prototype.call = function(/* args */) {
if (arguments.length) {
var last = arguments[arguments.length - 1];
var has_callback = (typeof last === 'function');
if (has_callback) arguments[arguments - 1] = this.monitor(last);
}
var is_closed = this.closed();
if (this.fn && is_closed) return this.fn.apply(null, arguments);
if (!is_closed) {
var err = new Error("CircuitBreakerOpen");
if (has_callback) return last(err);
return err;
}
};
CircuitBreaker.prototype.update = function() {
var should_be_closed = this.detect();
var is_closed = (this.state === CircuitBreaker.states.closed);
if (should_be_closed && !is_closed) this.reset();
if (!should_be_closed && is_closed) this.trip();
if (this.reset_timeout != null && (Date.now() - this.start) > this.reset_timeout)
this.reset();
};
CircuitBreaker.prototype.detect = function() {
var count_exceeded = false;
var rate_exceeded = false;
if (this.failure_count != null && this.failed > this.failure_count) count_exceeded = true;
if (this.failure_rate != null && this.failed && (this.failed / (this.passed + this.failed)) > this.failure_rate) rate_exceeded = true;
if (this.failure_count != null && this.failure_rate != null) return !count_exceeded || !rate_exceeded;
if (this.failure_count != null) return !count_exceeded;
if (this.failure_rate != null) return !rate_exceeded;
return true;
};
module.exports = CircuitBreaker;