Merge pull request #66 from an-tao/dev

Do some optimization
This commit is contained in:
An Tao 2019-03-02 12:45:17 +08:00 committed by GitHub
commit cf29b08050
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 20 additions and 41 deletions

View File

@ -17,7 +17,6 @@
#include <drogon/config.h>
#if USE_ORM
#include <drogon/orm/DbClient.h>
#define USE_FAST_CLIENT 1
#endif
#include <drogon/utils/Utilities.h>
#include <drogon/HttpBinder.h>
@ -187,10 +186,8 @@ class HttpAppFramework : public trantor::NonCopyable
virtual void setServerHeaderField(const std::string &server) = 0;
#if USE_ORM
virtual orm::DbClientPtr getDbClient(const std::string &name = "default") = 0;
#if USE_FAST_CLIENT
virtual void enableFastDbClient() = 0;
virtual orm::DbClientPtr getFastDbClient(const std::string &name = "default") = 0;
#endif
virtual void createDbClient(const std::string &dbType,
const std::string &host,
const u_short port,

View File

@ -229,12 +229,10 @@ static void loadApp(const Json::Value &app)
auto kickOffTimeout = app.get("idle_connection_timeout", 60).asUInt64();
drogon::app().setIdleConnectionTimeout(kickOffTimeout);
#if USE_ORM
#if USE_FAST_CLIENT
//Fast db client
auto fastDbClient = app.get("enable_fast_db_client", false).asBool();
if (fastDbClient)
drogon::app().enableFastDbClient();
#endif
#endif
auto server = app.get("server_header_field", "").asString();
if(!server.empty())

View File

@ -16,10 +16,8 @@
#include "ConfigLoader.h"
#include "HttpServer.h"
#if USE_ORM
#if USE_FAST_CLIENT
#include "../../orm_lib/src/DbClientLockFree.h"
#endif
#endif
#include <drogon/HttpTypes.h>
#include <drogon/utils/Utilities.h>
#include <drogon/DrClassMap.h>
@ -68,8 +66,9 @@ static void godaemon(void)
close(1);
close(2);
open("/dev/null", O_RDWR);
int ret = dup(0);
int ret = open("/dev/null", O_RDWR);
(void)ret;
ret = dup(0);
(void)ret;
ret = dup(0);
(void)ret;
@ -373,11 +372,9 @@ void HttpAppFrameworkImpl::run()
#endif
#if USE_ORM
#if USE_FAST_CLIENT
// Create fast db clients for every io loop
if (_enableFastDbClient)
createFastDbClient(ioLoops);
#endif
#endif
_httpCtrlsRouter.init(ioLoops);
_httpSimpleCtrlsRouter.init(ioLoops);
@ -414,7 +411,6 @@ void HttpAppFrameworkImpl::run()
loop()->loop();
}
#if USE_ORM
#if USE_FAST_CLIENT
void HttpAppFrameworkImpl::createFastDbClient(const std::vector<trantor::EventLoop *> &ioloops)
{
for (auto &iter : _dbClientsMap)
@ -433,7 +429,6 @@ void HttpAppFrameworkImpl::createFastDbClient(const std::vector<trantor::EventLo
}
}
#endif
#endif
void HttpAppFrameworkImpl::onWebsockDisconnect(const WebSocketConnectionPtr &wsConnPtr)
{
auto wsConnImplPtr = std::dynamic_pointer_cast<WebSocketConnectionImpl>(wsConnPtr);
@ -813,12 +808,10 @@ orm::DbClientPtr HttpAppFrameworkImpl::getDbClient(const std::string &name)
{
return _dbClientsMap[name];
}
#if USE_FAST_CLIENT
orm::DbClientPtr HttpAppFrameworkImpl::getFastDbClient(const std::string &name)
{
return _dbFastClientsMap[name][trantor::EventLoop::getEventLoopOfCurrentThread()];
}
#endif
void HttpAppFrameworkImpl::createDbClient(const std::string &dbType,
const std::string &host,
const u_short port,

View File

@ -119,13 +119,11 @@ class HttpAppFrameworkImpl : public HttpAppFramework
#if USE_ORM
virtual orm::DbClientPtr getDbClient(const std::string &name = "default") override;
#if USE_FAST_CLIENT
virtual void enableFastDbClient() override
{
_enableFastDbClient = true;
}
virtual orm::DbClientPtr getFastDbClient(const std::string &name = "default") override;
#endif
virtual void createDbClient(const std::string &dbType,
const std::string &host,
const u_short port,
@ -214,11 +212,9 @@ class HttpAppFrameworkImpl : public HttpAppFramework
#if USE_ORM
std::map<std::string, orm::DbClientPtr> _dbClientsMap;
std::vector<std::function<void()>> _dbFuncs;
#if USE_FAST_CLIENT
std::map<std::string, std::map<trantor::EventLoop *, orm::DbClientPtr>> _dbFastClientsMap;
void createFastDbClient(const std::vector<trantor::EventLoop *> &ioloops);
#endif
#endif
};
} // namespace drogon

View File

@ -36,7 +36,7 @@ class HttpClientImpl : public HttpClient, public std::enable_shared_from_this<Ht
trantor::EventLoop *_loop;
trantor::InetAddress _server;
bool _useSSL;
void sendReq(const trantor::TcpConnectionPtr &connectorPtr, const HttpRequestPtr &req);
void sendReq(const trantor::TcpConnectionPtr &connPtr, const HttpRequestPtr &req);
void sendRequestInLoop(const HttpRequestPtr &req, const HttpReqCallback &callback);
std::queue<std::pair<HttpRequestPtr, HttpReqCallback>> _reqAndCallbacks;
void onRecvMessage(const trantor::TcpConnectionPtr &, trantor::MsgBuffer *);

View File

@ -52,17 +52,12 @@ class HttpRequestParser
_request.reset(new HttpRequestImpl(_loop));
}
const HttpRequestPtr request() const
{
return _request;
}
// const HttpRequestPtr &request() const
// {
// return _request;
// }
HttpRequestImplPtr request()
{
return _request;
}
HttpRequestImplPtr requestImpl()
const HttpRequestImplPtr &requestImpl() const
{
return _request;
}
@ -76,7 +71,7 @@ class HttpRequestParser
}
return false;
}
WebSocketConnectionPtr webSocketConn()
const WebSocketConnectionPtr &webSocketConn() const
{
return _websockConnPtr;
}

