Merge pull request #8 from an-tao/kickoff_idle_connections

Kick off idle connections, add the corresponding option in configure file(config.example.json).
This commit is contained in:
an-tao 2018-10-24 16:54:38 +08:00 committed by GitHub
commit 6699429a7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 21 additions and 7 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -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<Session> SessionPtr;
std::unique_ptr<CacheMap<std::string, SessionPtr>> _sessionMapPtr;

View File

@ -73,7 +73,10 @@ class HttpServer : trantor::NonCopyable
{
server_.setIoLoopNum(numThreads);
}
void kickoffIdleConnections(size_t timeout)
{
server_.kickoffIdleConnections(timeout);
}
void start();
#ifdef USE_OPENSSL

@ -1 +1 @@
Subproject commit 531fa9df4b81adfcd7d202f37455f31f0915728d
Subproject commit 2bfc35721c70625cbd1393fa3a249c50fca1a1dc