Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions src/proxy/cache/asio.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include <concepts>
#include <entrypoint/http/body.h>
#include <entrypoint/http/stream.h>
#include <proxy/cache/awaitable_operators.h>

using namespace boost::asio::experimental::awaitable_operators;

namespace uh::cluster::proxy::cache {

Expand Down Expand Up @@ -38,8 +41,9 @@ template <BodyType Body> typename Body::writer make_writer(Body& b) {
*
* size can be replaced with parser implementation
*/
template <typename T>
coro<void> async_read(ep::http::stream& s, T& t, std::size_t size) {
template <typename S, typename T>
requires std::is_base_of_v<ep::http::stream, S>
coro<void> async_read(S& s, T& t, std::size_t size) {
auto&& reader = [&]() -> auto&& {
if constexpr (BodyType<T>) {
return make_reader(t);
Expand All @@ -50,6 +54,7 @@ coro<void> async_read(ep::http::stream& s, T& t, std::size_t size) {
"T must satisfy BodyType or ReaderBodyType");
}
}();

while (size > 0) {
auto sv = co_await s.read(size);
if (sv.empty())
Expand Down Expand Up @@ -79,11 +84,18 @@ template <typename T> coro<void> async_write(ep::http::stream& s, T& t) {
}
}();

while (true) {
std::span<const char> data = co_await writer.get();
if (data.empty())
break;
co_await s.write(data);
if constexpr (T::support_double_buffer::value) {
for (auto data = co_await writer.get(); !data.empty();) {
auto [d, _] = co_await (writer.get() && s.write(data));
data = d;
}
} else {
while (true) {
auto data = co_await writer.get();
if (data.empty())
break;
co_await s.write(data);
}
}
}

Expand Down
Loading
Loading