| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2018 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <grpc/support/port_platform.h> |
| 20 |
|
| 21 |
#include "src/core/tsi/ssl/session_cache/ssl_session_cache.h" |
| 22 |
|
| 23 |
#include <grpc/support/log.h> |
| 24 |
#include <grpc/support/string_util.h> |
| 25 |
|
| 26 |
#include "src/core/lib/gprpp/sync.h" |
| 27 |
#include "src/core/lib/slice/slice_internal.h" |
| 28 |
#include "src/core/tsi/ssl/session_cache/ssl_session.h" |
| 29 |
|
| 30 |
namespace tsi { |
| 31 |
|
| 32 |
/// Node for single cached session. |
| 33 |
class SslSessionLRUCache::Node { |
| 34 |
public: |
| 35 |
Node(const std::string& key, SslSessionPtr session) : key_(key) { |
| 36 |
SetSession(std::move(session)); |
| 37 |
} |
| 38 |
|
| 39 |
// Not copyable nor movable. |
| 40 |
Node(const Node&) = delete; |
| 41 |
Node& operator=(const Node&) = delete; |
| 42 |
|
| 43 |
const std::string& key() const { return key_; } |
| 44 |
|
| 45 |
/// Returns a copy of the node's cache session. |
| 46 |
SslSessionPtr CopySession() const { return session_->CopySession(); } |
| 47 |
|
| 48 |
/// Set the \a session (which is moved) for the node. |
| 49 |
void SetSession(SslSessionPtr session) { |
| 50 |
session_ = SslCachedSession::Create(std::move(session)); |
| 51 |
} |
| 52 |
|
| 53 |
private: |
| 54 |
friend class SslSessionLRUCache; |
| 55 |
|
| 56 |
std::string key_; |
| 57 |
std::unique_ptr<SslCachedSession> session_; |
| 58 |
|
| 59 |
Node* next_ = nullptr; |
| 60 |
Node* prev_ = nullptr; |
| 61 |
}; |
| 62 |
|
| 63 |
SslSessionLRUCache::SslSessionLRUCache(size_t capacity) : capacity_(capacity) { |
| 64 |
GPR_ASSERT(capacity > 0); |
| 65 |
} |
| 66 |
|
| 67 |
SslSessionLRUCache::~SslSessionLRUCache() { |
| 68 |
Node* node = use_order_list_head_; |
| 69 |
while (node) { |
| 70 |
Node* next = node->next_; |
| 71 |
delete node; |
| 72 |
node = next; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
size_t SslSessionLRUCache::Size() { |
| 77 |
grpc_core::MutexLock lock(&lock_); |
| 78 |
return use_order_list_size_; |
| 79 |
} |
| 80 |
|
| 81 |
SslSessionLRUCache::Node* SslSessionLRUCache::FindLocked( |
| 82 |
const std::string& key) { |
| 83 |
auto it = entry_by_key_.find(key); |
| 84 |
if (it == entry_by_key_.end()) { |
| 85 |
return nullptr; |
| 86 |
} |
| 87 |
Node* node = it->second; |
| 88 |
// Move to the beginning. |
| 89 |
Remove(node); |
| 90 |
PushFront(node); |
| 91 |
AssertInvariants(); |
| 92 |
return node; |
| 93 |
} |
| 94 |
|
| 95 |
void SslSessionLRUCache::Put(const char* key, SslSessionPtr session) { |
| 96 |
grpc_core::MutexLock lock(&lock_); |
| 97 |
Node* node = FindLocked(key); |
| 98 |
if (node != nullptr) { |
| 99 |
node->SetSession(std::move(session)); |
| 100 |
return; |
| 101 |
} |
| 102 |
node = new Node(key, std::move(session)); |
| 103 |
PushFront(node); |
| 104 |
entry_by_key_.emplace(key, node); |
| 105 |
AssertInvariants(); |
| 106 |
if (use_order_list_size_ > capacity_) { |
| 107 |
GPR_ASSERT(use_order_list_tail_); |
| 108 |
node = use_order_list_tail_; |
| 109 |
Remove(node); |
| 110 |
// Order matters, key is destroyed after deleting node. |
| 111 |
entry_by_key_.erase(node->key()); |
| 112 |
delete node; |
| 113 |
AssertInvariants(); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
SslSessionPtr SslSessionLRUCache::Get(const char* key) { |
| 118 |
grpc_core::MutexLock lock(&lock_); |
| 119 |
// Key is only used for lookups. |
| 120 |
Node* node = FindLocked(key); |
| 121 |
if (node == nullptr) { |
| 122 |
return nullptr; |
| 123 |
} |
| 124 |
return node->CopySession(); |
| 125 |
} |
| 126 |
|
| 127 |
void SslSessionLRUCache::Remove(SslSessionLRUCache::Node* node) { |
| 128 |
if (node->prev_ == nullptr) { |
| 129 |
use_order_list_head_ = node->next_; |
| 130 |
} else { |
| 131 |
node->prev_->next_ = node->next_; |
| 132 |
} |
| 133 |
if (node->next_ == nullptr) { |
| 134 |
use_order_list_tail_ = node->prev_; |
| 135 |
} else { |
| 136 |
node->next_->prev_ = node->prev_; |
| 137 |
} |
| 138 |
GPR_ASSERT(use_order_list_size_ >= 1); |
| 139 |
use_order_list_size_--; |
| 140 |
} |
| 141 |
|
| 142 |
void SslSessionLRUCache::PushFront(SslSessionLRUCache::Node* node) { |
| 143 |
if (use_order_list_head_ == nullptr) { |
| 144 |
use_order_list_head_ = node; |
| 145 |
use_order_list_tail_ = node; |
| 146 |
node->next_ = nullptr; |
| 147 |
node->prev_ = nullptr; |
| 148 |
} else { |
| 149 |
node->next_ = use_order_list_head_; |
| 150 |
node->next_->prev_ = node; |
| 151 |
use_order_list_head_ = node; |
| 152 |
node->prev_ = nullptr; |
| 153 |
} |
| 154 |
use_order_list_size_++; |
| 155 |
} |
| 156 |
|
| 157 |
#ifndef NDEBUG |
| 158 |
void SslSessionLRUCache::AssertInvariants() { |
| 159 |
size_t size = 0; |
| 160 |
Node* prev = nullptr; |
| 161 |
Node* current = use_order_list_head_; |
| 162 |
while (current != nullptr) { |
| 163 |
size++; |
| 164 |
GPR_ASSERT(current->prev_ == prev); |
| 165 |
auto it = entry_by_key_.find(current->key()); |
| 166 |
GPR_ASSERT(it != entry_by_key_.end()); |
| 167 |
GPR_ASSERT(it->second == current); |
| 168 |
prev = current; |
| 169 |
current = current->next_; |
| 170 |
} |
| 171 |
GPR_ASSERT(prev == use_order_list_tail_); |
| 172 |
GPR_ASSERT(size == use_order_list_size_); |
| 173 |
GPR_ASSERT(entry_by_key_.size() == use_order_list_size_); |
| 174 |
} |
| 175 |
#else |
| 176 |
void SslSessionLRUCache::AssertInvariants() {} |
| 177 |
#endif |
| 178 |
|
| 179 |
} // namespace tsi |