ssl_session_cache.cc
Go to the documentation of this file.
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 
20 
22 
23 #include <grpc/support/log.h>
25 
29 
30 namespace tsi {
31 
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 
46  SslSessionPtr CopySession() const { return session_->CopySession(); }
47 
49  void SetSession(SslSessionPtr session) {
51  }
52 
53  private:
54  friend class SslSessionLRUCache;
55 
57  std::unique_ptr<SslCachedSession> session_;
58 
59  Node* next_ = nullptr;
60  Node* prev_ = nullptr;
61 };
62 
64  GPR_ASSERT(capacity > 0);
65 }
66 
68  Node* node = use_order_list_head_;
69  while (node) {
70  Node* next = node->next_;
71  delete node;
72  node = next;
73  }
74 }
75 
78  return use_order_list_size_;
79 }
80 
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);
92  return node;
93 }
94 
95 void SslSessionLRUCache::Put(const char* key, SslSessionPtr session) {
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);
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;
114  }
115 }
116 
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 
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  }
140 }
141 
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  }
155 }
156 
157 #ifndef NDEBUG
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  }
174 }
175 #else
177 #endif
178 
179 } // namespace tsi
tsi::SslSessionLRUCache::PushFront
void PushFront(Node *node)
Definition: ssl_session_cache.cc:142
tsi::SslSessionLRUCache::use_order_list_head_
Node * use_order_list_head_
Definition: ssl_session_cache.h:82
tsi::SslSessionLRUCache::AssertInvariants
void AssertInvariants()
Definition: ssl_session_cache.cc:158
regen-readme.it
it
Definition: regen-readme.py:15
log.h
tsi::SslSessionLRUCache::Get
SslSessionPtr Get(const char *key)
Definition: ssl_session_cache.cc:117
opencensus.proto.agent.common.v1.common_pb2.Node
Node
Definition: common_pb2.py:308
tsi::SslSessionLRUCache
Definition: ssl_session_cache.h:47
capacity
uint16_t capacity
Definition: protobuf/src/google/protobuf/descriptor.cc:948
grpc_core::MutexLock
Definition: src/core/lib/gprpp/sync.h:88
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
tsi::SslSessionLRUCache::SslSessionLRUCache
SslSessionLRUCache(size_t capacity)
Definition: ssl_session_cache.cc:63
tsi::SslSessionLRUCache::entry_by_key_
std::map< std::string, Node * > entry_by_key_
Definition: ssl_session_cache.h:85
string_util.h
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
tsi::SslSessionPtr
std::unique_ptr< SSL_SESSION, SslSessionDeleter > SslSessionPtr
Definition: ssl_session.h:46
capacity_
uint32_t capacity_
Definition: abseil-cpp/absl/synchronization/internal/graphcycles.cc:132
tsi::SslSessionLRUCache::capacity_
size_t capacity_
Definition: ssl_session_cache.h:80
tsi::SslSessionLRUCache::Node::prev_
Node * prev_
Definition: ssl_session_cache.cc:60
ssl_session_cache.h
tsi::SslSessionLRUCache::Node::key_
std::string key_
Definition: ssl_session_cache.cc:56
slice_internal.h
tsi::SslSessionLRUCache::Node::key
const std::string & key() const
Definition: ssl_session_cache.cc:43
tsi::SslSessionLRUCache::Remove
void Remove(Node *node)
Definition: ssl_session_cache.cc:127
tsi::SslSessionLRUCache::~SslSessionLRUCache
~SslSessionLRUCache() override
Definition: ssl_session_cache.cc:67
tsi::SslSessionLRUCache::lock_
grpc_core::Mutex lock_
Definition: ssl_session_cache.h:79
tsi::SslSessionLRUCache::Node
Node for single cached session.
Definition: ssl_session_cache.cc:33
tsi::SslSessionLRUCache::Node::operator=
Node & operator=(const Node &)=delete
tsi::SslSessionLRUCache::Put
void Put(const char *key, SslSessionPtr session)
Definition: ssl_session_cache.cc:95
key
const char * key
Definition: hpack_parser_table.cc:164
tsi::SslSessionLRUCache::Node::session_
std::unique_ptr< SslCachedSession > session_
Definition: ssl_session_cache.cc:57
tsi
Definition: ssl_key_logging.cc:29
tsi::SslSessionLRUCache::Node::SetSession
void SetSession(SslSessionPtr session)
Set the session (which is moved) for the node.
Definition: ssl_session_cache.cc:49
next
AllocList * next[kMaxLevel]
Definition: abseil-cpp/absl/base/internal/low_level_alloc.cc:100
tsi::SslSessionLRUCache::use_order_list_size_
size_t use_order_list_size_
Definition: ssl_session_cache.h:84
tsi::SslSessionLRUCache::FindLocked
Node * FindLocked(const std::string &key)
Definition: ssl_session_cache.cc:81
ssl_session.h
tsi::SslSessionLRUCache::Node::Node
Node(const std::string &key, SslSessionPtr session)
Definition: ssl_session_cache.cc:35
tsi::SslSessionLRUCache::Node::next_
Node * next_
Definition: ssl_session_cache.cc:59
tsi::SslSessionLRUCache::use_order_list_tail_
Node * use_order_list_tail_
Definition: ssl_session_cache.h:83
tsi::SslSessionLRUCache::Node::CopySession
SslSessionPtr CopySession() const
Returns a copy of the node's cache session.
Definition: ssl_session_cache.cc:46
tsi::SslSessionLRUCache::Size
size_t Size()
Returns current number of sessions in the cache.
Definition: ssl_session_cache.cc:76
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
sync.h
tsi::SslCachedSession::Create
static std::unique_ptr< SslCachedSession > Create(SslSessionPtr session)
Create single cached instance of session.
Definition: ssl_session_openssl.cc:70
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:21