File: PipeliningTest.cc

package info (click to toggle)
drogon 1.9.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 4,096 kB
  • sloc: cpp: 52,222; sh: 249; xml: 20; makefile: 11
file content (146 lines) | stat: -rw-r--r-- 4,456 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "PipeliningTest.h"
#include <trantor/net/EventLoop.h>
#include <atomic>
#include <mutex>

void PipeliningTest::normalPipe(
    const HttpRequestPtr &req,
    std::function<void(const HttpResponsePtr &)> &&callback) const
{
    static std::atomic<int> counter{0};
    int c = counter.fetch_add(1);
    if (c % 3 == 1)
    {
        auto resp = HttpResponse::newHttpResponse();
        auto str = utils::formattedString("<P>the %dth response</P>", 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("<P>the %dth response</P>", 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("<P>the %dth response</P>", c);
            resp->addHeader("counter", utils::formattedString("%d", c));
            resp->setBody(std::move(str));
            callback(resp);
        });
}

// Receive 1, cache 1
// Receive 2, send 1 send 2
void PipeliningTest::strangePipe1(
    const HttpRequestPtr &req,
    std::function<void(const HttpResponsePtr &)> &&callback)
{
    static std::mutex mtx;
    static std::vector<
        std::pair<std::function<void(const HttpResponsePtr &)>, std::string>>
        callbacks;

    LOG_INFO << "Receive request " << req->body();
    std::function<void(const HttpResponsePtr &)> cb1;
    std::string body1;
    std::function<void(const HttpResponsePtr &)> cb2;
    std::string body2;
    {
        std::lock_guard<std::mutex> lock(mtx);
        if (callbacks.empty())
        {
            callbacks.emplace_back(std::move(callback), req->getBody());
            return;
        }
        auto item = std::move(callbacks.back());
        callbacks.pop_back();
        cb1 = std::move(item.first);
        body1 = std::move(item.second);
        cb2 = std::move(callback);
        body2 = std::string{req->body()};
    }
    if (cb1)
    {
        auto resp = HttpResponse::newHttpResponse();
        resp->setBody(body1);
        cb1(resp);
    }
    if (cb2)
    {
        auto resp = HttpResponse::newHttpResponse();
        resp->setBody(body2);
        cb2(resp);
    }
}

// Receive 1, cache 1
// Receive 2, send 1 cache 2
// Receive 3, send 2 send 3
void PipeliningTest::strangePipe2(
    const HttpRequestPtr &req,
    std::function<void(const HttpResponsePtr &)> &&callback)
{
    static std::mutex mtx;
    static std::vector<
        std::pair<std::function<void(const HttpResponsePtr &)>, std::string>>
        callbacks;
    static uint64_t idx{0};

    LOG_INFO << "Receive request " << req->body();
    std::function<void(const HttpResponsePtr &)> cb1;
    std::string body1;
    std::function<void(const HttpResponsePtr &)> cb2;
    std::string body2;
    {
        std::lock_guard<std::mutex> lock(mtx);
        ++idx;
        if (idx % 3 == 1)
        {
            assert(callbacks.empty());
            callbacks.emplace_back(std::move(callback), req->getBody());
            return;
        }
        assert(callbacks.size() == 1);
        if (idx % 3 == 2)
        {
            auto item = std::move(callbacks.back());
            cb1 = std::move(item.first);
            body1 = std::move(item.second);
            callbacks.pop_back();
            callbacks.emplace_back(std::move(callback), req->getBody());
        }
        else
        {
            auto item = std::move(callbacks.back());
            cb1 = std::move(item.first);
            body1 = std::move(item.second);
            callbacks.pop_back();
            cb2 = std::move(callback);
            body2 = std::string{req->body()};
        }
    }
    if (cb1)
    {
        auto resp = HttpResponse::newHttpResponse();
        resp->setBody(body1);
        cb1(resp);
    }
    if (cb2)
    {
        auto resp = HttpResponse::newHttpResponse();
        resp->setBody(body2);
        cb2(resp);
    }
}