diff --git a/config.example.json b/config.example.json index bdc6b04d..6c6e05ee 100644 --- a/config.example.json +++ b/config.example.json @@ -67,7 +67,7 @@ //log:set log output,drogon output logs to stdout by default "log": { //log_path:log file path,empty by default,in which case,log will output to the stdout - "log_path": "./", + //"log_path": "./", //logfile_base_name:log file base name,empty by default which means drogon will name logfile as //drogon.log ... "logfile_base_name": "", @@ -76,7 +76,7 @@ "log_size_limit": 100000000, //log_level:"DEBUG" by default,options:"TRACE","DEBUG","INFO","WARN" //The TRACE level is only valid when built in DEBUG mode. - "log_level": "DEBUG" + "log_level": "TRACE" }, //run_as_daemon:false by default "run_as_daemon": false, @@ -98,6 +98,9 @@ "http_methods": ["get","post"], "filters": ["FilterClassName"] } - ] + ], + //idle_connection_timeout: defaults to 60 seconds, the lifetime + //of the connection without read or write + "idle_connection_timeout":10 } -} \ No newline at end of file +} diff --git a/lib/inc/drogon/HttpAppFramework.h b/lib/inc/drogon/HttpAppFramework.h index 7e2b8167..7d29277f 100755 --- a/lib/inc/drogon/HttpAppFramework.h +++ b/lib/inc/drogon/HttpAppFramework.h @@ -135,6 +135,7 @@ class HttpAppFramework : public trantor::NonCopyable virtual bool useGzip() const = 0; virtual void setStaticFilesCacheTime(int cacheTime) = 0; virtual int staticFilesCacheTime() const = 0; + virtual void setIdleConnectionTimeout(size_t timeout) = 0; private: virtual void registerHttpApiController(const std::string &pathPattern, diff --git a/lib/src/ConfigLoader.cc b/lib/src/ConfigLoader.cc index bd930e29..f894f502 100644 --- a/lib/src/ConfigLoader.cc +++ b/lib/src/ConfigLoader.cc @@ -127,7 +127,7 @@ static void loadControllers(const Json::Value &controllers) constraints.push_back(filter.asString()); } } - HttpAppFramework::instance().registerHttpSimpleController(path,ctrlName,constraints); + HttpAppFramework::instance().registerHttpSimpleController(path, ctrlName, constraints); } } static void loadApp(const Json::Value &app) @@ -212,6 +212,9 @@ static void loadApp(const Json::Value &app) auto staticFilesCacheTime = app.get("static_files_cache_time", 5).asInt(); HttpAppFramework::instance().setStaticFilesCacheTime(staticFilesCacheTime); loadControllers(app["simple_controllers_map"]); + //Kick off idle connections + auto kickOffTimeout = app.get("idle_connection_timeout", 60).asUInt64(); + HttpAppFramework::instance().setIdleConnectionTimeout(kickOffTimeout); } static void loadListeners(const Json::Value &listeners) { diff --git a/lib/src/HttpAppFrameworkImpl.cc b/lib/src/HttpAppFrameworkImpl.cc index 3890aeb1..e39c86cb 100755 --- a/lib/src/HttpAppFrameworkImpl.cc +++ b/lib/src/HttpAppFrameworkImpl.cc @@ -432,6 +432,7 @@ void HttpAppFrameworkImpl::run() } serverPtr->setHttpAsyncCallback(std::bind(&HttpAppFrameworkImpl::onAsyncRequest, this, _1, _2)); serverPtr->setConnectionCallback(std::bind(&HttpAppFrameworkImpl::onConnection, this, _1)); + serverPtr->kickoffIdleConnections(_idleConnectionTimeout); serverPtr->start(); servers.push_back(serverPtr); } @@ -465,6 +466,7 @@ void HttpAppFrameworkImpl::run() serverPtr->setWebsocketMessageCallback(std::bind(&HttpAppFrameworkImpl::onWebsockMessage, this, _1, _2)); serverPtr->setDisconnectWebsocketCallback(std::bind(&HttpAppFrameworkImpl::onWebsockDisconnect, this, _1)); serverPtr->setConnectionCallback(std::bind(&HttpAppFrameworkImpl::onConnection, this, _1)); + serverPtr->kickoffIdleConnections(_idleConnectionTimeout); serverPtr->start(); servers.push_back(serverPtr); #endif diff --git a/lib/src/HttpAppFrameworkImpl.h b/lib/src/HttpAppFrameworkImpl.h index 44be39e7..bb8e17a0 100644 --- a/lib/src/HttpAppFrameworkImpl.h +++ b/lib/src/HttpAppFrameworkImpl.h @@ -76,6 +76,7 @@ class HttpAppFrameworkImpl : public HttpAppFramework virtual bool useGzip() const override { return _useGzip; } virtual void setStaticFilesCacheTime(int cacheTime) override { _staticFilesCacheTime = cacheTime; } virtual int staticFilesCacheTime() const override { return _staticFilesCacheTime; } + virtual void setIdleConnectionTimeout(size_t timeout) override { _idleConnectionTimeout = timeout; } virtual ~HttpAppFrameworkImpl() { //Destroy the following objects before _loop destruction @@ -108,6 +109,7 @@ class HttpAppFrameworkImpl : public HttpAppFramework //if uuid package found,we can use a uuid string as session id; //set _sessionTimeout=0 to make location session valid forever based on cookies; size_t _sessionTimeout = 0; + size_t _idleConnectionTimeout = 60; bool _useSession = false; typedef std::shared_ptr SessionPtr; std::unique_ptr> _sessionMapPtr; diff --git a/lib/src/HttpServer.h b/lib/src/HttpServer.h index 5be4cea8..152bcfed 100755 --- a/lib/src/HttpServer.h +++ b/lib/src/HttpServer.h @@ -73,7 +73,10 @@ class HttpServer : trantor::NonCopyable { server_.setIoLoopNum(numThreads); } - + void kickoffIdleConnections(size_t timeout) + { + server_.kickoffIdleConnections(timeout); + } void start(); #ifdef USE_OPENSSL diff --git a/trantor b/trantor index 531fa9df..2bfc3572 160000 --- a/trantor +++ b/trantor @@ -1 +1 @@ -Subproject commit 531fa9df4b81adfcd7d202f37455f31f0915728d +Subproject commit 2bfc35721c70625cbd1393fa3a249c50fca1a1dc