Skip to content
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

Improve coro server #423

Merged
merged 2 commits into from
Oct 16, 2023
Merged
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
2 changes: 1 addition & 1 deletion cmake/build.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ endif()
if (MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++latest")
else ()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -std=c++20")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -pthread -std=c++20")
endif ()

# --------------------- Gcc
Expand Down
2 changes: 0 additions & 2 deletions include/cinatra/coro_http_response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,6 @@ class coro_http_response {
void clear() {
head_.clear();
content_.clear();
head_.shrink_to_fit();
content_.shrink_to_fit();

resp_headers_.clear();
resp_headers_sv_.clear();
Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if(ENABLE_FILE_IO_URING)
endif()

if (UNIX)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -pthread")
endif()

## manual import
Expand Down
21 changes: 11 additions & 10 deletions tests/test_coro_http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,31 @@ using namespace std::chrono_literals;

TEST_CASE("coro_server example, will block") {
return; // remove this line when you run the coro server.
cinatra::coro_http_server server(1, 9001);
cinatra::coro_http_server server(std::thread::hardware_concurrency(), 9001);
server.set_http_handler<cinatra::GET, cinatra::POST>(
"/", [](coro_http_request &req, coro_http_response &resp) {
// response in io thread.
std::cout << std::this_thread::get_id() << "\n";
resp.set_keepalive(true);
std::this_thread::sleep_for(10ms);
resp.set_status_and_content(cinatra::status_type::ok, "hello world");
});

server.set_http_handler<cinatra::GET>(
"/coro",
[](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
std::cout << std::this_thread::get_id() << "\n";

co_await coro_io::post([&] {
co_await coro_io::post([&]() {
// coroutine in other thread.
std::cout << std::this_thread::get_id() << "\n";
resp.set_status(cinatra::status_type::ok);
resp.set_content("hello world in coro");
std::this_thread::sleep_for(10ms);
resp.set_status_and_content(cinatra::status_type::ok, "hello world");
});
std::cout << std::this_thread::get_id() << "\n";
co_return;
});

server.set_http_handler<cinatra::GET, cinatra::POST>(
"/echo", [](coro_http_request &req, coro_http_response &resp) {
// response in io thread.
resp.set_status_and_content(cinatra::status_type::ok, "hello world");
});
server.sync_start();
CHECK(server.port() > 0);
}
Expand Down