每日一题:如何处理Promise并发限流

提问

大家应该都经历过这么一个场景,当我们使用微博或者写文章的时候,网站允许我们一次性选择多个图片进行上传,或者当我们写爬虫的时候都需要用并发来提高效率,那么问题来了,因为并发数过大会影响服务器的性能,因此需要限流。那我们如何实现并发的同时又限制个数呢?请实现一个Promise.map方法

先思考一下下……..

回答

设计成 Bluebird(npm包) 的 API,是比较模块化,也是易于使用的。代码的关键在于维护一个队列,当超过限定数量的 Promise 时,则交与队列维护。代码如下

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

class Limit {
constructor (n) {
this.limit = n
this.count = 0
this.queue = []
}

enqueue (fn) {
// 关键代码: fn, resolve, reject 统一管理
return new Promise((resolve, reject) => {
this.queue.push({ fn, resolve, reject })
})
}

dequeue () {
if (this.count < this.limit && this.queue.length) {
// 等到 Promise 计数器小于阈值时,则出队执行
const { fn, resolve, reject } = this.queue.shift()
this.run(fn).then(resolve).catch(reject)
}
}

// async/await 简化错误处理
async run (fn) {
this.count++
// 维护一个计数器
const value = await fn()
this.count--
// 执行完,看看队列有东西没
this.dequeue()
return value
}

build (fn) {
if (this.count < this.limit) {
// 如果没有到达阈值,直接执行
return this.run(fn)
} else {
// 如果超出阈值,则先扔到队列中,等待有空闲时执行
return this.enqueue(fn)
}
}
}
// concurrency 并发数目
Promise.map = function (list, fn, { concurrency }) {
const limit = new Limit(concurrency)
return Promise.all(list.map((...args) => {
return limit.build(() => fn(...args))
}))
}

课外题:自己实现一个Promise.all函数