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 #ifndef GRPC_SRC_CORE_TSI_SSL_SESSION_CACHE_SSL_SESSION_CACHE_H 20 #define GRPC_SRC_CORE_TSI_SSL_SESSION_CACHE_SSL_SESSION_CACHE_H 21 22 #include <grpc/impl/grpc_types.h> 23 #include <grpc/slice.h> 24 #include <grpc/support/port_platform.h> 25 #include <grpc/support/sync.h> 26 #include <openssl/ssl.h> 27 28 #include <map> 29 30 #include "src/core/tsi/ssl/session_cache/ssl_session.h" 31 #include "src/core/util/cpp_impl_of.h" 32 #include "src/core/util/memory.h" 33 #include "src/core/util/ref_counted.h" 34 #include "src/core/util/sync.h" 35 36 /// Cache for SSL sessions for sessions resumption. 37 /// 38 /// Older sessions may be evicted from the cache using LRU policy if capacity 39 /// limit is hit. All sessions are associated with some key, usually server 40 /// name. Note that servers are required to share session ticket encryption keys 41 /// in order for cache to be effective. 42 /// 43 /// This class is thread safe. 44 45 namespace tsi { 46 47 class SslSessionLRUCache 48 : public grpc_core::CppImplOf<SslSessionLRUCache, 49 struct tsi_ssl_session_cache>, 50 public grpc_core::RefCounted<SslSessionLRUCache> { 51 public: 52 /// Create new LRU cache with the given capacity. Create(size_t capacity)53 static grpc_core::RefCountedPtr<SslSessionLRUCache> Create(size_t capacity) { 54 return grpc_core::MakeRefCounted<SslSessionLRUCache>(capacity); 55 } 56 57 // Use Create function instead of using this directly. 58 explicit SslSessionLRUCache(size_t capacity); 59 ~SslSessionLRUCache() override; 60 61 // Not copyable nor movable. 62 SslSessionLRUCache(const SslSessionLRUCache&) = delete; 63 SslSessionLRUCache& operator=(const SslSessionLRUCache&) = delete; 64 ChannelArgName()65 static absl::string_view ChannelArgName() { 66 return GRPC_SSL_SESSION_CACHE_ARG; 67 } 68 69 /// Returns current number of sessions in the cache. 70 size_t Size(); 71 /// Add \a session in the cache using \a key. This operation may discard older 72 /// sessions. 73 void Put(const char* key, SslSessionPtr session); 74 /// Returns the session from the cache associated with \a key or null if not 75 /// found. 76 SslSessionPtr Get(const char* key); 77 78 private: 79 class Node; 80 81 Node* FindLocked(const std::string& key); 82 void Remove(Node* node); 83 void PushFront(Node* node); 84 void AssertInvariants(); 85 86 grpc_core::Mutex lock_; 87 size_t capacity_; 88 89 Node* use_order_list_head_ = nullptr; 90 Node* use_order_list_tail_ = nullptr; 91 size_t use_order_list_size_ = 0; 92 std::map<std::string, Node*> entry_by_key_; 93 }; 94 95 } // namespace tsi 96 97 #endif // GRPC_SRC_CORE_TSI_SSL_SESSION_CACHE_SSL_SESSION_CACHE_H 98