Skip to content

Commit

Permalink
make HttpThreadPoolSize configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
laoshanxi committed Sep 17, 2019
1 parent 9abdfe9 commit eb55242
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 33 deletions.
31 changes: 8 additions & 23 deletions ApplicationManager/Configuration.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#include "Configuration.h"
#include <json/reader.h>
#include "../common/Utility.h"
#include "ApplicationPeriodRun.h"

#define DEFAULT_SCHEDULE_INTERVAL 3

std::shared_ptr<Configuration> Configuration::m_instance = nullptr;
Configuration::Configuration()
:m_scheduleInterval(0), m_restListenPort(DEFAULT_REST_LISTEN_PORT), m_sslEnabled(false), m_restEnabled(true), m_jwtEnabled(true)
:m_scheduleInterval(0), m_restListenPort(DEFAULT_REST_LISTEN_PORT), m_sslEnabled(false), m_restEnabled(true), m_jwtEnabled(true), m_threadPoolSize(6)
{
m_jsonFilePath = Utility::getSelfFullPath() + ".json";
LOG_INF << "Configuration file <" << m_jsonFilePath << ">";
Expand Down Expand Up @@ -77,7 +76,11 @@ std::shared_ptr<Configuration> Configuration::FromJson(const std::string& str)
config->m_jwtAdminKey = GET_STD_STRING(jwt.at(GET_STRING_T("admin")).as_object().at(GET_STRING_T("key")).as_string());
config->m_jwtUserName = GET_STD_STRING(jwt.at(GET_STRING_T("user")).as_object().at(GET_STRING_T("name")).as_string());
config->m_jwtUserKey = GET_STD_STRING(jwt.at(GET_STRING_T("user")).as_object().at(GET_STRING_T("key")).as_string());

auto threadpool = GET_JSON_INT_VALUE(jobj, "HttpThreadPoolSize");
if (threadpool > 0 && threadpool < 40)
{
config->m_threadPoolSize = threadpool;
}
config->parseTags(jobj.at(GET_STRING_T("Lables")));

m_instance = config;
Expand All @@ -101,8 +104,8 @@ web::json::value Configuration::AsJson(bool returnRuntimeInfo)
result[GET_STRING_T("SSLEnabled")] = web::json::value::boolean(m_sslEnabled);
result[GET_STRING_T("SSLCertificateFile")] = web::json::value::string(GET_STRING_T(m_sslCertificateFile));
result[GET_STRING_T("SSLCertificateKeyFile")] = web::json::value::string(GET_STRING_T(m_sslCertificateKeyFile));

result[GET_STRING_T("JWTEnabled")] = web::json::value::boolean(m_jwtEnabled);
result[GET_STRING_T("HttpThreadPoolSize")] = web::json::value::number(m_threadPoolSize);
if (!returnRuntimeInfo)
{
web::json::value jwt = web::json::value::object();
Expand Down Expand Up @@ -354,7 +357,7 @@ void Configuration::saveConfigToDisk()
std::ofstream ofs(tmpFile, ios::trunc);
if (ofs.is_open())
{
ofs << prettyJson(content);
ofs << Utility::prettyJson(content);
ofs.close();
if (ACE_OS::rename(tmpFile.c_str(), m_jsonFilePath.c_str()) == 0)
{
Expand Down Expand Up @@ -404,24 +407,6 @@ std::shared_ptr<Application> Configuration::parseApp(web::json::object jsonApp)
return app;
}

std::string Configuration::prettyJson(const std::string & jsonStr)
{
static Json::CharReaderBuilder builder;
static Json::CharReader* reader(builder.newCharReader());
Json::Value root;
Json::String errs;
if (reader->parse(jsonStr.c_str(), jsonStr.c_str() + std::strlen(jsonStr.c_str()), &root, &errs))
{
return root.toStyledString();
}
else
{
std::string msg = "Failed to parse json : " + jsonStr + " with error :" + errs;
LOG_ERR << msg;
throw std::invalid_argument(msg);
}
}

std::shared_ptr<Application> Configuration::getApp(const std::string & appName)
{
std::vector<std::shared_ptr<Application>> apps = getApps();
Expand Down
6 changes: 4 additions & 2 deletions ApplicationManager/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class Configuration
const std::string & getJwtUserName() const;
const std::string & getJwtAdminKey() const;
const std::string & getJwtUserKey() const;

static std::string prettyJson(const std::string & jsonStr);
const size_t getThreadPoolSize() const { return m_threadPoolSize; }

void dump();

private:
Expand Down Expand Up @@ -79,6 +79,8 @@ class Configuration
std::string m_jwtAdminKey;
std::string m_jwtUserKey;

size_t m_threadPoolSize;

static std::shared_ptr<Configuration> m_instance;
};

Expand Down
8 changes: 4 additions & 4 deletions ApplicationManager/RestHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ void RestHandler::apiRegShellApp(const http_request& message)
LOG_DBG << fname << "Shell app json: " << jsonApp.serialize();

auto app = Configuration::instance()->addApp(jobj);
message.reply(status_codes::OK, Configuration::prettyJson(GET_STD_STRING(app->AsJson(true).serialize())));
message.reply(status_codes::OK, Utility::prettyJson(GET_STD_STRING(app->AsJson(true).serialize())));
}

void RestHandler::apiControlApp(const http_request & message)
Expand Down Expand Up @@ -587,7 +587,7 @@ void RestHandler::apiGetApp(const http_request& message)
{
auto path = GET_STD_STRING(http::uri::decode(message.relative_uri().path()));
std::string app = path.substr(strlen("/app/"));
message.reply(status_codes::OK, Configuration::prettyJson(GET_STD_STRING(Configuration::instance()->getApp(app)->AsJson(true).serialize())));
message.reply(status_codes::OK, Utility::prettyJson(GET_STD_STRING(Configuration::instance()->getApp(app)->AsJson(true).serialize())));
}

void RestHandler::apiRunApp(const http_request& message)
Expand Down Expand Up @@ -738,7 +738,7 @@ void RestHandler::apiGetApps(const http_request& message)

void RestHandler::apiGetResources(const http_request& message)
{
message.reply(status_codes::OK, Configuration::prettyJson(GET_STD_STRING(ResourceCollection::instance()->AsJson().serialize())));
message.reply(status_codes::OK, Utility::prettyJson(GET_STD_STRING(ResourceCollection::instance()->AsJson().serialize())));
}

void RestHandler::apiRegApp(const http_request& message)
Expand All @@ -749,5 +749,5 @@ void RestHandler::apiRegApp(const http_request& message)
throw std::invalid_argument("invalid json format");
}
auto app = Configuration::instance()->addApp(jsonApp.as_object());
message.reply(status_codes::OK, Configuration::prettyJson(GET_STD_STRING(app->AsJson(false).serialize())));
message.reply(status_codes::OK, Utility::prettyJson(GET_STD_STRING(app->AsJson(false).serialize())));
}
1 change: 1 addition & 0 deletions ApplicationManager/appsvc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"SSLEnabled": true,
"SSLCertificateFile": "server.crt",
"SSLCertificateKeyFile": "server.key",
"HttpThreadPoolSize": 6,
"JWTEnabled": true,
"Applications": [
{
Expand Down
5 changes: 3 additions & 2 deletions ApplicationManager/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,16 @@ int main(int argc, char * argv[])
ACE::init();
Utility::initLogging();

LOG_DBG << fname << "Entered.";
LOG_INF << fname << "Entered.";

auto config = readConfiguration();
Utility::setLogLevel(config->getLogLevel());
if (config->getRestEnabled())
{
// 1. Thread pool: 6 threads
crossplat::threadpool::initialize_with_threads(6);
crossplat::threadpool::initialize_with_threads(config->getThreadPoolSize());
m_httpHandler = std::make_shared<RestHandler>(config->getRestListenIp(), config->getRestListenPort());
LOG_INF << fname << "initialize_with_threads:" << config->getThreadPoolSize();
}

auto apps = config->getApps();
Expand Down
2 changes: 1 addition & 1 deletion CommandLine/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ CXXFLAGS = -std=c++11 -ggdb3 -Wall -O2
OEXT = o

INCLUDES = -I/usr/local/include -I/usr/local/ace/include/
DEP_LIBS = -L/usr/local/ace/lib/ -L/usr/local/lib -L/usr/local/lib64 -lpthread -lssl -lcrypto -lcpprest -lboost_system -lboost_program_options -lACE -Wl,-Bstatic -llog4cpp -Wl,-Bdynamic
DEP_LIBS = -L/usr/local/ace/lib/ -L/usr/local/lib -L/usr/local/lib64 -lpthread -lssl -lcrypto -lcpprest -lboost_system -lboost_program_options -lACE -Wl,-Bstatic -llog4cpp -ljsoncpp -Wl,-Bdynamic

all : format $(TARGET)

Expand Down
20 changes: 19 additions & 1 deletion common/Utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <log4cpp/PatternLayout.hh>
#include <log4cpp/RollingFileAppender.hh>
#include <log4cpp/OstreamAppender.hh>

#include <json/reader.h>
#include <ace/UUID.h>

#include "../common/Utility.h"
Expand Down Expand Up @@ -606,3 +606,21 @@ void Utility::getEnvironmentSize(const std::map<std::string, std::string>& envMa
totalEnvArgs += numEntriesConst;
totalEnvSize += bufferSizeConst;
}

std::string Utility::prettyJson(const std::string & jsonStr)
{
static Json::CharReaderBuilder builder;
static Json::CharReader* reader(builder.newCharReader());
Json::Value root;
Json::String errs;
if (reader->parse(jsonStr.c_str(), jsonStr.c_str() + std::strlen(jsonStr.c_str()), &root, &errs))
{
return root.toStyledString();
}
else
{
std::string msg = "Failed to parse json : " + jsonStr + " with error :" + errs;
LOG_ERR << msg;
throw std::invalid_argument(msg);
}
}
1 change: 1 addition & 0 deletions common/Utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class Utility
static bool startWith(const std::string& str, std::string head);
static std::string stringReplace(const std::string &strBase, const std::string& strSrc, const std::string& strDst);
static std::string humanReadableSize(long double bytesSize);
static std::string prettyJson(const std::string & jsonStr);

static void initLogging();
static void setLogLevel(const std::string & level);
Expand Down

0 comments on commit eb55242

Please sign in to comment.