This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The old implementation was using 2 thread pools: the main one, used to schedule and run tasks in secondary threads; and a helper one, used to schedule a health check routine. This design wasn't good because EACH.SINGLE.CALL to WorkerPool::post() would imply TWO new tasks being scheduled: 1. The task itself 2. A timed task to measure execution time In case the execution time was longer than 3 seconds, the task was considered "locked", and somehow assumed that it would never recover, so a new thread would be spawned. Now picture this: the server is under heavy load and tasks start to get too much time to finish. The existing implementation would start to consider all tasks locked, and would spawn threads for all of them, which soon enough would again take too much time, thus causing an explosion of thread spawns. The fact that two tasks are scheduled per post() only exacerbated the issue. The new code is much simpler. We realize that, for now, in case of CPU exhaustion there is not much we can do: tasks will take longer, and growing the numbr of threads won't necessarily help. The code is based on the simple but effective ideas shown in several sources: * Asio C++ library recipes: A thread pool for executing arbitrary tasks - http://think-async.com/Asio/Recipes - https://stackoverflow.com/questions/19500404/how-to-create-a-thread- pool-using-boost-in-c/19500405#19500405 * Boost Asio timer tutorial - https://www.boost.org/doc/libs/1_72_0/doc/html/boost_asio/tutorial.html versions of Boost >= 1.66.0 have a 'thread_pool' class: https://www.boost.org/doc/libs/1_66_0/doc/html/boost_asio/reference/thread_pool.html which maybe would have been interesting for this, but right now we have to use Boost 1.58.0 (Ubuntu 16.04 Xenial) and Boost 1.65.1 (Ubuntu 18.04 Bionic)
- Loading branch information
Showing
7 changed files
with
158 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.