Add the reuse_port option (#634)

This commit is contained in:
An Tao 2020-11-25 19:47:36 +08:00 committed by GitHub
parent 69d687dbcb
commit 7dd2d6123b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 41 additions and 7 deletions

View File

@ -217,7 +217,9 @@
"client_max_memory_body_size": "64K",
//client_max_websocket_message_size: Set the maximum size of messages sent by WebSocket client. The default value is "128K".
//One can set it to "1024", "1k", "10M", "1G", etc. Setting it to "" means no limit.
"client_max_websocket_message_size": "128K"
"client_max_websocket_message_size": "128K",
//reuse_port: Defaults to false, users can run multiple processes listening on the same port at the same time.
"reuse_port": false
},
//plugins: Define all plugins running in the application
"plugins": [{

View File

@ -217,7 +217,9 @@
"client_max_memory_body_size": "64K",
//client_max_websocket_message_size: Set the maximum size of messages sent by WebSocket client. The default value is "128K".
//One can set it to "1024", "1k", "10M", "1G", etc. Setting it to "" means no limit.
"client_max_websocket_message_size": "128K"
"client_max_websocket_message_size": "128K",
//reuse_port: Defaults to false, users can run multiple processes listening on the same port at the same time.
"reuse_port": false
},
//plugins: Define all plugins running in the application
"plugins": [{

View File

@ -20,7 +20,10 @@ void TimeFilter::doFilter(const HttpRequestPtr &req,
{
auto lastDate = req->session()->get<trantor::Date>(VDate);
LOG_TRACE << "last:" << lastDate.toFormattedString(false);
req->session()->insert(VDate, now);
req->session()->modify<trantor::Date>(VDate,
[now](trantor::Date &vdate) {
vdate = now;
});
LOG_TRACE << "update visitDate";
if (now > lastDate.after(10))
{

View File

@ -1172,6 +1172,21 @@ class HttpAppFramework : public trantor::NonCopyable
*/
virtual std::vector<trantor::InetAddress> getListeners() const = 0;
/**
* @brief Enable ReusePort mode or not. If the mode is enabled, one can run
* multiple processes listening to the same port at the same time. If this
* method is not called, the feature is disabled.
*
* @note
* This operation can be performed by an option in the configuration file.
*/
virtual void enableReusePort(bool enable = true) = 0;
/**
* @brief Return if the ReusePort mode is enabled.
*/
virtual bool reusePort() const = 0;
private:
virtual void registerHttpController(
const std::string &pathPattern,

View File

@ -1,7 +1,7 @@
/**
*
* @file Session.h
* An Tao
* @author An Tao
*
* Copyright 2018, An Tao. All rights reserved.
* https://github.com/an-tao/drogon
@ -117,6 +117,7 @@ class Session
* @code
sessionPtr->insert("user name", userNameString);
@endcode
* @note If the key already exists, the element is not inserted.
*/
void insert(const std::string &key, const any &obj)
{
@ -130,6 +131,7 @@ class Session
* @code
sessionPtr->insert("user name", userNameString);
@endcode
* @note If the key already exists, the element is not inserted.
*/
void insert(const std::string &key, any &&obj)
{

View File

@ -452,6 +452,7 @@ static void loadApp(const Json::Value &app)
<< std::endl;
exit(1);
}
drogon::app().enableReusePort(app.get("reuse_port", false).asBool());
drogon::app().setHomePage(app.get("home_page", "index.html").asString());
}
static void loadDbClients(const Json::Value &dbClients)

View File

@ -488,6 +488,14 @@ class HttpAppFrameworkImpl : public HttpAppFramework
{
return usingCustomErrorHandler_;
}
virtual void enableReusePort(bool enable = true) override
{
reusePort_ = enable;
}
virtual bool reusePort() const override
{
return reusePort_;
}
private:
virtual void registerHttpController(
@ -581,6 +589,7 @@ class HttpAppFrameworkImpl : public HttpAppFramework
static InitBeforeMainFunction initFirst_;
bool enableServerHeader_{true};
bool enableDateHeader_{true};
bool reusePort_{false};
std::vector<std::function<void()>> beginningAdvices_;
std::vector<std::function<bool(const trantor::InetAddress &,
const trantor::InetAddress &)>>

View File

@ -1,7 +1,7 @@
/**
*
* @file HttpServer.cc
* An Tao
* @author An Tao
*
* Copyright 2018, An Tao. All rights reserved.
* https://github.com/an-tao/drogon
@ -158,7 +158,7 @@ HttpServer::HttpServer(
#ifdef __linux__
: server_(loop, listenAddr, name.c_str()),
#else
: server_(loop, listenAddr, name.c_str(), true, false),
: server_(loop, listenAddr, name.c_str(), true, app().reusePort()),
#endif
httpAsyncCallback_(defaultHttpAsyncCallback),
newWebsocketCallback_(defaultWebSockAsyncCallback),

View File

@ -91,7 +91,7 @@ std::vector<trantor::EventLoop *> ListenerManager::createListeners(
auto const &ip = listener.ip_;
bool isIpv6 = ip.find(':') == std::string::npos ? false : true;
std::shared_ptr<HttpServer> serverPtr;
if (i == 0)
if (i == 0 && !app().reusePort())
{
DrogonFileLocker lock;
// Check whether the port is in use.