sapling/eden/fs/config/ReloadableConfig.cpp
Adam Simpkins 51641fffc5 turn ReloadableConfig into an implementation class
Summary:
Move the `ReloadableConfig` implementation out of `ServerState` and into the
`ReloadableConfig` class itself.  This also updates `ServerState` to simply
contain a `ReloadableConfig` member variable rather than inheriting from
`ReloadableConfig`.

This makes it easier to build other utilities that need a `ReloadableConfig`
object but do not have a full `ServerState` object, such as the
`eden_store_util` class and the `zero_blob` helper tool used by the
integration tests.

Reviewed By: chadaustin

Differential Revision: D15162813

fbshipit-source-id: 1ebc0ac6c475f4aa1090dd933ebce16bc222674c
2019-05-07 17:41:36 -07:00

63 lines
1.9 KiB
C++

/*
* Copyright (c) 2019-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#include "eden/fs/config/ReloadableConfig.h"
#include "eden/fs/config/EdenConfig.h"
namespace {
/** Throttle EdenConfig change checks, max of 1 per kEdenConfigMinPollSeconds */
constexpr std::chrono::seconds kEdenConfigMinPollSeconds{5};
} // namespace
namespace facebook {
namespace eden {
ReloadableConfig::ReloadableConfig(std::shared_ptr<const EdenConfig> config)
: state_{ConfigState{config}} {}
ReloadableConfig::~ReloadableConfig() {}
std::shared_ptr<const EdenConfig> ReloadableConfig::getEdenConfig(
bool skipUpdate) {
if (!skipUpdate) {
return getUpdatedEdenConfig();
}
return state_.rlock()->config;
}
// TODO: Update this monitoring code to use FileChangeMonitor.
std::shared_ptr<const EdenConfig> ReloadableConfig::getUpdatedEdenConfig() {
std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
// Throttle the updates
auto state = state_.wlock();
if ((now - state->lastCheck) < kEdenConfigMinPollSeconds) {
return state->config;
}
// Update the throttle setting - to prevent thrashing.
state->lastCheck = now;
bool userConfigChanged = state->config->hasUserConfigFileChanged();
bool systemConfigChanged = state->config->hasSystemConfigFileChanged();
if (userConfigChanged || systemConfigChanged) {
auto newConfig = std::make_shared<EdenConfig>(*state->config);
if (userConfigChanged) {
newConfig->loadUserConfig();
}
if (systemConfigChanged) {
newConfig->loadSystemConfig();
}
state->config = std::move(newConfig);
}
return state->config;
}
} // namespace eden
} // namespace facebook