summaryrefslogtreecommitdiffstats
path: root/dnsdist-session-cache.cc
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 09:34:30 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 09:34:30 +0000
commit4fc2f55f761d71aae1f145d5aa94ba929cc39676 (patch)
tree5c1e1db3b46dd4edbe11f612d93cb94b96891ce3 /dnsdist-session-cache.cc
parentInitial commit. (diff)
downloaddnsdist-upstream.tar.xz
dnsdist-upstream.zip
Adding upstream version 1.7.3.upstream/1.7.3upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dnsdist-session-cache.cc')
-rw-r--r--dnsdist-session-cache.cc90
1 files changed, 90 insertions, 0 deletions
diff --git a/dnsdist-session-cache.cc b/dnsdist-session-cache.cc
new file mode 100644
index 0000000..42ba272
--- /dev/null
+++ b/dnsdist-session-cache.cc
@@ -0,0 +1,90 @@
+/*
+ * This file is part of PowerDNS or dnsdist.
+ * Copyright -- PowerDNS.COM B.V. and its contributors
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * In addition, for the avoidance of any doubt, permission is granted to
+ * link this program with OpenSSL and to (re)distribute the binaries
+ * produced as the result of such linking.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#include "dnsdist-session-cache.hh"
+
+TLSSessionCache g_sessionCache;
+
+time_t TLSSessionCache::s_cleanupDelay{60};
+time_t TLSSessionCache::s_sessionValidity{600};
+uint16_t TLSSessionCache::s_maxSessionsPerBackend{20};
+
+void TLSSessionCache::cleanup(time_t now, LockGuardedHolder<TLSSessionCache::CacheData>& data)
+{
+ time_t cutOff = now + s_sessionValidity;
+
+ for (auto it = data->d_sessions.begin(); it != data->d_sessions.end();) {
+ if (it->second.d_lastUsed > cutOff || it->second.d_sessions.size() == 0) {
+ it = data->d_sessions.erase(it);
+ }
+ else {
+ ++it;
+ }
+ }
+
+ data->d_nextCleanup = now + s_cleanupDelay;
+}
+
+void TLSSessionCache::putSessions(const boost::uuids::uuid& backendID, time_t now, std::vector<std::unique_ptr<TLSSession>>&& sessions)
+{
+ auto data = d_data.lock();
+ if (data->d_nextCleanup == 0 || now > data->d_nextCleanup) {
+ cleanup(now, data);
+ }
+
+ for (auto& session : sessions) {
+ auto& entry = data->d_sessions[backendID];
+ if (entry.d_sessions.size() >= s_maxSessionsPerBackend) {
+ entry.d_sessions.pop_back();
+ }
+ entry.d_sessions.push_front(std::move(session));
+ }
+}
+
+std::unique_ptr<TLSSession> TLSSessionCache::getSession(const boost::uuids::uuid& backendID, time_t now)
+{
+ auto data = d_data.lock();
+ auto it = data->d_sessions.find(backendID);
+ if (it == data->d_sessions.end()) {
+ return nullptr;
+ }
+
+ auto& entry = it->second;
+ if (entry.d_sessions.size() == 0) {
+ return nullptr;
+ }
+
+ entry.d_lastUsed = now;
+ auto value = std::move(entry.d_sessions.front());
+ entry.d_sessions.pop_front();
+
+ return value;
+}
+
+size_t TLSSessionCache::getSize()
+{
+ size_t count = 0;
+ auto data = d_data.lock();
+ for (const auto& backend : data->d_sessions) {
+ count += backend.second.d_sessions.size();
+ }
+ return count;
+}