@@ -139,11 +139,26 @@ var sessionStore = new MongoStore({ mongooseConnection: db });
139
139
var ensureIntegerOrNull = require ( './libs/helpers' ) . ensureIntegerOrNull ;
140
140
141
141
var maxLag = ensureIntegerOrNull ( process . env . BUSY_MAXLAG ) || 70 ;
142
+ var pollInterval = ensureIntegerOrNull ( process . env . BUSY_INTERVAL ) || 500 ;
143
+
144
+ toobusy . maxLag ( maxLag ) ;
145
+ toobusy . interval ( pollInterval ) ;
146
+
147
+ if ( isDbg ) {
148
+ toobusy . onLag ( function ( aCurrentLag ) {
149
+ console . warn ( 'Event loop lag detected! Latency:' , aCurrentLag + 'ms' ) ;
150
+ } ) ;
151
+ }
152
+
153
+ var hostMaxMem = ensureIntegerOrNull ( process . env . HOST_MAXMEM_BYTES ) || 1073741824 ; // 1GiB default
154
+ var maxMem = ensureIntegerOrNull ( process . env . BUSY_MAXMEM ) || 50 ; // 50% default
155
+
156
+ var forceBusyAbsolute = process . env . FORCE_BUSY_ABSOLUTE === 'true' ;
157
+ var forceBusy = process . env . FORCE_BUSY === 'true' ;
158
+
142
159
app . use ( function ( aReq , aRes , aNext ) {
143
160
var pathname = aReq . _parsedUrl . pathname ;
144
- var hostMaxMem = process . env . HOST_MAXMEM_BYTES || 1073741824 ; // NOTE: Default 1GiB
145
161
var usedMem = null ;
146
- var maxMem = null ;
147
162
var isSources = null ;
148
163
149
164
if (
@@ -160,15 +175,15 @@ app.use(function (aReq, aRes, aNext) {
160
175
/ ^ \/ (?: a d m i n | m o d ) / . test ( pathname )
161
176
162
177
) {
163
- aNext ( ) ; // NOTE: Allow styling to pass through on these routes
178
+ aNext ( ) ; // NOTE: Allow to pass through on these routes
164
179
return ;
165
180
}
166
181
167
- if ( process . env . FORCE_BUSY_ABSOLUTE === 'true' ) { // Always busy
182
+ if ( forceBusyAbsolute ) { // Always busy
168
183
aRes . status ( 503 ) . send ( ) ; // NOTE: No UI period just response header
169
184
return ;
170
185
171
- } else if ( process . env . FORCE_BUSY === 'true' ) { // Graceful busy
186
+ } else if ( forceBusy ) { // Graceful busy
172
187
statusCodePage ( aReq , aRes , aNext , {
173
188
statusCode : 503 ,
174
189
statusMessage :
@@ -183,24 +198,25 @@ app.use(function (aReq, aRes, aNext) {
183
198
// Calculate current whole percentage of RSS memory used
184
199
usedMem = parseInt ( process . memoryUsage ( ) . rss / hostMaxMem * 100 ) ;
185
200
186
- // Compare current RSS memory used to maximum
187
- maxMem = ensureIntegerOrNull ( process . env . BUSY_MAXMEM ) ;
188
- if ( usedMem > ( isSources ? ( parseInt ( maxMem / 3 * 2 ) || 50 ) : ( maxMem || 75 ) ) ) {
201
+ // Compare current RSS memory percentage used to maximum percentage
202
+ if ( usedMem > maxMem ) {
189
203
statusCodePage ( aReq , aRes , aNext , {
190
204
statusCode : 503 ,
191
- statusMessage : 'We are very busy right now. Please try again later.'
205
+ statusMessage : 'We are very busy right now\u2026 Please try again later.'
192
206
} ) ;
193
207
return ;
194
208
}
195
209
}
196
210
197
- if ( toobusy ( ) ) { // check if we're toobusy
211
+ if ( toobusy ( ) ) { // check if toobusy
198
212
statusCodePage ( aReq , aRes , aNext , {
199
213
statusCode : 503 ,
200
214
statusMessage : 'We are very busy right now. Please try again later.'
201
215
} ) ;
216
+ return ;
202
217
} else {
203
218
aNext ( ) ; // not toobusy
219
+ // fallthrough
204
220
}
205
221
}
206
222
} ) ;
0 commit comments