View File

@ -123,10 +123,10 @@ void HttpServer::onMessage(const TcpConnectionPtr &conn,
requestParser->requestImpl()->setPeerAddr(conn->peerAddr());
requestParser->requestImpl()->setLocalAddr(conn->localAddr());
requestParser->requestImpl()->setReceiveDate(trantor::Date::date());
if (requestParser->firstReq() && isWebSocket(conn, requestParser->request()))
if (requestParser->firstReq() && isWebSocket(conn, requestParser->requestImpl()))
{
auto wsConn = std::make_shared<WebSocketConnectionImpl>(conn);
_newWebsocketCallback(requestParser->request(),
_newWebsocketCallback(requestParser->requestImpl(),
[=](const HttpResponsePtr &resp) mutable {
if (resp->statusCode() == k101SwitchingProtocols)
{
@ -138,7 +138,7 @@ void HttpServer::onMessage(const TcpConnectionPtr &conn,
wsConn);
}
else
onRequest(conn, requestParser->request());
onRequest(conn, requestParser->requestImpl());
requestParser->reset();
}
}

View File

@ -84,7 +84,7 @@ class DbClientImpl : public DbClient, public std::enable_shared_from_this<DbClie
const size_t paraNum,
std::vector<const char *> &&parameters,
std::vector<int> &&length,
std::vector<int> format,
std::vector<int> &&format,
QueryCallback &&cb,
ExceptPtrCallback &&exceptCb)
: _sql(std::move(sql)),
@ -100,7 +100,7 @@ class DbClientImpl : public DbClient, public std::enable_shared_from_this<DbClie
std::deque<std::shared_ptr<SqlCmd>> _sqlCmdBuffer;
std::mutex _bufferMutex;
void handleNewTask(const DbConnectionPtr &conn);
void handleNewTask(const DbConnectionPtr &connPtr);
};
} // namespace orm

View File

@ -66,7 +66,7 @@ class DbClientLockFree : public DbClient, public std::enable_shared_from_this<Db
const size_t paraNum,
std::vector<const char *> &&parameters,
std::vector<int> &&length,
std::vector<int> format,
std::vector<int> &&format,
QueryCallback &&cb,
ExceptPtrCallback &&exceptCb)
: _sql(std::move(sql)),

View File

@ -68,7 +68,7 @@ class MysqlResultImpl : public ResultImpl
}
virtual size_type size() const noexcept override;
virtual row_size_type columns() const noexcept override;
virtual const char *columnName(row_size_type Number) const override;
virtual const char *columnName(row_size_type number) const override;
virtual size_type affectedRows() const noexcept override;
virtual row_size_type columnNumber(const char colName[]) const override;
virtual const char *getValue(size_type row, row_size_type column) const override;

View File

@ -35,7 +35,7 @@ class PostgreSQLResultImpl : public ResultImpl
}
virtual size_type size() const noexcept override;
virtual row_size_type columns() const noexcept override;
virtual const char *columnName(row_size_type Number) const override;
virtual const char *columnName(row_size_type number) const override;
virtual size_type affectedRows() const noexcept override;
virtual row_size_type columnNumber(const char colName[]) const override;
virtual const char *getValue(size_type row, row_size_type column) const override;

View File

@ -29,13 +29,13 @@ namespace orm
class Sqlite3ResultImpl : public ResultImpl
{
public:
Sqlite3ResultImpl(const std::string &query) noexcept
explicit Sqlite3ResultImpl(const std::string &query) noexcept
: ResultImpl(query)
{
}
virtual size_type size() const noexcept override;
virtual row_size_type columns() const noexcept override;
virtual const char *columnName(row_size_type Number) const override;
virtual const char *columnName(row_size_type number) const override;
virtual size_type affectedRows() const noexcept override;
virtual row_size_type columnNumber(const char colName[]) const override;
virtual const char *getValue(size_type row, row_size_type column) const override;