Mastering Node.js(Second Edition)
上QQ阅读APP看书,第一时间看更新

Collaboration

What would be preferable to the blocking system described previously is a collaborative work environment, where workers are regularly assigned new tasks to do, instead of idling. In order to achieve such a goal, what we need is a virtual switchboard, where requests for services are dispatched to available workers, and where workers notify the switchboard of their availability.

One way to achieve this goal is to have a pool of available labors, improving efficiency by delegating tasks to different workers as the tasks come in:

One drawback to this method is the amount of scheduling and worker surveillance that needs to be done. The dispatcher must field a steady stream of requests, while managing messages coming from workers about their availability, neatly breaking up requests into manageable tasks and efficiently sorting them, so that the fewest number of workers are idling. 

Perhaps most importantly, what happens when all workers are fully booked? Does the dispatcher begin to drop requests from clients? Dispatching is resource-intensive as well, and there are limits even to the dispatcher's resources. If requests continue to arrive and no worker is available to service them, what does the dispatcher do? Manage a queue? We now have a situation where the dispatcher is no longer doing the right job (dispatching), and has become responsible for bookkeeping and keeping lists, further extending the time each task takes to complete. Each task takes some amount of time, and must be processed in arrival order. This task execution model stacks fixed time intervals — ticks of time. This is synchronous execution.