-
Notifications
You must be signed in to change notification settings - Fork 393
Expand file tree
/
Copy pathtoobusy.ts
More file actions
48 lines (43 loc) · 1.09 KB
/
toobusy.ts
File metadata and controls
48 lines (43 loc) · 1.09 KB
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
/**
* Filter for rpc log.
* Reject rpc request when toobusy
*/
import { getLogger } from 'pinus-logger';
import {IRpcFilter} from 'pinus-rpc';
let rpcLogger = getLogger('rpc-log', __filename);
let toobusy : any = null;
let DEFAULT_MAXLAG = 70;
export class RpcToobusyFilter implements IRpcFilter
{
constructor(maxLag = DEFAULT_MAXLAG)
{
try
{
toobusy = require('toobusy');
} catch (e)
{
}
if (!!toobusy)
{
toobusy.maxLag(maxLag);
}
};
name = 'toobusy';
/**
* Before filter for rpc
*/
before(serverId: string, msg: any, opts: any, next: (target?: Error | string, message?: any, options?: any) => void)
{
opts = opts || {};
if (!!toobusy && toobusy())
{
rpcLogger.warn('Server too busy for rpc request, serverId:' + serverId + ' msg: ' + msg);
let err = new Error('Backend server ' + serverId + ' is too busy now!');
(err as any).code = 500;
next(err);
} else
{
next();
}
};
}