| 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231 |
1
1
1
1
1
1
1
1
1
1
1
1
1
602
1
563
563
563
563
563
563
563
563
563
563
563
563
1
1
1
1
836
836
836
5
5
3
3
2
2
2
833
1
742
742
742
1
40
40
40
40
32
1
3
3
3
3
3
3
3
3
3
3
1
1
1
1
20
20
20
7
7
20
6
14
1
7
7
1
7
7
7
1
1
1
1
1
1
1
1
1
1
1
1
| // Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
'use strict';
var format = require('util').format;
var inherits = require('util').inherits;
var errors = require('./errors');
module.exports.HealthyState = HealthyState;
module.exports.UnhealthyState = UnhealthyState;
module.exports.LockedHealthyState = LockedHealthyState;
module.exports.LockedUnhealthyState = LockedUnhealthyState;
/*
* Collectively, the health states receive additional options through the peer
* options:
* - maxErrorRate (error rate to go from healthy to unhealthy)
* - minResponseCount (response count to go from unhealthy to healthy)
* - TODO
*
* They also inherit:
* - channel.timers
* - channel.random
*/
var symptoms = {
'BadRequest': false, // not an indicator of bad health
'Cancelled': false, // not an indicator of bad health
'Timeout': true, // TODO throttle
'Busy': true, // TODO throttle
'Declined': true,
'UnexpectedError': true,
'NetworkError': true,
'FatalProtocolError': true
};
function State() {
}
State.prototype.onRequest = function onRequest(/* req */) {
};
State.prototype.onRequestResponse = function onRequestResponse(/* req */) {
};
State.prototype.onRequestError = function onRequestError(err) {
};
State.prototype.close = function close(callback) {
callback(null);
};
function HealthyState(options) {
var self = this;
self.stateMachine = options.stateMachine;
self.nextHandler = options.nextHandler;
self.timers = options.timers;
self.random = options.random;
self.period = options.period || 1000; // ms
self.start = self.timers.now();
self.maxErrorRate = options.maxErrorRate || 0.5;
self.okCount = 0;
self.notOkCount = 0;
self.totalReqs = 0;
self.minRequests = typeof options.minRequests === 'number' ?
options.minRequests : 5;
}
inherits(HealthyState, State);
HealthyState.prototype.type = 'tchannel.healthy';
HealthyState.prototype.toString = function healthyToString() {
var self = this;
return format('[Healthy %s ok %s err]', self.okCount, self.notOkCount);
};
HealthyState.prototype.shouldRequest = function shouldRequest(req, options) {
var self = this;
var now = self.timers.now();
// At the conclusion of a period
if (now - self.start >= self.period) {
var totalCount = self.okCount + self.notOkCount;
// Transition to unhealthy state if the success rate dips below the
// acceptable threshold.
if (self.notOkCount / totalCount > self.maxErrorRate &&
self.totalReqs > self.minRequests
) {
self.stateMachine.setState(UnhealthyState);
return 0;
}
// Alternatley, start a new monitoring period.
self.start = self.timers.now();
self.okCount = 0;
self.notOkCount = 0;
}
return self.stateMachine.shouldRequest();
};
HealthyState.prototype.onRequestResponse = function onRequestResponse(/* req */) {
var self = this;
self.okCount++;
self.totalReqs++;
};
HealthyState.prototype.onRequestError = function onRequestError(err) {
var self = this;
self.totalReqs++;
var codeString = errors.classify(err);
if (symptoms[codeString]) {
self.notOkCount++;
}
};
function UnhealthyState(options) {
var self = this;
self.stateMachine = options.stateMachine;
self.nextHandler = options.nextHandler;
self.timers = options.timers;
self.random = options.random;
self.minResponseCount = options.probation || 5;
self.period = options.period || 1000;
self.start = self.timers.now();
self.successCount = 0;
self.triedThisPeriod = true;
}
inherits(UnhealthyState, State);
UnhealthyState.prototype.type = 'tchannel.unhealthy';
UnhealthyState.prototype.toString = function healthyToString() {
var self = this;
return format('[Unhealthy %s ok]', self.successCount);
};
UnhealthyState.prototype.shouldRequest = function shouldRequest(req, options) {
var self = this;
// Start a new period if the previous has concluded
var now = self.timers.now();
if (now - self.start >= self.period) {
self.start = self.timers.now();
self.triedThisPeriod = false;
}
// Allow one trial per period
if (self.triedThisPeriod) {
return 0;
}
return self.nextHandler.shouldRequest();
};
UnhealthyState.prototype.onRequest = function onRequest(/* req */) {
var self = this;
self.triedThisPeriod = true;
};
UnhealthyState.prototype.onRequestResponse = function onRequestResponse(/* req */) {
var self = this;
self.successCount++;
if (self.successCount > self.minResponseCount) {
self.stateMachine.setState(HealthyState);
}
};
UnhealthyState.prototype.onRequestError = function onRequestError(err) {
var self = this;
var codeString = errors.classify(err);
if (symptoms[codeString]) {
self.successCount = 0;
}
};
function LockedHealthyState(options) {
var self = this;
self.nextHandler = options.nextHandler;
}
inherits(LockedHealthyState, State);
LockedHealthyState.prototype.type = 'tchannel.healthy-locked';
LockedHealthyState.prototype.toString = function lockedHealthyToString() {
return '[Healthy state (locked)]';
};
LockedHealthyState.prototype.shouldRequest = function shouldRequest(req, options) {
var self = this;
return self.nextHandler.shouldRequest(req, options);
};
function LockedUnhealthyState(options) {
var self = this;
self.nextHandler = options.nextHandler;
}
inherits(LockedUnhealthyState, State);
LockedUnhealthyState.prototype.type = 'tchannel.unhealthy-locked';
LockedUnhealthyState.prototype.toString = function lockedUnhealthyToString() {
return '[Unhealthy state (locked)]';
};
LockedUnhealthyState.prototype.shouldRequest = function shouldRequest(req, options) {
return 0;
};
|