proxygen
wangle::ShardedLocalSSLSessionCache Class Reference

#include <SSLSessionCacheManager.h>

Inheritance diagram for wangle::ShardedLocalSSLSessionCache:

Public Member Functions

 ShardedLocalSSLSessionCache (uint32_t n_buckets, uint32_t maxCacheSize, uint32_t cacheCullSize)
 
SSL_SESSION * lookupSession (const std::string &sessionId)
 
void storeSession (const std::string &sessionId, SSL_SESSION *session, SSLStats *stats)
 
void removeSession (const std::string &sessionId)
 
size_t hash (const std::string &key)
 

Public Attributes

std::vector< std::unique_ptr< LocalSSLSessionCache > > caches_
 

Detailed Description

A sharded LRU for SSL sessions. The sharding is inteneded to reduce contention for the LRU locks. Assuming uniform distribution, two workers will contend for the same lock with probability 1 / n_buckets^2.

Definition at line 63 of file SSLSessionCacheManager.h.

Constructor & Destructor Documentation

wangle::ShardedLocalSSLSessionCache::ShardedLocalSSLSessionCache ( uint32_t  n_buckets,
uint32_t  maxCacheSize,
uint32_t  cacheCullSize 
)

Definition at line 73 of file SSLSessionCacheManager.cpp.

References i, wangle::LocalSSLSessionCache::LocalSSLSessionCache(), and uint32_t.

76  {
77  CHECK(n_buckets > 0);
78  maxCacheSize = (uint32_t)(((double)maxCacheSize) / n_buckets);
79  cacheCullSize = (uint32_t)(((double)cacheCullSize) / n_buckets);
80  if (maxCacheSize == 0) {
81  maxCacheSize = 1;
82  }
83  if (cacheCullSize == 0) {
84  cacheCullSize = 1;
85  }
86  for (uint32_t i = 0; i < n_buckets; i++) {
87  caches_.push_back(std::unique_ptr<LocalSSLSessionCache>(
88  new LocalSSLSessionCache(maxCacheSize, cacheCullSize)));
89  }
90 }
std::vector< std::unique_ptr< LocalSSLSessionCache > > caches_

Member Function Documentation

size_t wangle::ShardedLocalSSLSessionCache::hash ( const std::string key)
inline

Definition at line 75 of file SSLSessionCacheManager.h.

Referenced by TEST().

75  {
76  return folly::Hash()(key) % caches_.size();
77  }
std::vector< std::unique_ptr< LocalSSLSessionCache > > caches_
SSL_SESSION * wangle::ShardedLocalSSLSessionCache::lookupSession ( const std::string sessionId)

Definition at line 92 of file SSLSessionCacheManager.cpp.

References folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::end(), g(), wangle::LocalSSLSessionCache::lock, and wangle::LocalSSLSessionCache::sessionCache.

93  {
94  size_t bucket = hash(sessionId);
95  SSL_SESSION* session = nullptr;
96  std::lock_guard<std::mutex> g(caches_[bucket]->lock);
97 
98  auto itr = caches_[bucket]->sessionCache.find(sessionId);
99  if (itr != caches_[bucket]->sessionCache.end()) {
100  session = itr->second;
101  }
102 
103  if (session) {
104  SSL_SESSION_up_ref(session);
105  }
106  return session;
107 }
std::vector< std::unique_ptr< LocalSSLSessionCache > > caches_
auto lock(SynchronizedLocker...lockersIn) -> std::tuple< typename SynchronizedLocker::LockedPtr... >
Definition: Synchronized.h:871
g_t g(f_t)
size_t hash(const std::string &key)
void wangle::ShardedLocalSSLSessionCache::removeSession ( const std::string sessionId)

Definition at line 134 of file SSLSessionCacheManager.cpp.

References g(), and wangle::LocalSSLSessionCache::lock.

134  {
135  size_t bucket = hash(sessionId);
136  std::lock_guard<std::mutex> g(caches_[bucket]->lock);
137  caches_[bucket]->sessionCache.erase(sessionId);
138 }
std::vector< std::unique_ptr< LocalSSLSessionCache > > caches_
auto lock(SynchronizedLocker...lockersIn) -> std::tuple< typename SynchronizedLocker::LockedPtr... >
Definition: Synchronized.h:871
g_t g(f_t)
size_t hash(const std::string &key)
void wangle::ShardedLocalSSLSessionCache::storeSession ( const std::string sessionId,
SSL_SESSION *  session,
SSLStats stats 
)

Definition at line 109 of file SSLSessionCacheManager.cpp.

References folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::end(), g(), wangle::LocalSSLSessionCache::lock, wangle::SSLStats::recordSSLSessionFree(), wangle::LocalSSLSessionCache::removedSessions_, and wangle::LocalSSLSessionCache::sessionCache.

112  {
113  size_t bucket = hash(sessionId);
114  SSL_SESSION* oldSession = nullptr;
115  std::lock_guard<std::mutex> g(caches_[bucket]->lock);
116 
117  auto itr = caches_[bucket]->sessionCache.find(sessionId);
118  if (itr != caches_[bucket]->sessionCache.end()) {
119  oldSession = itr->second;
120  }
121 
122  if (oldSession) {
123  // LRUCacheMap doesn't free on overwrite, so 2x the work for us
124  // This can happen in race conditions
125  SSL_SESSION_free(oldSession);
126  }
127  caches_[bucket]->removedSessions_ = 0;
128  caches_[bucket]->sessionCache.set(sessionId, session, true);
129  if (stats) {
130  stats->recordSSLSessionFree(caches_[bucket]->removedSessions_);
131  }
132 }
std::vector< std::unique_ptr< LocalSSLSessionCache > > caches_
auto lock(SynchronizedLocker...lockersIn) -> std::tuple< typename SynchronizedLocker::LockedPtr... >
Definition: Synchronized.h:871
g_t g(f_t)
size_t hash(const std::string &key)

Member Data Documentation

std::vector< std::unique_ptr<LocalSSLSessionCache> > wangle::ShardedLocalSSLSessionCache::caches_

Definition at line 79 of file SSLSessionCacheManager.h.


The documentation for this class was generated from the following files: