Repository navigation
Routes HTTP dynamiques et envoi en flux (#454) #488
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a805b7f
Add dynamic HTTP routes and stream replies in bounded chunks (#454)
ambroise-leclerc ba5223b
Keep files flowing while routes block, send no body on failed HEAD, k…
ambroise-leclerc 64ed533
Lighten the large-body streaming test for slow builds (#454)
ambroise-leclerc 96a02ba
Drain cancelled tasks before joining the workers and answer 500 for a…
ambroise-leclerc 9d26bfb
Normalize route paths, keep a literal plus, stream a handler's 405 an…
ambroise-leclerc e552f12
Remove docs/website from this change and harden the new tests (#454)
ambroise-leclerc 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,168 @@ | ||
| /// @brief Server-owned thread running blocking application work (route handlers, file reads) off the I/O context. | ||
| #pragma once | ||
| #include "../networking/Tasks.hpp" | ||
|
|
||
| #include <condition_variable> | ||
| #include <deque> | ||
| #include <exception> | ||
| #include <functional> | ||
| #include <mutex> | ||
| #include <thread> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| namespace webfront::http { | ||
|
|
||
| /// Runs the work senders it hands out on lazily started threads, up to a fixed number: a job waits for a free | ||
| /// thread, so slow work delays only the jobs queued behind as many slow ones as there are threads. | ||
| /// | ||
| /// A sender completes on that thread: the awaiting task continues on the I/O context with `continues_on`. | ||
| /// Work still queued when the worker stops completes as stopped, so no task outlives the shutdown; the work | ||
| /// already running is not interrupted. A stop request of the awaiting task skips work not yet started. | ||
| class BlockingWorker { | ||
| struct Job { | ||
| virtual void run() noexcept = 0; | ||
| virtual void cancel() noexcept = 0; | ||
|
|
||
| protected: | ||
| ~Job() = default; | ||
| }; | ||
|
|
||
| public: | ||
| explicit BlockingWorker(std::size_t maxThreads = 1) : limit(maxThreads == 0 ? 1 : maxThreads) {} | ||
| BlockingWorker(const BlockingWorker&) = delete; | ||
| BlockingWorker& operator=(const BlockingWorker&) = delete; | ||
| ~BlockingWorker() { | ||
| stop(); | ||
| join(); | ||
| } | ||
|
|
||
| /// Thread-safe. Refuses new work and lets the thread end once the queued work is cancelled. | ||
| void stop() noexcept { | ||
| { | ||
| std::lock_guard lock(mutex); | ||
| stopping = true; | ||
| } | ||
| changed.notify_all(); | ||
| } | ||
|
|
||
| /// Waits for the running work; call after `stop()` and outside the worker's own thread. | ||
| void join() noexcept { | ||
| std::vector<std::thread> ending; | ||
| { | ||
| std::lock_guard lock(mutex); | ||
| ending.swap(threads); | ||
| } | ||
| for (auto& thread : ending) | ||
| if (thread.joinable()) thread.join(); | ||
| } | ||
|
|
||
| struct Sender { | ||
| using sender_concept = networking::ex::sender_t; | ||
| using completion_signatures = networking::ex:: | ||
| completion_signatures<networking::ex::set_value_t(), networking::ex::set_error_t(std::exception_ptr), networking::ex::set_stopped_t()>; | ||
|
|
||
| BlockingWorker* worker; | ||
| std::function<void()> work; | ||
|
|
||
| template <typename Receiver> | ||
| struct Operation final : Job { | ||
| BlockingWorker* worker; | ||
| Receiver receiver; | ||
| std::function<void()> work; | ||
| Operation(BlockingWorker* owner, Receiver value, std::function<void()> function) | ||
| : worker(owner), receiver(std::move(value)), work(std::move(function)) {} | ||
| Operation(const Operation&) = delete; | ||
| Operation(Operation&&) = delete; | ||
|
|
||
| void start() noexcept { | ||
| try { | ||
| if (worker->enqueue(this)) return; | ||
| } catch (...) { | ||
| networking::ex::set_error(std::move(receiver), std::current_exception()); | ||
| return; | ||
| } | ||
| networking::ex::set_stopped(std::move(receiver)); | ||
| } | ||
| // Completing may destroy this operation: nothing touches a member afterwards. | ||
| void run() noexcept override { | ||
| if (networking::ex::get_stop_token(networking::ex::get_env(receiver)).stop_requested()) { | ||
| networking::ex::set_stopped(std::move(receiver)); | ||
| return; | ||
| } | ||
| try { | ||
| work(); | ||
| } catch (...) { | ||
| networking::ex::set_error(std::move(receiver), std::current_exception()); | ||
| return; | ||
| } | ||
| networking::ex::set_value(std::move(receiver)); | ||
| } | ||
| void cancel() noexcept override { networking::ex::set_stopped(std::move(receiver)); } | ||
| }; | ||
|
|
||
| template <typename Receiver> | ||
| auto connect(Receiver receiver) && { | ||
| return Operation<Receiver>(worker, std::move(receiver), std::move(work)); | ||
| } | ||
| }; | ||
|
|
||
| /// A sender running `work` on the worker thread. State it touches must outlive the await. | ||
| [[nodiscard]] Sender run(std::function<void()> work) { return {this, std::move(work)}; } | ||
|
|
||
| private: | ||
| std::mutex mutex; | ||
| std::condition_variable changed; | ||
| std::deque<Job*> queue; | ||
| bool stopping = false; | ||
| std::vector<std::thread> threads; | ||
| std::size_t limit; | ||
| std::size_t idle = 0; | ||
|
|
||
| bool enqueue(Job* job) { | ||
| { | ||
| std::lock_guard lock(mutex); | ||
| if (stopping) return false; | ||
| queue.push_back(job); | ||
| if (queue.size() > idle && threads.size() < limit) { | ||
| try { | ||
| threads.emplace_back([this] { loop(); }); | ||
| } catch (...) { | ||
| if (threads.empty()) { | ||
| queue.pop_back(); | ||
| throw; | ||
| } // else a running thread will take the job | ||
| } | ||
| } | ||
| } | ||
| changed.notify_one(); | ||
| return true; | ||
| } | ||
|
|
||
| void loop() noexcept { | ||
| for (;;) { | ||
| Job* job = nullptr; | ||
| std::deque<Job*> cancelled; | ||
| { | ||
| std::unique_lock lock(mutex); | ||
| ++idle; | ||
| changed.wait(lock, [this] { return stopping || !queue.empty(); }); | ||
| --idle; | ||
| if (stopping) { | ||
| cancelled.swap(queue); | ||
| } else { | ||
| job = queue.front(); | ||
| queue.pop_front(); | ||
| } | ||
| } | ||
| if (job) { | ||
| job->run(); | ||
| continue; | ||
| } | ||
| for (auto* pending : cancelled) pending->cancel(); | ||
| return; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| } // namespace webfront::http |
This file contains hidden or 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.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.