• 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/tsi/ssl/session_cache/ssl_session.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 : public grpc_core::RefCounted<SslSessionLRUCache> {
48  public:
49   /// Create new LRU cache with the given capacity.
Create(size_t capacity)50   static grpc_core::RefCountedPtr<SslSessionLRUCache> Create(size_t capacity) {
51     return grpc_core::MakeRefCounted<SslSessionLRUCache>(capacity);
52   }
53 
54   // Use Create function instead of using this directly.
55   explicit SslSessionLRUCache(size_t capacity);
56   ~SslSessionLRUCache();
57 
58   // Not copyable nor movable.
59   SslSessionLRUCache(const SslSessionLRUCache&) = delete;
60   SslSessionLRUCache& operator=(const SslSessionLRUCache&) = delete;
61 
62   /// Returns current number of sessions in the cache.
63   size_t Size();
64   /// Add \a session in the cache using \a key. This operation may discard older
65   /// sessions.
66   void Put(const char* key, SslSessionPtr session);
67   /// Returns the session from the cache associated with \a key or null if not
68   /// found.
69   SslSessionPtr Get(const char* key);
70 
71  private:
72   class Node;
73 
74   Node* FindLocked(const grpc_slice& key);
75   void Remove(Node* node);
76   void PushFront(Node* node);
77   void AssertInvariants();
78 
79   gpr_mu lock_;
80   size_t capacity_;
81 
82   Node* use_order_list_head_ = nullptr;
83   Node* use_order_list_tail_ = nullptr;
84   size_t use_order_list_size_ = 0;
85   grpc_avl entry_by_key_;
86 };
87 
88 }  // namespace tsi
89 
90 #endif /* GRPC_CORE_TSI_SSL_SESSION_CACHE_SSL_SESSION_CACHE_H */
91