-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
blocking-queue.js
139 lines (115 loc) · 2.94 KB
/
blocking-queue.js
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
131
132
133
134
135
136
137
138
139
/* @flow */
import map from './map.js';
const debug = require('debug')('yarn');
export default class BlockingQueue {
constructor(alias: string, maxConcurrency?: number = Infinity) {
this.concurrencyQueue = [];
this.maxConcurrency = maxConcurrency;
this.runningCount = 0;
this.warnedStuck = false;
this.alias = alias;
this.first = true;
this.running = map();
this.queue = map();
(this: any).stuckTick = this.stuckTick.bind(this);
}
concurrencyQueue: Array<() => void>;
warnedStuck: boolean;
maxConcurrency: number;
runningCount: number;
stuckTimer: ?number;
alias: string;
first: boolean;
queue: {
[key: string]: Array<{
factory: () => Promise<any>,
resolve: (val: any) => void,
reject: Function,
}>,
};
running: {
[key: string]: boolean,
};
stillActive() {
if (this.stuckTimer) {
clearTimeout(this.stuckTimer);
}
this.stuckTimer = setTimeout(this.stuckTick, 5000);
}
stuckTick() {
if (this.runningCount === 1) {
this.warnedStuck = true;
debug(
`The ${JSON.stringify(this.alias)} blocking queue may be stuck. 5 seconds ` +
`without any activity with 1 worker: ${Object.keys(this.running)[0]}`,
);
}
}
push<T>(key: string, factory: () => Promise<T>): Promise<T> {
if (this.first) {
this.first = false;
} else {
this.stillActive();
}
return new Promise((resolve, reject) => {
// we're already running so push ourselves to the queue
const queue = (this.queue[key] = this.queue[key] || []);
queue.push({factory, resolve, reject});
if (!this.running[key]) {
this.shift(key);
}
});
}
shift(key: string) {
if (this.running[key]) {
delete this.running[key];
this.runningCount--;
if (this.warnedStuck) {
this.warnedStuck = false;
debug(`${JSON.stringify(this.alias)} blocking queue finally resolved. Nothing to worry about.`);
}
}
const queue = this.queue[key];
if (!queue) {
return;
}
const {resolve, reject, factory} = queue.shift();
if (!queue.length) {
delete this.queue[key];
}
const next = () => {
this.shift(key);
this.shiftConcurrencyQueue();
};
const run = () => {
this.running[key] = true;
this.runningCount++;
factory()
.then(function(val): null {
resolve(val);
next();
return null;
})
.catch(function(err) {
reject(err);
next();
});
};
this.maybePushConcurrencyQueue(run);
}
maybePushConcurrencyQueue(run: () => void) {
if (this.runningCount < this.maxConcurrency) {
run();
} else {
this.concurrencyQueue.push(run);
}
}
shiftConcurrencyQueue() {
if (this.runningCount < this.maxConcurrency) {
const fn = this.concurrencyQueue.shift();
if (fn) {
fn();
}
}
}
}