-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
limit offchain threads #1630
Merged
Merged
limit offchain threads #1630
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef KAGOME_OFFCHAIN_IMPL_RUNNER_HPP | ||
#define KAGOME_OFFCHAIN_IMPL_RUNNER_HPP | ||
|
||
#include <deque> | ||
#include <gsl/gsl_util> | ||
#include <mutex> | ||
|
||
#include "utils/thread_pool.hpp" | ||
|
||
namespace kagome::offchain { | ||
/** | ||
* Enqueue at most `max_tasks_` to run on number of `threads_`. | ||
* Old tasks do not run and are removed when queue is full. | ||
*/ | ||
class Runner : public std::enable_shared_from_this<Runner> { | ||
public: | ||
using Task = std::function<void()>; | ||
|
||
Runner(size_t threads, size_t max_tasks) | ||
: threads_{threads}, | ||
free_threads_{threads}, | ||
max_tasks_{max_tasks}, | ||
thread_pool_{threads_} {} | ||
|
||
void run(Task &&task) { | ||
std::unique_lock lock{mutex_}; | ||
if (tasks_.size() >= max_tasks_) { | ||
tasks_.pop_front(); | ||
} | ||
if (free_threads_ <= 0) { | ||
turuslan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tasks_.emplace_back(std::move(task)); | ||
return; | ||
} | ||
--free_threads_; | ||
lock.unlock(); | ||
thread_pool_.io_context()->post( | ||
[weak{weak_from_this()}, task{std::move(task)}] { | ||
if (auto self = weak.lock()) { | ||
auto release = gsl::finally([&] { | ||
std::unique_lock lock{self->mutex_}; | ||
++self->free_threads_; | ||
}); | ||
task(); | ||
self->drain(); | ||
} | ||
}); | ||
} | ||
|
||
private: | ||
void drain() { | ||
while (true) { | ||
std::unique_lock lock{mutex_}; | ||
if (tasks_.empty()) { | ||
break; | ||
} | ||
auto task{std::move(tasks_.front())}; | ||
tasks_.pop_front(); | ||
lock.unlock(); | ||
task(); | ||
} | ||
} | ||
|
||
std::mutex mutex_; | ||
const size_t threads_; | ||
size_t free_threads_; | ||
const size_t max_tasks_; | ||
std::deque<Task> tasks_; | ||
ThreadPool thread_pool_; | ||
}; | ||
} // namespace kagome::offchain | ||
|
||
#endif // KAGOME_OFFCHAIN_IMPL_RUNNER_HPP |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
мне кажется не стоит тут порождать потоки, у нас уже создается пулл для фоновых задач и можно выполнять в нем https://imgur.com/7zEUjSI.png
А в пулле нам по сути без разницы на какую работу тратится время пулла, от этого ресурсов больше не станет, так что счетчик max_tasks будет нормально работать.
Зато в дальнейшем это даст например возможность ограничить этим же кодом некоторые задачи в парачейнах.