Skip to content

Commit 417c352

Browse files
Feature: TcpServer hot reload SSL file (#355)
1 parent 65f2455 commit 417c352

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

trantor/net/TcpServer.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,23 @@ void TcpServer::enableSSL(
234234
.setValidate(caPath.empty() ? false : true);
235235
sslContextPtr_ = newSSLContext(*policyPtr_, true);
236236
}
237+
238+
void TcpServer::reloadSSL()
239+
{
240+
if (loop_->isInLoopThread())
241+
{
242+
if (policyPtr_)
243+
{
244+
sslContextPtr_ = newSSLContext(*policyPtr_, true);
245+
}
246+
}
247+
else
248+
{
249+
loop_->queueInLoop([this]() {
250+
if (policyPtr_)
251+
{
252+
sslContextPtr_ = newSSLContext(*policyPtr_, true);
253+
}
254+
});
255+
}
256+
}

trantor/net/TcpServer.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,14 @@ class TRANTOR_EXPORT TcpServer : NonCopyable
260260
sslContextPtr_ = newSSLContext(*policyPtr_, true);
261261
}
262262

263+
/**
264+
* @brief Reload the SSL context.
265+
* @note Call this function when the certificate or private key is updated.
266+
* The server will reload the SSL context and use the new certificate and
267+
* private key. new connections will use the new SSL context.
268+
*/
269+
void reloadSSL();
270+
263271
private:
264272
void handleCloseInLoop(const TcpConnectionPtr &connectionPtr);
265273
void newConnection(int fd, const InetAddress &peer);

0 commit comments

Comments
 (0)