-
Notifications
You must be signed in to change notification settings - Fork 374
/
serial.ts
48 lines (44 loc) · 1.45 KB
/
serial.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
46
47
48
/**
* Filter to keep request sequence.
*/
import { getLogger } from 'pinus-logger'; let logger = getLogger('pinus', __filename);
import * as taskManager from '../../common/manager/taskManager';
import { RouteRecord } from '../../util/constants';
import { IHandlerFilter } from '../../interfaces/IHandlerFilter';
import { HandlerCallback } from '../../common/service/handlerService';
import { FrontendOrBackendSession } from '../../server/server';
export class SerialFilter implements IHandlerFilter
{
constructor(private timeout : number)
{
};
/**
* request serialization after filter
*/
before(routeRecord : RouteRecord , msg : any, session : FrontendOrBackendSession, next : HandlerCallback)
{
taskManager.addTask(session.id, function (task)
{
(session as any).__serialTask__ = task;
next(null);
}, function ()
{
logger.error('[serial filter] msg timeout, msg:' + JSON.stringify(msg));
}, this.timeout);
};
/**
* request serialization after filter
*/
after(err : Error, routeRecord : RouteRecord , msg : any, session : FrontendOrBackendSession, resp : any, next : HandlerCallback)
{
let task = (session as any).__serialTask__;
if (task)
{
if (!task.done() && !err)
{
err = new Error('task time out. msg:' + JSON.stringify(msg));
}
}
next(err);
};
}