Use DelayQueueExecutor from `rx-queue` to manage tasks of calling the Tencent API
Created by: huan
Today, we start using DelayExecutor to manage the async tasks in Wechaty.
That's because of we will always do a map
/reduce
-like job internally from the Puppet, which will make huge numbers of API call to the server.
For example, we might want to do a Room.findAll()
with a Wechat account who has 500 rooms with 500 members in each room, which will cause Wechaty need Puppet to call 500 times for each room for the room information, with another 500 times to get the room members information.
Even worse, we might want to a Contact.findAll()
with a Wechaty account which has 5000 accounts, then we have to do 5000 API calls to the server, to get the contact information.
Indeed we had cache internally too; however, there are many edge cases will cause lots of the API call, for example, the cache had been deleted, or we start from a fresh memory.
If those calls are not well managed, it's very easy to reach the server threshold and will be blocked temporarily after a while.
so, we will add the API call task to the DelayedQueueExecutor:
delayQueueExecutor = new DelayQueueExector(1000 * 3)
delayQueueExecutor.execute(() => makeApiCall())
delayQueueExecutor.execute(() => makeApiCall())
delayQueueExecutor.execute(() => makeApiCall())
// All the above makeApiCall() will be called one by one, every 3 seconds.