Browse Source

Update drogon to 1.0b11 (#5221)

An Tao 5 years ago
parent
commit
9e9593b3b2

+ 1 - 1
frameworks/C++/drogon/drogon-core.dockerfile

@@ -44,7 +44,7 @@ RUN git clone https://github.com/an-tao/drogon
 
 WORKDIR $DROGON_ROOT
 
-RUN git checkout ac377bd650e2fd80f3d5db522c876332edb65dc1
+RUN git checkout 139d2db02b324be02430a18dfe93fe822b141b4d
 RUN git submodule update --init
 RUN mkdir build
 

+ 1 - 1
frameworks/C++/drogon/drogon.dockerfile

@@ -44,7 +44,7 @@ RUN git clone https://github.com/an-tao/drogon
 
 WORKDIR $DROGON_ROOT
 
-RUN git checkout ac377bd650e2fd80f3d5db522c876332edb65dc1
+RUN git checkout 139d2db02b324be02430a18dfe93fe822b141b4d
 RUN git submodule update --init
 RUN mkdir build
 

+ 5 - 2
frameworks/C++/drogon/drogon_benchmark/controllers/DbCtrl.cc

@@ -13,12 +13,15 @@ void DbCtrl::asyncHandleHttpRequest(
     // write your application logic here
     static std::once_flag once;
     std::call_once(once, []() { srand(time(NULL)); });
-    auto client = drogon::app().getFastDbClient();
+    if (!*_dbClient)
+    {
+        *_dbClient = drogon::app().getFastDbClient();
+    }
 
     auto callbackPtr =
         std::make_shared<std::function<void(const HttpResponsePtr &)>>(
             std::move(callback));
-    drogon::orm::Mapper<World> mapper(client);
+    drogon::orm::Mapper<World> mapper(*_dbClient);
     World::PrimaryKeyType id = rand() % 10000 + 1;
     mapper.findByPrimaryKey(
         id,

+ 4 - 0
frameworks/C++/drogon/drogon_benchmark/controllers/DbCtrl.h

@@ -1,5 +1,7 @@
 #pragma once
 #include <drogon/HttpSimpleController.h>
+#include <drogon/IOThreadStorage.h>
+
 using namespace drogon;
 class DbCtrl : public drogon::HttpSimpleController<DbCtrl>
 {
@@ -11,4 +13,6 @@ class DbCtrl : public drogon::HttpSimpleController<DbCtrl>
     // list path definitions here;
     // PATH_ADD("/db", Get);
     PATH_LIST_END
+  private:
+    IOThreadStorage<orm::DbClientPtr> _dbClient;
 };

+ 5 - 2
frameworks/C++/drogon/drogon_benchmark/controllers/DbCtrlRaw.cc

@@ -13,13 +13,16 @@ void DbCtrlRaw::asyncHandleHttpRequest(
     // write your application logic here
     static std::once_flag once;
     std::call_once(once, []() { srand(time(NULL)); });
-    auto client = drogon::app().getFastDbClient();
+    if (!*_dbClient)
+    {
+        *_dbClient = drogon::app().getFastDbClient();
+    }
     int id = rand() % 10000 + 1;
     auto callbackPtr =
         std::make_shared<std::function<void(const HttpResponsePtr &)>>(
             std::move(callback));
 
-    *client << "select * from world where id=$1" << id >>
+    **_dbClient << "select * from world where id=$1" << id >>
         [callbackPtr, id](const Result &rows) {
             auto resp = HttpResponse::newHttpResponse();
             char json[64];

+ 4 - 0
frameworks/C++/drogon/drogon_benchmark/controllers/DbCtrlRaw.h

@@ -1,5 +1,7 @@
 #pragma once
 #include <drogon/HttpSimpleController.h>
+#include <drogon/IOThreadStorage.h>
+
 using namespace drogon;
 class DbCtrlRaw : public drogon::HttpSimpleController<DbCtrlRaw>
 {
@@ -11,4 +13,6 @@ class DbCtrlRaw : public drogon::HttpSimpleController<DbCtrlRaw>
     // list path definitions here;
     // PATH_ADD("/db", Get);
     PATH_LIST_END
+  private:
+    IOThreadStorage<orm::DbClientPtr> _dbClient;
 };

+ 5 - 2
frameworks/C++/drogon/drogon_benchmark/controllers/FortuneCtrl.cc

@@ -7,8 +7,11 @@ void FortuneCtrl::asyncHandleHttpRequest(
     const HttpRequestPtr &req,
     std::function<void(const HttpResponsePtr &)> &&callback)
 {
-    auto client = drogon::app().getFastDbClient();
-    drogon::orm::Mapper<Fortune> mapper(client);
+    if (!*_dbClient)
+    {
+        *_dbClient = drogon::app().getFastDbClient();
+    }
+    drogon::orm::Mapper<Fortune> mapper(*_dbClient);
     auto callbackPtr =
         std::make_shared<std::function<void(const HttpResponsePtr &)>>(
             std::move(callback));

+ 4 - 0
frameworks/C++/drogon/drogon_benchmark/controllers/FortuneCtrl.h

@@ -1,5 +1,7 @@
 #pragma once
 #include <drogon/HttpSimpleController.h>
+#include <drogon/IOThreadStorage.h>
+
 using namespace drogon;
 class FortuneCtrl : public drogon::HttpSimpleController<FortuneCtrl>
 {
@@ -10,4 +12,6 @@ class FortuneCtrl : public drogon::HttpSimpleController<FortuneCtrl>
     PATH_LIST_BEGIN
     // list path definitions here;
     PATH_LIST_END
+  private:
+    IOThreadStorage<orm::DbClientPtr> _dbClient;
 };

+ 33 - 27
frameworks/C++/drogon/drogon_benchmark/controllers/FortuneCtrlRaw.cc

@@ -7,35 +7,41 @@ void FortuneCtrlRaw::asyncHandleHttpRequest(
     const HttpRequestPtr &req,
     std::function<void(const HttpResponsePtr &)> &&callback)
 {
-    auto client = drogon::app().getFastDbClient();
+    if (!*_dbClient)
+    {
+        *_dbClient = drogon::app().getFastDbClient();
+    }
     auto callbackPtr =
         std::make_shared<std::function<void(const HttpResponsePtr &)>>(
             std::move(callback));
 
-    *client << "select * from fortune where 1=$1" << 1 >> [callbackPtr](
-                                                              const Result &r) {
-        std::vector<std::pair<string_view, string_view>> rows;
-        for (auto const &row : r)
-        {
-            rows.emplace_back(row[0ul].as<string_view>(),  // id
-                              row[1ul].as<string_view>()); // message
-        }
-        rows.emplace_back("0", "Additional fortune added at request time.");
-        std::sort(rows.begin(),
-                  rows.end(),
-                  [](const std::pair<string_view, string_view> &p1,
-                     const std::pair<string_view, string_view> &p2) -> bool {
-                      if (p1.second < p2.second)
-                          return true;
-                      return false;
-                  });
-        HttpViewData data;
-        data.insert("rows", std::move(rows));
-        auto resp = HttpResponse::newHttpViewResponse("fortune_raw.csp", data);
-        (*callbackPtr)(resp);
-    } >> [callbackPtr](const DrogonDbException &err) {
-        auto resp = HttpResponse::newHttpResponse();
-        resp->setBody(std::string("error:") + err.base().what());
-        (*callbackPtr)(resp);
-    };
+    **_dbClient << "select * from fortune where 1=$1" << 1 >>
+        [callbackPtr](const Result &r) {
+            std::vector<std::pair<string_view, string_view>> rows;
+            for (auto const &row : r)
+            {
+                rows.emplace_back(row[0ul].as<string_view>(),   // id
+                                  row[1ul].as<string_view>());  // message
+            }
+            rows.emplace_back("0", "Additional fortune added at request time.");
+            std::sort(rows.begin(),
+                      rows.end(),
+                      [](const std::pair<string_view, string_view> &p1,
+                         const std::pair<string_view, string_view> &p2)
+                          -> bool {
+                          if (p1.second < p2.second)
+                              return true;
+                          return false;
+                      });
+            HttpViewData data;
+            data.insert("rows", std::move(rows));
+            auto resp =
+                HttpResponse::newHttpViewResponse("fortune_raw.csp", data);
+            (*callbackPtr)(resp);
+        } >>
+        [callbackPtr](const DrogonDbException &err) {
+            auto resp = HttpResponse::newHttpResponse();
+            resp->setBody(std::string("error:") + err.base().what());
+            (*callbackPtr)(resp);
+        };
 }

+ 4 - 0
frameworks/C++/drogon/drogon_benchmark/controllers/FortuneCtrlRaw.h

@@ -1,5 +1,7 @@
 #pragma once
 #include <drogon/HttpSimpleController.h>
+#include <drogon/IOThreadStorage.h>
+
 using namespace drogon;
 class FortuneCtrlRaw : public drogon::HttpSimpleController<FortuneCtrlRaw>
 {
@@ -10,4 +12,6 @@ class FortuneCtrlRaw : public drogon::HttpSimpleController<FortuneCtrlRaw>
     PATH_LIST_BEGIN
     // list path definitions here;
     PATH_LIST_END
+  private:
+    IOThreadStorage<orm::DbClientPtr> _dbClient;
 };

+ 3 - 1
frameworks/C++/drogon/drogon_benchmark/controllers/PlaintextCtrl.cc

@@ -1,5 +1,7 @@
 #include "PlaintextCtrl.h"
-void PlaintextCtrl::asyncHandleHttpRequest(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback)
+void PlaintextCtrl::asyncHandleHttpRequest(
+    const HttpRequestPtr &req,
+    std::function<void(const HttpResponsePtr &)> &&callback)
 {
     auto resp = HttpResponse::newHttpResponse();
     resp->setBody("Hello, World!");

+ 5 - 2
frameworks/C++/drogon/drogon_benchmark/controllers/QueriesCtrl.cc

@@ -27,8 +27,11 @@ void QueriesCtrl::asyncHandleHttpRequest(
         std::make_shared<std::function<void(const HttpResponsePtr &)>>(
             std::move(callback));
     auto counter = std::make_shared<int>(queries);
-    auto client = app().getFastDbClient();
-    drogon::orm::Mapper<World> mapper(client);
+    if (!*_dbClient)
+    {
+        *_dbClient = drogon::app().getFastDbClient();
+    }
+    drogon::orm::Mapper<World> mapper(*_dbClient);
 
     for (int i = 0; i < queries; i++)
     {

+ 4 - 0
frameworks/C++/drogon/drogon_benchmark/controllers/QueriesCtrl.h

@@ -1,5 +1,7 @@
 #pragma once
 #include <drogon/HttpSimpleController.h>
+#include <drogon/IOThreadStorage.h>
+
 using namespace drogon;
 class QueriesCtrl : public drogon::HttpSimpleController<QueriesCtrl>
 {
@@ -11,4 +13,6 @@ class QueriesCtrl : public drogon::HttpSimpleController<QueriesCtrl>
     // list path definitions here;
     // PATH_ADD("/path","filter1","filter2",HttpMethod1,HttpMethod2...);
     PATH_LIST_END
+  private:
+    IOThreadStorage<orm::DbClientPtr> _dbClient;
 };

+ 5 - 2
frameworks/C++/drogon/drogon_benchmark/controllers/QueriesCtrlRaw.cc

@@ -21,14 +21,17 @@ void QueriesCtrlRaw::asyncHandleHttpRequest(
         std::make_shared<std::function<void(const HttpResponsePtr &)>>(
             std::move(callback));
     auto counter = std::make_shared<int>(queries);
-    auto client = app().getFastDbClient();
+    if (!*_dbClient)
+    {
+        *_dbClient = drogon::app().getFastDbClient();
+    }
     auto jsonStr = std::make_shared<std::string>();
     jsonStr->reserve(queries * 36);
     jsonStr->append("[", 1);
     for (int i = 0; i < queries; i++)
     {
         int id = rand() % 10000 + 1;
-        *client << "select * from world where id=$1" << id >>
+        **_dbClient << "select * from world where id=$1" << id >>
             [callbackPtr, counter, jsonStr, id](const Result &r) mutable {
                 (*counter)--;
                 if (r.size() > 0)

+ 4 - 0
frameworks/C++/drogon/drogon_benchmark/controllers/QueriesCtrlRaw.h

@@ -1,5 +1,7 @@
 #pragma once
 #include <drogon/HttpSimpleController.h>
+#include <drogon/IOThreadStorage.h>
+
 using namespace drogon;
 class QueriesCtrlRaw : public drogon::HttpSimpleController<QueriesCtrlRaw>
 {
@@ -11,4 +13,6 @@ class QueriesCtrlRaw : public drogon::HttpSimpleController<QueriesCtrlRaw>
     // list path definitions here;
     // PATH_ADD("/path","filter1","filter2",HttpMethod1,HttpMethod2...);
     PATH_LIST_END
+  private:
+    IOThreadStorage<orm::DbClientPtr> _dbClient;
 };

+ 7 - 3
frameworks/C++/drogon/drogon_benchmark/controllers/UpdatesCtrl.cc

@@ -27,15 +27,19 @@ void UpdatesCtrl::asyncHandleHttpRequest(
         std::make_shared<std::function<void(const HttpResponsePtr &)>>(
             std::move(callback));
     auto counter = std::make_shared<int>(queries);
-    auto client = app().getFastDbClient();
-    drogon::orm::Mapper<World> mapper(client);
+    if (!*_dbClient)
+    {
+        *_dbClient = drogon::app().getFastDbClient();
+    }
+    drogon::orm::Mapper<World> mapper(*_dbClient);
 
     for (int i = 0; i < queries; i++)
     {
         World::PrimaryKeyType id = rand() % 10000 + 1;
         mapper.findByPrimaryKey(
             id,
-            [callbackPtr, counter, json, client](World w) mutable {
+            [callbackPtr, counter, json, &client = *_dbClient](
+                World w) mutable {
                 w.setRandomnumber(rand() % 10000 + 1);
                 drogon::orm::Mapper<World> mapper(client);
                 mapper.update(

+ 4 - 0
frameworks/C++/drogon/drogon_benchmark/controllers/UpdatesCtrl.h

@@ -1,5 +1,7 @@
 #pragma once
 #include <drogon/HttpSimpleController.h>
+#include <drogon/IOThreadStorage.h>
+
 using namespace drogon;
 class UpdatesCtrl : public drogon::HttpSimpleController<UpdatesCtrl>
 {
@@ -11,4 +13,6 @@ class UpdatesCtrl : public drogon::HttpSimpleController<UpdatesCtrl>
     // list path definitions here;
     // PATH_ADD("/path","filter1","filter2",HttpMethod1,HttpMethod2...);
     PATH_LIST_END
+  private:
+    IOThreadStorage<orm::DbClientPtr> _dbClient;
 };

+ 6 - 3
frameworks/C++/drogon/drogon_benchmark/controllers/UpdatesCtrlRaw.cc

@@ -87,12 +87,15 @@ void UpdatesCtrlRaw::asyncHandleHttpRequest(
             std::move(callback));
     auto resultSetPtr = std::make_shared<std::vector<Result>>();
     resultSetPtr->reserve(queries);
-    auto client = app().getFastDbClient();
+    if (!*_dbClient)
+    {
+        *_dbClient = drogon::app().getFastDbClient();
+    }
     for (size_t i = 0; i < queries; i++)
     {
         int id = rand() % 10000 + 1;
-        *client << "select * from world where id=$1" << id >>
-            [callbackPtr, resultSetPtr, client, queries](
+        **_dbClient << "select * from world where id=$1" << id >>
+            [callbackPtr, resultSetPtr, &client = *_dbClient, queries](
                 const Result &r) mutable {
                 resultSetPtr->push_back(r);
                 if (resultSetPtr->size() == queries)

+ 4 - 0
frameworks/C++/drogon/drogon_benchmark/controllers/UpdatesCtrlRaw.h

@@ -1,5 +1,7 @@
 #pragma once
 #include <drogon/HttpSimpleController.h>
+#include <drogon/IOThreadStorage.h>
+
 using namespace drogon;
 class UpdatesCtrlRaw : public drogon::HttpSimpleController<UpdatesCtrlRaw>
 {
@@ -11,4 +13,6 @@ class UpdatesCtrlRaw : public drogon::HttpSimpleController<UpdatesCtrlRaw>
     // list path definitions here;
     // PATH_ADD("/path","filter1","filter2",HttpMethod1,HttpMethod2...);
     PATH_LIST_END
+  private:
+    IOThreadStorage<orm::DbClientPtr> _dbClient;
 };

+ 3 - 3
frameworks/C++/drogon/drogon_benchmark/plugins/SyncPlugin.h

@@ -8,11 +8,12 @@
 
 #include <drogon/plugins/Plugin.h>
 
-
 class SyncPlugin : public drogon::Plugin<SyncPlugin>
 {
   public:
-    SyncPlugin() {}
+    SyncPlugin()
+    {
+    }
     /// This method must be called by drogon to initialize and start the plugin.
     /// It must be implemented by the user.
     virtual void initAndStart(const Json::Value &config) override;
@@ -21,4 +22,3 @@ class SyncPlugin : public drogon::Plugin<SyncPlugin>
     /// It must be implemented by the user.
     virtual void shutdown() override;
 };
-