XRootD
XrdClHttpOptionsCache.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* Copyright (C) 2025, Pelican Project, Morgridge Institute for Research */
3 /* */
4 /* This file is part of the XrdClHttp client plugin for XRootD. */
5 /* */
6 /* XRootD is free software: you can redistribute it and/or modify it under */
7 /* the terms of the GNU Lesser General Public License as published by the */
8 /* Free Software Foundation, either version 3 of the License, or (at your */
9 /* option) any later version. */
10 /* */
11 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
12 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
13 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
14 /* License for more details. */
15 /* */
16 /* The copyright holder's institutional names and contributor's names may not */
17 /* be used to endorse or promote products derived from this software without */
18 /* specific prior written permission of the institution or contributor. */
19 /******************************************************************************/
20 
21 #include "XrdClHttpOptionsCache.hh"
22 
23 #include <curl/curl.h>
24 
25 #include <thread>
26 
27 XrdClHttp::VerbsCache XrdClHttp::VerbsCache::g_cache;
28 std::once_flag XrdClHttp::VerbsCache::m_expiry_launch;
29 std::mutex XrdClHttp::VerbsCache::m_shutdown_lock;
30 std::condition_variable XrdClHttp::VerbsCache::m_shutdown_requested_cv;
31 bool XrdClHttp::VerbsCache::m_shutdown_requested = false;
32 std::thread XrdClHttp::VerbsCache::m_expire_tid;
33 
34 // shutdown trigger, must be last of the static members
35 XrdClHttp::VerbsCache::shutdown_s XrdClHttp::VerbsCache::m_shutdowns;
36 
38  std::unique_lock lk(m_shutdown_lock);
39  if (!m_shutdown_requested) {
40  std::call_once(m_expiry_launch, [] {
41  std::thread t(VerbsCache::ExpireThread);
42  m_expire_tid = std::move(t);
43  });
44  }
45  return g_cache;
46 }
47 
48 void XrdClHttp::VerbsCache::ExpireThread()
49 {
50  while (true) {
51  {
52  std::unique_lock lock(m_shutdown_lock);
53  m_shutdown_requested_cv.wait_for(
54  lock,
55  std::chrono::seconds(30),
56  []{return m_shutdown_requested;}
57  );
58  if (m_shutdown_requested) {
59  break;
60  }
61  }
62  auto now = std::chrono::steady_clock::now();
63  g_cache.Expire(now);
64  }
65 }
66 
67 void XrdClHttp::VerbsCache::Expire(std::chrono::steady_clock::time_point now)
68 {
69  std::unique_lock lock(m_mutex);
70  for (auto iter = m_verbs_map.begin(); iter != m_verbs_map.end();) {
71  if (iter->second.m_expiry < now) {
72  iter = m_verbs_map.erase(iter);
73  } else {
74  ++iter;
75  }
76  }
77 }
78 
79 void
80 XrdClHttp::VerbsCache::Shutdown()
81 {
82  {
83  std::unique_lock lock(m_shutdown_lock);
84  m_shutdown_requested = true;
85  m_shutdown_requested_cv.notify_one();
86  }
87 
88  if (m_expire_tid.joinable()) {
89  m_expire_tid.join();
90  }
91 }
void Expire(std::chrono::steady_clock::time_point now)
static VerbsCache & Instance()