proxygen
SSLSessionPersistentCache.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <folly/Executor.h>
19 #include <folly/Memory.h>
24 
25 #include <atomic>
26 #include <chrono>
27 #include <map>
28 
29 #include <openssl/ssl.h>
30 
31 namespace wangle {
32 
37 template<typename K>
39 
40  public:
41  class TimeUtil {
42  public:
43  virtual ~TimeUtil() {}
44 
45  virtual std::chrono::time_point<std::chrono::system_clock> now() const {
47  }
48  };
49 
51  std::shared_ptr<PersistentCache<K, SSLSessionCacheData>> cache);
52 
54  std::shared_ptr<folly::Executor> executor,
55  const std::string& filename,
56  std::size_t cacheCapacity,
57  std::chrono::seconds syncInterval);
58 
60  const std::string& filename,
61  std::size_t cacheCapacity,
62  std::chrono::seconds syncInterval);
63 
64  // Store the session data of the specified identity in cache. Note that the
65  // implementation must make it's own memory copy of the session data to put
66  // into the cache.
67  void setSSLSession(
68  const std::string& identity, SSLSessionPtr session) noexcept override;
69 
70  // Return a SSL session if the cache contained session information for the
71  // specified identity. It is the caller's responsibility to decrement the
72  // reference count of the returned session pointer.
74  const std::string& identity) const noexcept override;
75 
76  // Remove session data of the specified identity from cache. Return true if
77  // there was session data associated with the identity before removal, or
78  // false otherwise.
79  bool removeSSLSession(const std::string& identity) noexcept override;
80 
81  // Return true if the underlying cache supports persistence
82  bool supportsPersistence() const noexcept override {
83  return true;
84  }
85 
86  void setTimeUtil(std::unique_ptr<TimeUtil> timeUtil) noexcept {
87  timeUtil_ = std::move(timeUtil);
88  }
89 
90  // For test only, returns the number of entries in the cache.
91  size_t size() const override;
92 
93  protected:
94  // Get the persistence key from the session's identity
95  virtual K getKey(const std::string& identity) const = 0;
96 
97  std::shared_ptr<PersistentCache<K, SSLSessionCacheData>>
99  std::unique_ptr<TimeUtil> timeUtil_;
100 };
101 
103  public SSLSessionPersistentCacheBase<std::string> {
104  public:
106  std::shared_ptr<folly::Executor> executor,
107  const std::string& filename,
108  std::size_t cacheCapacity,
109  std::chrono::seconds syncInterval)
111  std::move(executor),
112  filename,
113  cacheCapacity,
114  syncInterval) {}
115 
117  const std::string& filename,
118  std::size_t cacheCapacity,
119  std::chrono::seconds syncInterval)
120  : SSLSessionPersistentCacheBase(filename, cacheCapacity, syncInterval) {}
121 
122  protected:
123  std::string getKey(const std::string& identity) const override {
124  return identity;
125  }
126 
127 };
128 }
129 
bool supportsPersistence() const noexceptoverride
SSLSessionPersistentCache(const std::string &filename, std::size_t cacheCapacity, std::chrono::seconds syncInterval)
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::chrono::steady_clock::time_point now()
STL namespace.
virtual std::chrono::time_point< std::chrono::system_clock > now() const
void setTimeUtil(std::unique_ptr< TimeUtil > timeUtil) noexcept
requires E e noexcept(noexcept(s.error(std::move(e))))
PUSHMI_INLINE_VAR constexpr __adl::get_executor_fn executor
void setSSLSession(const std::string &identity, SSLSessionPtr session) noexceptoverride
std::shared_ptr< PersistentCache< K, SSLSessionCacheData > > persistentCache_
std::unique_ptr< SSL_SESSION, SessionDestructor > SSLSessionPtr
Definition: SSLSession.h:32
bool removeSSLSession(const std::string &identity) noexceptoverride
std::string getKey(const std::string &identity) const override
SSLSessionPtr getSSLSession(const std::string &identity) const noexceptoverride
const char * string
Definition: Conv.cpp:212
const
Definition: upload.py:398
virtual K getKey(const std::string &identity) const =0
SSLSessionPersistentCache(std::shared_ptr< folly::Executor > executor, const std::string &filename, std::size_t cacheCapacity, std::chrono::seconds syncInterval)
SSLSessionPersistentCacheBase(std::shared_ptr< PersistentCache< K, SSLSessionCacheData >> cache)