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