Merge pull request #43 from an-tao/dev

Fix reference count error in HttpClientImpl class
This commit is contained in:
An Tao 2019-01-25 20:01:55 +08:00 committed by GitHub
commit 388ef634af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 46 additions and 37 deletions

View File

@ -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();
}

View File

@ -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();

View File

@ -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

View File

@ -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<SharedLibManager>(new SharedLibManager(&_loop, _libFilePaths));
_sharedLibManagerPtr = std::unique_ptr<SharedLibManager>(new SharedLibManager(loop(), _libFilePaths));
}
std::vector<std::shared_ptr<HttpServer>> servers;
std::vector<std::shared_ptr<EventLoopThread>> loopThreads;
@ -370,18 +370,17 @@ void HttpAppFrameworkImpl::run()
tmpTimeout = tmpTimeout / 100;
}
}
_sessionMapPtr = std::unique_ptr<CacheMap<std::string, SessionPtr>>(new CacheMap<std::string, SessionPtr>(&_loop, 1.0, wheelNum, bucketNum));
_sessionMapPtr = std::unique_ptr<CacheMap<std::string, SessionPtr>>(new CacheMap<std::string, SessionPtr>(loop(), 1.0, wheelNum, bucketNum));
}
else if (_sessionTimeout == 0)
{
_sessionMapPtr = std::unique_ptr<CacheMap<std::string, SessionPtr>>(new CacheMap<std::string, SessionPtr>(&_loop, 0, 0, 0));
_sessionMapPtr = std::unique_ptr<CacheMap<std::string, SessionPtr>>(new CacheMap<std::string, SessionPtr>(loop(), 0, 0, 0));
}
}
_responseCachingMap = std::unique_ptr<CacheMap<std::string, HttpResponsePtr>>(new CacheMap<std::string, HttpResponsePtr>(&_loop, 1.0, 4, 50)); //Max timeout up to about 70 days;
_loop.loop();
_responseCachingMap = std::unique_ptr<CacheMap<std::string, HttpResponsePtr>>(new CacheMap<std::string, HttpResponsePtr>(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<WebSocketConnectionImpl>(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()

View File

@ -26,6 +26,7 @@
#include <drogon/HttpAppFramework.h>
#include <drogon/HttpSimpleController.h>
#include <drogon/version.h>
#include <trantor/net/EventLoop.h>
#include <string>
#include <vector>
@ -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<SharedLibManager> _sharedLibManagerPtr;
trantor::EventLoop _loop;
std::string _sslCertPath;
std::string _sslKeyPath;

View File

@ -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);
});
}

@ -1 +1 @@
Subproject commit 2c78af9ea6a7dac972940f61ba12c84a9915cab0
Subproject commit 607ed1a57b35e4defa686cbd55ec487efc60b299