Debian Codegraph @

src/core/tsi/ssl/session_cache/ssl_session_cache.h

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