168 lines
5.6 KiB
C++
168 lines
5.6 KiB
C++
#pragma once
|
|
|
|
#include <chrono>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace weaseldb {
|
|
|
|
/**
|
|
* @brief Configuration for a single network interface to listen on.
|
|
*/
|
|
struct ListenInterface {
|
|
enum class Type { TCP, Unix };
|
|
|
|
Type type;
|
|
/// For TCP: IP address to bind to (e.g., "127.0.0.1", "0.0.0.0")
|
|
std::string address;
|
|
/// For TCP: port number
|
|
int port = 0;
|
|
/// For Unix: socket file path
|
|
std::string path;
|
|
|
|
// Factory methods for cleaner config creation
|
|
static ListenInterface tcp(const std::string &addr, int port_num) {
|
|
return {Type::TCP, addr, port_num, ""};
|
|
}
|
|
|
|
static ListenInterface unix_socket(const std::string &socket_path) {
|
|
return {Type::Unix, "", 0, socket_path};
|
|
}
|
|
};
|
|
|
|
/**
|
|
* @brief Configuration settings for the WeaselDB server component.
|
|
*/
|
|
struct ServerConfig {
|
|
/// Network interfaces to listen on (TCP and/or Unix sockets)
|
|
std::vector<ListenInterface> interfaces;
|
|
/// Maximum size in bytes for incoming HTTP requests (default: 1MB)
|
|
int64_t max_request_size_bytes = 1024 * 1024;
|
|
/// Number of I/O threads for handling connections and network events
|
|
/// Each I/O thread gets its own dedicated epoll instance
|
|
int io_threads = 1;
|
|
/// Event batch size for epoll processing
|
|
int event_batch_size = 32;
|
|
/// Maximum number of concurrent connections (0 = unlimited)
|
|
int max_connections = 50000;
|
|
/// Buffer size for reading from socket connections (default: 16KB)
|
|
int read_buffer_size = 16 * 1024;
|
|
};
|
|
|
|
/**
|
|
* @brief Configuration settings for commit processing and validation.
|
|
*/
|
|
struct CommitConfig {
|
|
/// Minimum required length for request_id to ensure sufficient entropy
|
|
int min_request_id_length = 20;
|
|
/// How long to retain request IDs for duplicate detection
|
|
std::chrono::hours request_id_retention_hours{24};
|
|
/// Minimum number of commit versions to retain request IDs for
|
|
int64_t request_id_retention_versions = 100000000;
|
|
};
|
|
|
|
/**
|
|
* @brief Configuration settings for subscription streaming functionality.
|
|
*/
|
|
struct SubscriptionConfig {
|
|
/// Maximum buffer size for unconsumed subscription data before backpressure
|
|
int64_t max_buffer_size_bytes = 10 * 1024 * 1024;
|
|
/// Interval between keepalive comments in subscription streams
|
|
std::chrono::seconds keepalive_interval{30};
|
|
};
|
|
|
|
/**
|
|
* @brief Configuration settings for benchmarking and health check behavior.
|
|
*/
|
|
struct BenchmarkConfig {
|
|
/// CPU-intensive loop iterations for /ok requests in resolve stage
|
|
/// 0 = health check only, 4000 = default benchmark load (740ns, 1M req/s)
|
|
int ok_resolve_iterations = 0;
|
|
};
|
|
|
|
/**
|
|
* @brief Top-level configuration container for all WeaselDB settings.
|
|
*/
|
|
struct Config {
|
|
ServerConfig server; ///< Server networking and request handling settings
|
|
CommitConfig commit; ///< Commit processing and validation settings
|
|
SubscriptionConfig subscription; ///< Subscription streaming settings
|
|
BenchmarkConfig benchmark; ///< Benchmarking and health check settings
|
|
};
|
|
|
|
/**
|
|
* @brief TOML configuration file parser for WeaselDB settings.
|
|
*
|
|
* This class provides static methods to parse TOML configuration files
|
|
* and strings into structured Config objects. It uses the toml11 library
|
|
* for TOML parsing and provides fallback to default values for any
|
|
* missing configuration options.
|
|
*
|
|
* @example
|
|
* ```cpp
|
|
* // Load from file
|
|
* auto config = ConfigParser::load_from_file("config.toml");
|
|
* if (config) {
|
|
* std::cout << "Server port: " << config->server.port << std::endl;
|
|
* }
|
|
*
|
|
* // Parse from string
|
|
* std::string toml = "[server]\nport = 9090\n";
|
|
* auto config2 = ConfigParser::parse_toml_string(toml);
|
|
* ```
|
|
*/
|
|
struct ConfigParser {
|
|
public:
|
|
/**
|
|
* @brief Load configuration from a TOML file.
|
|
* @param file_path Path to the TOML configuration file
|
|
* @return Config object if successful, nullopt if parsing failed
|
|
*/
|
|
static std::optional<Config> load_from_file(const std::string &file_path);
|
|
|
|
/**
|
|
* @brief Parse configuration from a TOML string.
|
|
* @param toml_content TOML-formatted configuration string
|
|
* @return Config object if successful, nullopt if parsing failed
|
|
*/
|
|
static std::optional<Config>
|
|
parse_toml_string(const std::string &toml_content);
|
|
|
|
/**
|
|
* @brief Validate configuration values are within reasonable bounds.
|
|
*
|
|
* This function performs comprehensive validation of all configuration
|
|
* parameters and writes detailed diagnostic messages to stderr for any
|
|
* validation failures or warnings encountered.
|
|
*
|
|
* @param config Configuration object to validate
|
|
* @return true if configuration is valid, false otherwise
|
|
* @note Validation errors and warnings are written to stderr
|
|
*/
|
|
static bool validate_config(const Config &config);
|
|
|
|
private:
|
|
// Generic configuration parsing utilities
|
|
template <typename T>
|
|
static void parse_field(const auto §ion, const std::string &field_name,
|
|
T &target);
|
|
|
|
template <typename Rep, typename Period>
|
|
static void parse_duration_field(const auto §ion,
|
|
const std::string &field_name,
|
|
std::chrono::duration<Rep, Period> &target);
|
|
|
|
static void parse_section(const auto &toml_data,
|
|
const std::string §ion_name, auto parse_func);
|
|
|
|
static void parse_server_config(const auto &toml_data, ServerConfig &config);
|
|
static void parse_commit_config(const auto &toml_data, CommitConfig &config);
|
|
static void parse_subscription_config(const auto &toml_data,
|
|
SubscriptionConfig &config);
|
|
static void parse_benchmark_config(const auto &toml_data,
|
|
BenchmarkConfig &config);
|
|
};
|
|
|
|
} // namespace weaseldb
|