diff --git a/examples/benchmark/main.cc b/examples/benchmark/main.cc index 805f861c..c397a218 100644 --- a/examples/benchmark/main.cc +++ b/examples/benchmark/main.cc @@ -7,5 +7,10 @@ int main() app().setLogLevel(trantor::Logger::WARN); app().addListener("0.0.0.0", 7770); app().setThreadNum(16); + app().enableRunAsDaemon(); + // app().enableRelaunchOnError(); + // app().loop()->runEvery(1, []() { + // LOG_WARN << "HAHA"; + // }); app().run(); } \ No newline at end of file diff --git a/examples/simple_example_test/main.cc b/examples/simple_example_test/main.cc index 5fb55700..be63aabd 100644 --- a/examples/simple_example_test/main.cc +++ b/examples/simple_example_test/main.cc @@ -281,30 +281,30 @@ void doTest(const HttpClientPtr &client) } }); - app().loop()->runAfter(0.5, [=]() mutable { - req = HttpRequest::newHttpRequest(); - req->setMethod(drogon::Post); - req->setPath("/api/v1/apitest/static"); - client->sendRequest(req, [=](ReqResult result, const HttpResponsePtr &resp) { - if (result == ReqResult::Ok) + //auto loop = app().loop(); + + req = HttpRequest::newHttpRequest(); + req->setMethod(drogon::Post); + req->setPath("/api/v1/apitest/static"); + client->sendRequest(req, [=](ReqResult result, const HttpResponsePtr &resp) { + if (result == ReqResult::Ok) + { + if (resp->getBody() == "staticApi,hello!!") { - if (resp->getBody() == "staticApi,hello!!") - { - outputGood(req); - } - else - { - LOG_DEBUG << resp->getBody(); - LOG_ERROR << "Error!"; - exit(1); - } + outputGood(req); } else { + LOG_DEBUG << resp->getBody(); LOG_ERROR << "Error!"; exit(1); } - }); + } + else + { + LOG_ERROR << "Error!"; + exit(1); + } }); req = HttpRequest::newHttpRequest(); diff --git a/lib/inc/drogon/HttpAppFramework.h b/lib/inc/drogon/HttpAppFramework.h index aed33edc..d6881e3d 100755 --- a/lib/inc/drogon/HttpAppFramework.h +++ b/lib/inc/drogon/HttpAppFramework.h @@ -69,11 +69,15 @@ class HttpAppFramework : public trantor::NonCopyable ///Run the event loop /** - * Calling this method will start the event loop which drive the network; - * The thread calling this method must be the first thread to call instance() method; - * Usually the thread calling this method is main thread of the application; + * Calling this method will start the IO event loops and the main loop of the application; + * Usually, the thread that calls this method is the main thread of the application; + * If the loop() method is called before this method, it must be called from the thread + * that first calls the loop () method. + * If all loop() calls are after this method, it can be called from any thread. */ virtual void run() = 0; + + /// Return true if the framework is running virtual bool isRunning() = 0; ///Quit the event loop diff --git a/lib/src/HttpAppFrameworkImpl.cc b/lib/src/HttpAppFrameworkImpl.cc index 763a1cef..812e2852 100755 --- a/lib/src/HttpAppFrameworkImpl.cc +++ b/lib/src/HttpAppFrameworkImpl.cc @@ -208,9 +208,9 @@ void HttpAppFrameworkImpl::run() //go daemon! godaemon(); #ifdef __linux__ - _loop.resetTimerQueue(); + loop()->resetTimerQueue(); #endif - _loop.resetAfterFork(); + loop()->resetAfterFork(); } //set relaunching if (_relaunchOnError) @@ -233,7 +233,7 @@ void HttpAppFrameworkImpl::run() sleep(1); LOG_INFO << "start new process"; } - _loop.resetAfterFork(); + loop()->resetAfterFork(); } //set logger @@ -273,7 +273,7 @@ void HttpAppFrameworkImpl::run() if (!_libFilePaths.empty()) { - _sharedLibManagerPtr = std::unique_ptr(new SharedLibManager(&_loop, _libFilePaths)); + _sharedLibManagerPtr = std::unique_ptr(new SharedLibManager(loop(), _libFilePaths)); } std::vector> servers; std::vector> loopThreads; @@ -370,18 +370,17 @@ void HttpAppFrameworkImpl::run() tmpTimeout = tmpTimeout / 100; } } - _sessionMapPtr = std::unique_ptr>(new CacheMap(&_loop, 1.0, wheelNum, bucketNum)); + _sessionMapPtr = std::unique_ptr>(new CacheMap(loop(), 1.0, wheelNum, bucketNum)); } else if (_sessionTimeout == 0) { - _sessionMapPtr = std::unique_ptr>(new CacheMap(&_loop, 0, 0, 0)); + _sessionMapPtr = std::unique_ptr>(new CacheMap(loop(), 0, 0, 0)); } } - _responseCachingMap = std::unique_ptr>(new CacheMap(&_loop, 1.0, 4, 50)); //Max timeout up to about 70 days; - _loop.loop(); + _responseCachingMap = std::unique_ptr>(new CacheMap(loop(), 1.0, 4, 50)); //Max timeout up to about 70 days; + loop()->loop(); } - void HttpAppFrameworkImpl::onWebsockDisconnect(const WebSocketConnectionPtr &wsConnPtr) { auto wsConnImplPtr = std::dynamic_pointer_cast(wsConnPtr); @@ -808,7 +807,8 @@ void HttpAppFrameworkImpl::readSendFile(const std::string &filePath, const HttpR trantor::EventLoop *HttpAppFrameworkImpl::loop() { - return &_loop; + static trantor::EventLoop loop; + return &loop; } HttpAppFramework &HttpAppFramework::instance() diff --git a/lib/src/HttpAppFrameworkImpl.h b/lib/src/HttpAppFrameworkImpl.h index 3f91d090..90964072 100644 --- a/lib/src/HttpAppFrameworkImpl.h +++ b/lib/src/HttpAppFrameworkImpl.h @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -102,8 +103,8 @@ class HttpAppFrameworkImpl : public HttpAppFramework virtual trantor::EventLoop *loop() override; virtual void quit() override { - if (_loop.isRunning()) - _loop.quit(); + if (loop()->isRunning()) + loop()->quit(); } #if USE_ORM virtual orm::DbClientPtr getDbClient(const std::string &name = "default") override; @@ -164,8 +165,6 @@ class HttpAppFrameworkImpl : public HttpAppFramework std::unique_ptr _sharedLibManagerPtr; - trantor::EventLoop _loop; - std::string _sslCertPath; std::string _sslKeyPath; diff --git a/lib/src/HttpClientImpl.cc b/lib/src/HttpClientImpl.cc index c2289725..d59f4426 100644 --- a/lib/src/HttpClientImpl.cc +++ b/lib/src/HttpClientImpl.cc @@ -94,8 +94,9 @@ HttpClientImpl::~HttpClientImpl() void HttpClientImpl::sendRequest(const drogon::HttpRequestPtr &req, const drogon::HttpReqCallback &callback) { - _loop->runInLoop([=]() { - sendRequestInLoop(req, callback); + auto thisPtr = shared_from_this(); + _loop->runInLoop([thisPtr,req,callback]() { + thisPtr->sendRequestInLoop(req, callback); }); } diff --git a/trantor b/trantor index 2c78af9e..607ed1a5 160000 --- a/trantor +++ b/trantor @@ -1 +1 @@ -Subproject commit 2c78af9ea6a7dac972940f61ba12c84a9915cab0 +Subproject commit 607ed1a57b35e4defa686cbd55ec487efc60b299