feat: Integrate new webserver (#1722)

For #919.
The new web server is not using dosguard yet. It will be fixed by a
separate PR.
This commit is contained in:
Sergey Kuznetsov
2024-11-21 14:48:32 +00:00
committed by GitHub
parent fc3ba07f2e
commit c77154a5e6
90 changed files with 4029 additions and 683 deletions

View File

@@ -31,7 +31,7 @@
namespace util {
CoroutineGroup::CoroutineGroup(boost::asio::yield_context yield, std::optional<int> maxChildren)
CoroutineGroup::CoroutineGroup(boost::asio::yield_context yield, std::optional<size_t> maxChildren)
: timer_{yield.get_executor(), boost::asio::steady_timer::duration::max()}, maxChildren_{maxChildren}
{
}
@@ -41,28 +41,30 @@ CoroutineGroup::~CoroutineGroup()
ASSERT(childrenCounter_ == 0, "CoroutineGroup is destroyed without waiting for child coroutines to finish");
}
bool
CoroutineGroup::canSpawn() const
{
return not maxChildren_.has_value() or childrenCounter_ < *maxChildren_;
}
bool
CoroutineGroup::spawn(boost::asio::yield_context yield, std::function<void(boost::asio::yield_context)> fn)
{
if (not canSpawn())
if (isFull())
return false;
++childrenCounter_;
boost::asio::spawn(yield, [this, fn = std::move(fn)](boost::asio::yield_context yield) {
fn(yield);
--childrenCounter_;
if (childrenCounter_ == 0)
timer_.cancel();
onCoroutineCompleted();
});
return true;
}
std::optional<std::function<void()>>
CoroutineGroup::registerForeign()
{
if (isFull())
return std::nullopt;
++childrenCounter_;
return [this]() { onCoroutineCompleted(); };
}
void
CoroutineGroup::asyncWait(boost::asio::yield_context yield)
{
@@ -79,4 +81,20 @@ CoroutineGroup::size() const
return childrenCounter_;
}
bool
CoroutineGroup::isFull() const
{
return maxChildren_.has_value() && childrenCounter_ >= *maxChildren_;
}
void
CoroutineGroup::onCoroutineCompleted()
{
ASSERT(childrenCounter_ != 0, "onCoroutineCompleted() called more times than the number of child coroutines");
--childrenCounter_;
if (childrenCounter_ == 0)
timer_.cancel();
}
} // namespace util