blob: a80ce54d774b7339e1e19961e5d4cbca0bb10b7a [file] [edit]
#ifndef THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_CONFIGS_RATE_LIMITER_CONFIG_H_
#define THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_CONFIGS_RATE_LIMITER_CONFIG_H_
#include <string>
#include <string_view>
#include "absl/base/no_destructor.h"
#include "absl/base/thread_annotations.h"
#include "absl/status/status.h"
#include "absl/synchronization/mutex.h"
#include "central_config.pb.h"
namespace milotic_tlbmc {
// Provides configuration for RedfishRateLimiterModule.
// It loads the base configuration from TlbmcConfig and merges it with
// an overlay configuration from overlay_path if it exists.
class RateLimiterConfig {
public:
// Initializes the config directory. Must be called before GetConfig() to use
// a custom directory. If not called, GetConfig() will use the default
// directory. Should only be called once during program initialization.
static void Init(std::string_view config_dir);
// Returns the current configuration.
static RedfishRateLimiterModule GetConfig();
// Merges the given patch data with the current configuration and saves it
// to the overlay file.
// If replace_bypass_uris is true, the bypass_uri list in the overlay will be
// replaced by the one in patch_data (if any), instead of appended.
// Note: While the configuration is updated, consumers of this configuration
// may need to be re-initialized to use the new values. For example, the
// crow::RateLimiter in BmcWebServer is initialized with the config but won't
// automatically pick up changes made via UpdateConfiguration.
static absl::Status UpdateConfiguration(
const RedfishRateLimiterModule& patch_data,
bool replace_bypass_uris = false);
// Resets the singleton instance for testing.
protected:
RateLimiterConfig();
static void Reset();
private:
// Returns singleton instance.
static RateLimiterConfig& Instance();
void InitImpl(std::string_view config_dir);
RedfishRateLimiterModule GetConfigImpl() const;
absl::Status UpdateConfigurationImpl(
const RedfishRateLimiterModule& patch_data, bool replace_bypass_uris);
void ResetImpl();
// Loads configuration from the given path.
// This function is not thread-safe and must be called with mutex_ held or
// during construction.
void LoadConfigLocked(std::string_view overlay_file_path)
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
static absl::Status SaveConfigToFile(const RedfishRateLimiterModule& config,
std::string_view overlay_file_path);
mutable absl::Mutex mutex_;
RedfishRateLimiterModule config_ ABSL_GUARDED_BY(mutex_);
RedfishRateLimiterModule overlay_config_ ABSL_GUARDED_BY(mutex_);
std::string overlay_file_path_ ABSL_GUARDED_BY(mutex_);
friend class absl::NoDestructor<RateLimiterConfig>;
};
} // namespace milotic_tlbmc
#endif // THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_CONFIGS_RATE_LIMITER_CONFIG_H_