Skip to content

Commit 9dae7b7

Browse files
committed
Reduce TLS handshake contention on SSLCertContext
Replace std::mutex with ts::bravo::shared_mutex on SSLCertContext to allow true reader concurrency for getCtx() on the TLS handshake hot path. setCtx() (config reload only) takes an exclusive lock. Memory trade-off: BRAVO uses 256 cache-line-aligned reader slots (~16 KB per mutex) vs ~40 bytes for std::mutex or ~56 bytes for std::shared_mutex on Linux. For 256 certificates this is ~4 MB (vs 10 KB / 14 KB), a modest cost relative to the SSL_CTX objects themselves but worth noting for deployments with many certs.
1 parent 8e6b509 commit 9dae7b7

2 files changed

Lines changed: 8 additions & 7 deletions

File tree

src/iocore/net/P_SSLCertLookup.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
#include "iocore/eventsystem/ConfigProcessor.h"
2727
#include "iocore/net/SSLTypes.h"
2828
#include "records/RecCore.h"
29+
#include "tsutil/Bravo.h"
2930

3031
#include <set>
3132
#include <openssl/ssl.h>
32-
#include <mutex>
3333
#include <unordered_map>
3434
#include <utility>
3535

@@ -94,8 +94,8 @@ using shared_ssl_ticket_key_block = std::shared_ptr<ssl_ticket_key_block>;
9494
*/
9595
struct SSLCertContext {
9696
private:
97-
mutable std::mutex ctx_mutex;
98-
shared_SSL_CTX ctx;
97+
mutable ts::bravo::shared_mutex ctx_mutex;
98+
shared_SSL_CTX ctx;
9999

100100
public:
101101
SSLCertContext() : ctx_mutex(), ctx(nullptr), opt(SSLCertContextOption::OPT_NONE), userconfig(nullptr), keyblock(nullptr) {}

src/iocore/net/SSLCertLookup.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
#include "P_SSLUtils.h"
3535

36+
#include <mutex>
3637
#include <unordered_map>
3738
#include <utility>
3839
#include <vector>
@@ -237,7 +238,7 @@ SSLCertContext::SSLCertContext(SSLCertContext const &other)
237238
userconfig = other.userconfig;
238239
keyblock = other.keyblock;
239240
ctx_type = other.ctx_type;
240-
std::lock_guard<std::mutex> lock(other.ctx_mutex);
241+
ts::bravo::shared_lock lock(other.ctx_mutex);
241242
ctx = other.ctx;
242243
}
243244

@@ -249,7 +250,7 @@ SSLCertContext::operator=(SSLCertContext const &other)
249250
this->userconfig = other.userconfig;
250251
this->keyblock = other.keyblock;
251252
this->ctx_type = other.ctx_type;
252-
std::lock_guard<std::mutex> lock(other.ctx_mutex);
253+
ts::bravo::shared_lock lock(other.ctx_mutex);
253254
this->ctx = other.ctx;
254255
}
255256
return *this;
@@ -258,14 +259,14 @@ SSLCertContext::operator=(SSLCertContext const &other)
258259
shared_SSL_CTX
259260
SSLCertContext::getCtx()
260261
{
261-
std::lock_guard<std::mutex> lock(ctx_mutex);
262+
ts::bravo::shared_lock lock(ctx_mutex);
262263
return ctx;
263264
}
264265

265266
void
266267
SSLCertContext::setCtx(shared_SSL_CTX sc)
267268
{
268-
std::lock_guard<std::mutex> lock(ctx_mutex);
269+
std::lock_guard lock(ctx_mutex);
269270
ctx = std::move(sc);
270271
}
271272

0 commit comments

Comments
 (0)