2019-04-07 04:15:02 +00:00
|
|
|
#include "PipeliningTest.h"
|
2019-03-12 10:21:35 +00:00
|
|
|
#include <trantor/net/EventLoop.h>
|
|
|
|
#include <atomic>
|
|
|
|
|
2019-05-09 03:25:25 +00:00
|
|
|
void PipeliningTest::asyncHandleHttpRequest(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
|
2019-03-12 10:21:35 +00:00
|
|
|
{
|
2019-03-20 03:46:27 +00:00
|
|
|
static std::atomic<int> counter{0};
|
2019-03-12 10:21:35 +00:00
|
|
|
int c = counter.fetch_add(1);
|
|
|
|
double delay = ((double)(10 - (c % 10))) / 10.0;
|
|
|
|
trantor::EventLoop::getEventLoopOfCurrentThread()->runAfter(delay, [c, callback]() {
|
|
|
|
auto resp = HttpResponse::newHttpResponse();
|
2019-03-21 09:47:45 +00:00
|
|
|
auto str = utils::formattedString("<P>the %dth response</P>", c);
|
2019-04-07 04:15:02 +00:00
|
|
|
resp->addHeader("counter", utils::formattedString("%d", c));
|
2019-03-12 10:21:35 +00:00
|
|
|
resp->setBody(std::move(str));
|
|
|
|
callback(resp);
|
|
|
|
});
|
2019-03-20 03:46:27 +00:00
|
|
|
}
|