#include "PipeliningTest.h" #include #include void PipeliningTest::asyncHandleHttpRequest( const HttpRequestPtr &req, std::function &&callback) { static std::atomic counter{0}; int c = counter.fetch_add(1); if (c % 3 == 1) { auto resp = HttpResponse::newHttpResponse(); auto str = utils::formattedString("

the %dth response

", c); resp->addHeader("counter", utils::formattedString("%d", c)); resp->setBody(std::move(str)); callback(resp); return; } double delay = ((double)(10 - (c % 10))) / 10.0; if (c % 3 == 2) { // call the callback in another thread. drogon::app().getLoop()->runAfter(delay, [c, callback]() { auto resp = HttpResponse::newHttpResponse(); auto str = utils::formattedString("

the %dth response

", c); resp->addHeader("counter", utils::formattedString("%d", c)); resp->setBody(std::move(str)); callback(resp); }); return; } trantor::EventLoop::getEventLoopOfCurrentThread()->runAfter( delay, [c, callback]() { auto resp = HttpResponse::newHttpResponse(); auto str = utils::formattedString("

the %dth response

", c); resp->addHeader("counter", utils::formattedString("%d", c)); resp->setBody(std::move(str)); callback(resp); }); }