Skip to content

Feature: TcpServer hot reload SSL file #355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions trantor/net/TcpServer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,23 @@ void TcpServer::enableSSL(
.setValidate(caPath.empty() ? false : true);
sslContextPtr_ = newSSLContext(*policyPtr_, true);
}

void TcpServer::reloadSSL()
{
if (loop_->isInLoopThread())
{
if (policyPtr_)
{
sslContextPtr_ = newSSLContext(*policyPtr_, true);
}
}
else
{
loop_->queueInLoop([this]() {
if (policyPtr_)
{
sslContextPtr_ = newSSLContext(*policyPtr_, true);
}
});
}
}
8 changes: 8 additions & 0 deletions trantor/net/TcpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,14 @@ class TRANTOR_EXPORT TcpServer : NonCopyable
sslContextPtr_ = newSSLContext(*policyPtr_, true);
}

/**
* @brief Reload the SSL context.
* @note Call this function when the certificate or private key is updated.
* The server will reload the SSL context and use the new certificate and
* private key. new connections will use the new SSL context.
*/
void reloadSSL();

private:
void handleCloseInLoop(const TcpConnectionPtr &connectionPtr);
void newConnection(int fd, const InetAddress &peer);
Expand Down