-
Notifications
You must be signed in to change notification settings - Fork 374
/
toobusy.ts
45 lines (41 loc) · 1.16 KB
/
toobusy.ts
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
/**
* Filter for toobusy.
* if the process is toobusy, just skip the new request
*/
import { getLogger } from 'pinus-logger';
import { IHandlerFilter } from '../../interfaces/IHandlerFilter';
import { RouteRecord } from '../../util/constants';
import { HandlerCallback } from '../../common/service/handlerService';
import { FrontendOrBackendSession } from '../../server/server';
let conLogger = getLogger('con-log', __filename);
let toobusy : any = null;
let DEFAULT_MAXLAG = 70;
export class ToobusyFilter implements IHandlerFilter
{
constructor(maxLag = DEFAULT_MAXLAG)
{
try
{
toobusy = require('toobusy');
} catch (e)
{
}
if (!!toobusy)
{
toobusy.maxLag(maxLag);
}
};
before(routeRecord : RouteRecord , msg : any, session : FrontendOrBackendSession, next : HandlerCallback)
{
if (!!toobusy && toobusy())
{
conLogger.warn('[toobusy] reject request msg: ' + msg);
let err = new Error('Server toobusy!');
(err as any).code = 500;
next(err);
} else
{
next(null);
}
};
}