forked from kevinaskin/apijson-node
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
48 lines (44 loc) · 1.3 KB
/
Copy pathindex.ts
File metadata and controls
48 lines (44 loc) · 1.3 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
// parser, handler here for json body transfer to ORM executor
import { RequestParser, JobInterface, StatusEnum } from './traverse'
interface jsonBodyParserReturn {
queue: JobInterface[],
errors: string[],
originalJson: any,
RequestParser: RequestParser
}
const POOL_SIZE: number = 5 // for current-limiting
const RequestParserPool: RequestParser[] = []
export function jsonBodyParser (queryBody: object): jsonBodyParserReturn {
if (RequestParserPool.length < POOL_SIZE) {
const RPInstance = new RequestParser(queryBody)
RequestParserPool.push(RPInstance)
return {
queue: RPInstance.queue.slice(),
errors: RPInstance.errors.slice(),
originalJson: queryBody,
RequestParser: RPInstance
}
} else {
const RPInstance = getFreeRequestParser(RequestParserPool)
if (RPInstance) {
RPInstance.startJob(queryBody)
return {
queue: RPInstance.queue.slice(),
errors: RPInstance.errors.slice(),
originalJson: queryBody,
RequestParser: RPInstance
}
} else {
return null
}
}
}
function getFreeRequestParser (pool: RequestParser[]): RequestParser | null {
for (let i = 0; i < pool.length; i++) {
if (pool[i].status === StatusEnum.Free) {
pool[i].flushJob()
return pool[i]
}
}
return null
}