smart-pool 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +9 -0
- package/package.json +46 -0
- package/readme.md +1007 -0
- package/src/heap.js +130 -0
- package/src/index.d.ts +118 -0
- package/src/index.js +996 -0
package/src/heap.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A Binary Max-Heap
|
|
3
|
+
*/
|
|
4
|
+
export class PriorityHeap {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.heap = [];
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
_compare(a, b) {
|
|
10
|
+
if (a.priority !== b.priority) {
|
|
11
|
+
return a.priority - b.priority;
|
|
12
|
+
}
|
|
13
|
+
return b.seq - a.seq;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
push(item) {
|
|
17
|
+
this.heap.push(item);
|
|
18
|
+
this._bubbleUp();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
pop() {
|
|
22
|
+
const size = this.heap.length;
|
|
23
|
+
if (size === 0) return null;
|
|
24
|
+
if (size === 1) return this.heap.pop();
|
|
25
|
+
const top = this.heap[0];
|
|
26
|
+
this.heap[0] = this.heap.pop();
|
|
27
|
+
this._bubbleDown(0);
|
|
28
|
+
return top;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
peek() {
|
|
32
|
+
return this.heap[0] || null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
clear() {
|
|
36
|
+
this.heap = [];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
remove(predicate) {
|
|
40
|
+
const initialSize = this.heap.length;
|
|
41
|
+
this.heap = this.heap.filter((item) => !predicate(item));
|
|
42
|
+
if (this.heap.length !== initialSize) {
|
|
43
|
+
this._rebuild();
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
adjustPriorities(threshold, amount, isDecay = false) {
|
|
50
|
+
const size = this.heap.length;
|
|
51
|
+
if (size <= 1) return;
|
|
52
|
+
|
|
53
|
+
let changed = false;
|
|
54
|
+
const targetPriority = isDecay
|
|
55
|
+
? this.heap.reduce((min, item) => Math.min(min, item.priority), Infinity)
|
|
56
|
+
: this.heap.reduce(
|
|
57
|
+
(max, item) => Math.max(max, item.priority),
|
|
58
|
+
-Infinity
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
for (let i = 0; i < size; i++) {
|
|
62
|
+
const item = this.heap[i];
|
|
63
|
+
const isEligible = isDecay
|
|
64
|
+
? item.priority > targetPriority
|
|
65
|
+
: item.priority < targetPriority;
|
|
66
|
+
|
|
67
|
+
if (isEligible) {
|
|
68
|
+
item.cycles = (item.cycles || 0) + 1;
|
|
69
|
+
if (item.cycles >= threshold) {
|
|
70
|
+
item.priority = targetPriority;
|
|
71
|
+
item.cycles = 0;
|
|
72
|
+
changed = true;
|
|
73
|
+
}
|
|
74
|
+
} else {
|
|
75
|
+
item.cycles = 0;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (changed) this._rebuild();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
size() {
|
|
83
|
+
return this.heap.length;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
_rebuild() {
|
|
87
|
+
const length = this.heap.length;
|
|
88
|
+
for (let i = (length >> 1) - 1; i >= 0; i--) {
|
|
89
|
+
this._bubbleDown(i);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
_bubbleUp() {
|
|
94
|
+
let index = this.heap.length - 1;
|
|
95
|
+
const element = this.heap[index];
|
|
96
|
+
while (index > 0) {
|
|
97
|
+
let parentIdx = (index - 1) >> 1;
|
|
98
|
+
if (this._compare(element, this.heap[parentIdx]) <= 0) break;
|
|
99
|
+
this.heap[index] = this.heap[parentIdx];
|
|
100
|
+
index = parentIdx;
|
|
101
|
+
}
|
|
102
|
+
this.heap[index] = element;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
_bubbleDown(index = 0) {
|
|
106
|
+
const length = this.heap.length;
|
|
107
|
+
const element = this.heap[index];
|
|
108
|
+
while (true) {
|
|
109
|
+
let leftChildIdx = (index << 1) + 1;
|
|
110
|
+
let rightChildIdx = (index << 1) + 2;
|
|
111
|
+
let swap = null;
|
|
112
|
+
|
|
113
|
+
if (leftChildIdx < length) {
|
|
114
|
+
if (this._compare(this.heap[leftChildIdx], element) > 0)
|
|
115
|
+
swap = leftChildIdx;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (rightChildIdx < length) {
|
|
119
|
+
const compareTo = swap === null ? element : this.heap[leftChildIdx];
|
|
120
|
+
if (this._compare(this.heap[rightChildIdx], compareTo) > 0)
|
|
121
|
+
swap = rightChildIdx;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (swap === null) break;
|
|
125
|
+
this.heap[index] = this.heap[swap];
|
|
126
|
+
index = swap;
|
|
127
|
+
}
|
|
128
|
+
this.heap[index] = element;
|
|
129
|
+
}
|
|
130
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
export interface RateLimit {
|
|
2
|
+
interval: number;
|
|
3
|
+
tasksPerInterval: number;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface TaskOptions {
|
|
7
|
+
priority?: number;
|
|
8
|
+
weight?: number;
|
|
9
|
+
type?: string;
|
|
10
|
+
cacheKey?: string;
|
|
11
|
+
batchKey?: string;
|
|
12
|
+
id?: string | number;
|
|
13
|
+
tags?: string[];
|
|
14
|
+
metadata?: Record<string, any>;
|
|
15
|
+
dependsOn?: (string | number)[];
|
|
16
|
+
deadline?: number;
|
|
17
|
+
signal?: AbortSignal;
|
|
18
|
+
timeout?: number;
|
|
19
|
+
retryCount?: number;
|
|
20
|
+
retryDelay?: number;
|
|
21
|
+
worker?: {
|
|
22
|
+
path: string;
|
|
23
|
+
data?: any;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface Metrics {
|
|
28
|
+
totalTasks: number;
|
|
29
|
+
successfulTasks: number;
|
|
30
|
+
failedTasks: number;
|
|
31
|
+
throughput: string;
|
|
32
|
+
errorRate: string;
|
|
33
|
+
percentiles: {
|
|
34
|
+
p50: string;
|
|
35
|
+
p90: string;
|
|
36
|
+
p99: string;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface WorkerHealth {
|
|
41
|
+
path: string;
|
|
42
|
+
busy: boolean;
|
|
43
|
+
active: boolean;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface GlobalOptions {
|
|
47
|
+
maxQueueSize?: number;
|
|
48
|
+
workerPoolSize?: number;
|
|
49
|
+
workerPathWhitelist?: string[];
|
|
50
|
+
batchSize?: number;
|
|
51
|
+
batchTimeout?: number;
|
|
52
|
+
rateLimits?: Record<string, RateLimit>;
|
|
53
|
+
circuitThreshold?: number;
|
|
54
|
+
circuitResetTimeout?: number;
|
|
55
|
+
retryCount?: number;
|
|
56
|
+
initialRetryDelay?: number;
|
|
57
|
+
retryFactor?: number;
|
|
58
|
+
maxRetryDelay?: number;
|
|
59
|
+
adaptive?: boolean;
|
|
60
|
+
minConcurrency?: number;
|
|
61
|
+
maxConcurrency?: number;
|
|
62
|
+
interval?: number;
|
|
63
|
+
tasksPerInterval?: number;
|
|
64
|
+
agingThreshold?: number;
|
|
65
|
+
agingBoost?: number;
|
|
66
|
+
decayThreshold?: number;
|
|
67
|
+
decayAmount?: number;
|
|
68
|
+
maxLatencyHistory?: number;
|
|
69
|
+
maxErrorHistory?: number;
|
|
70
|
+
completedTaskCleanupMs?: number;
|
|
71
|
+
emitter?: any;
|
|
72
|
+
onEnqueue?: (task: any) => void;
|
|
73
|
+
onDequeue?: (task: any) => void;
|
|
74
|
+
beforeExecute?: (task: any) => void;
|
|
75
|
+
afterExecute?: (
|
|
76
|
+
task: any,
|
|
77
|
+
profile: {
|
|
78
|
+
duration: number;
|
|
79
|
+
memoryDelta: number;
|
|
80
|
+
status: string;
|
|
81
|
+
error?: string;
|
|
82
|
+
}
|
|
83
|
+
) => void;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface PoolInstance {
|
|
87
|
+
<T>(task: () => Promise<T>, options?: number | TaskOptions): Promise<T>;
|
|
88
|
+
activeCount: number;
|
|
89
|
+
pendingCount: number;
|
|
90
|
+
currentLoad: number;
|
|
91
|
+
concurrency: number;
|
|
92
|
+
isDraining: boolean;
|
|
93
|
+
isPaused: boolean;
|
|
94
|
+
metrics: Metrics;
|
|
95
|
+
pause(): void;
|
|
96
|
+
resume(): void;
|
|
97
|
+
cancel(
|
|
98
|
+
query: { id?: string | number; tag?: string } | ((task: any) => boolean)
|
|
99
|
+
): number;
|
|
100
|
+
onIdle(): Promise<{ errors: Error[]; failed: boolean; metrics: Metrics }>;
|
|
101
|
+
drain(): Promise<{ errors: Error[]; failed: boolean; metrics: Metrics }>;
|
|
102
|
+
setConcurrency(limit: number): void;
|
|
103
|
+
peek(): any;
|
|
104
|
+
clear(): Promise<void>;
|
|
105
|
+
remove(predicate: (item: any) => boolean): boolean;
|
|
106
|
+
map<T, R>(
|
|
107
|
+
items: T[],
|
|
108
|
+
fn: (item: T) => Promise<R>,
|
|
109
|
+
opts?: number | TaskOptions
|
|
110
|
+
): Promise<R[]>;
|
|
111
|
+
getWorkerHealth(): WorkerHealth[];
|
|
112
|
+
useQueue(name: string, concurrency?: number): PoolInstance;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export default function leap(
|
|
116
|
+
initialConcurrency: number,
|
|
117
|
+
globalOptions?: GlobalOptions
|
|
118
|
+
): PoolInstance;
|