-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
145 lines (128 loc) · 3.59 KB
/
test.html
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
140
141
142
143
144
145
<html>
<script>
// function Semaphore(max) {
// var counter = 0;
// var waiting = [];
// var take = function() {
// if (waiting.length > 0 && counter < max){
// counter++;
// let promise = waiting.shift();
// promise.resolve();
// }
// }
// this.acquire = function() {
// if(counter < max) {
// counter++
// return new Promise(resolve => {
// resolve();
// });
// } else {
// return new Promise((resolve, err) => {
// waiting.push({resolve: resolve, err: err});
// });
// }
// }
// this.release = function() {
// counter--;
// take();
// }
// this.purge = function() {
// let unresolved = waiting.length;
// for (let i = 0; i < unresolved; i++) {
// waiting[i].err('Task has been purged.');
// }
// counter = 0;
// waiting = [];
// return unresolved;
// }
// }
// // testing the semaphore
// let sema = new Semaphore(5);
// async function test(id) {
// console.log('queueing task', id);
// try {
// await sema.acquire();
// console.log('running task', id);
// setTimeout(() => {
// sema.release();
// }, 2000);
// } catch (e) {
// console.error(id, e);
// }
// }
// test(1);
// test(2);
// test(3);
// test(4);
// test(5);
// setTimeout(() => {
// test(10);
// test(11);
// test(12);
// }, 1500);
// setTimeout(() => {
// test(20);
// test(21);
// test(22);
// }, 2700);
class Semaphore {
/**
* Creates a semaphore that limits the number of concurrent Promises being handled
* @param {*} maxConcurrentRequests max number of concurrent promises being handled at any time
*/
constructor(maxConcurrentRequests = 1) {
this.currentRequests = [];
this.runningRequests = 0;
this.maxConcurrentRequests = maxConcurrentRequests;
}
/**
* Returns a Promise that will eventually return the result of the function passed in
* Use this to limit the number of concurrent function executions
* @param {*} fnToCall function that has a cap on the number of concurrent executions
* @param {...any} args any arguments to be passed to fnToCall
* @returns Promise that will resolve with the resolved value as if the function passed in was directly called
*/
callFunction(fnToCall, ...args) {
return new Promise((resolve, reject) => {
this.currentRequests.push({
resolve,
reject,
fnToCall,
args,
});
this.tryNext();
});
}
tryNext() {
if (!this.currentRequests.length) {
return;
} else if (this.runningRequests < this.maxConcurrentRequests) {
let { resolve, reject, fnToCall, args } = this.currentRequests.shift();
this.runningRequests++;
let req = fnToCall(...args);
req.then((res) => resolve(res))
.catch((err) => reject(err))
.finally(() => {
this.runningRequests--;
this.tryNext();
});
}
}
}
/* HOW TO USE */
const throttler = new Semaphore(2);
const fu = (url) => {
console.log('fu')
return new Promise((resolve, reject) => {
setTimeout(() => {
// console.log(url)
resolve(url)
}, 1500)
})
}
throttler.callFunction(fu, 'www.facebook.com').then(console.log);
throttler.callFunction(fu, 'www.amazon.com').then(console.log);
throttler.callFunction(fu, 'www.netflix.com').then(console.log);
throttler.callFunction(fu, 'www.google.com').then(console.log);
</script>
</html>