-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfair-task-pool.ts
More file actions
130 lines (110 loc) · 2.99 KB
/
Copy pathfair-task-pool.ts
File metadata and controls
130 lines (110 loc) · 2.99 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
type Key = string | number
/** @description can be used for per-user task queue */
export class FairTaskPool {
private queues = new Map<Key, TaskQueue>()
constructor(
public options: {
/**
* @description max number of pending tasks per-key
* @default unlimited if undefined
* */
capacity?: number
/** @default false */
flushQueueWhenEmpty?: boolean
} = {},
) {}
/**
* @description dispatch the task to corresponding TaskQueue partitioned by the `key`
* @throws TaskQueueFullError when exceed
* */
enqueue<T>(key: Key, task: Task<T>): Promise<T> {
let queue = this.getQueue(key)
return queue.enqueue(task)
}
getPendingTaskCount(key: Key): number {
let queue = this.queues.get(key)
return queue ? queue.pendingTaskCount : 0
}
getQueueSize() {
return this.queues.size
}
private getQueue(key: Key): TaskQueue {
let queue = this.queues.get(key)
if (!queue) {
let onEmpty = this.options.flushQueueWhenEmpty
? () => {
this.queues.delete(key)
}
: undefined
let capacity = this.options.capacity
queue = capacity
? new LimitedTaskQueue({ onEmpty, capacity })
: new UnlimitedTaskQueue({ onEmpty })
this.queues.set(key, queue)
}
return queue
}
}
/** @description the task is executed with try-catch. */
export type Task<T> = () => T | Promise<T>
export interface TaskQueue {
pendingTaskCount: number
onEmpty?: () => void
/** @throws TaskQueueFullError when exceed */
enqueue<T>(task: Task<T>): Promise<T>
}
export class UnlimitedTaskQueue implements TaskQueue {
queue: Task<void>[] = []
running = false
onEmpty?: () => void
constructor(options?: { onEmpty?: () => void }) {
this.onEmpty = options?.onEmpty
}
get pendingTaskCount(): number {
return this.queue.length
}
enqueue<T>(task: Task<T>): Promise<T> {
return new Promise<T>((resolve, reject) => {
this.queue.push(async () => {
try {
resolve(await task())
} catch (error) {
reject(error)
}
})
this.tick()
})
}
async tick() {
if (this.running) return
this.running = true
while (this.queue.length > 0) {
let task = this.queue.shift()!
await task()
}
this.running = false
if (this.onEmpty) {
this.onEmpty()
}
}
}
export class LimitedTaskQueue extends UnlimitedTaskQueue implements TaskQueue {
capacity: number
constructor(options: { onEmpty?: () => void; capacity: number }) {
super(options)
this.capacity = options.capacity
}
/** @throws TaskQueueFullError when exceed */
enqueue<T>(task: Task<T>): Promise<T> {
if (this.queue.length >= this.capacity) {
throw new TaskQueueFullError(this.capacity)
}
return super.enqueue(task)
}
}
export class TaskQueueFullError extends Error {
statusCode = 429 as const
constructor(public capacity: number) {
super('exceed task queue capacity of ' + capacity)
}
}