Skip to content

Commit

Permalink
Add httpcl::Settings::updateTimestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
josephbirkner committed Jul 15, 2024
1 parent 2dd3b75 commit bd5f1f2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
9 changes: 9 additions & 0 deletions libs/httpcl/include/httpcl/http-settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ struct Settings
*/
std::vector<Config> settings;
YAML::Node document;
mutable std::shared_mutex mutex;
std::chrono::steady_clock::time_point lastRead;

/**
* Prompt settings instance to re-parse the HTTP settings file,
* by calling updateTimestamp with std::chrono::steady_clock::now().
*/
static void updateTimestamp(std::chrono::steady_clock::time_point time);
static std::atomic<std::chrono::steady_clock::time_point> lastUpdated;
};

struct secret
Expand Down
17 changes: 15 additions & 2 deletions libs/httpcl/src/http-settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,16 @@ Settings::Settings()
load();
}

std::atomic<std::chrono::steady_clock::time_point> Settings::lastUpdated{std::chrono::steady_clock::now()};

void Settings::updateTimestamp(std::chrono::steady_clock::time_point time) {
lastUpdated.store(time, std::memory_order_relaxed);
}

void Settings::load()
{
std::unique_lock lock(mutex);
lastRead = std::chrono::steady_clock::now();
settings.clear();

auto cookieJar = std::getenv("HTTP_SETTINGS_FILE");
Expand Down Expand Up @@ -412,15 +420,20 @@ Config::Config(const std::string& yamlConf)

Config Settings::operator[] (const std::string &url) const
{
Config result;
std::shared_lock lock(mutex);
if (lastRead < lastUpdated.load(std::memory_order_relaxed)) {
lock.unlock();
const_cast<Settings*>(this)->load();
lock.lock();
}

Config result;
for (auto const& config : settings)
{
if (!std::regex_match(url, config.urlPattern))
continue;
result |= config;
}

return result;
}

Expand Down

0 comments on commit bd5f1f2

Please sign in to comment.