Reload the gateway config without a restart

C++ · C++ · advanced · modification

Lets `gatewayd` pick up config changes without a restart. The new `config_store.hpp` / `config_store.cpp` add `load_config`, which reads, parses and validates the file (used at startup and on every reload), and `ConfigStore`, which keeps the config in use as a `std::shared_ptr<const Config>` behind a mutex: `current()` hands out a snapshot, and `reload()` loads the file again and swaps the new config in once it is valid, counting generations. `handler.hpp` / `handler.cpp` take the store instead of a `const Config&` and take one snapshot per request. `main.cpp` builds the store from the startup config, wires `reload()` to the file watcher and to `POST /reload` on the admin port, and reloads once if the file changed between the startup read and the watcher's start. `tests/config_store_test.cpp` covers the validation limits, a published and a rejected reload, a snapshot held across a reload, and a run with reader threads, two reloading threads and a thread replacing the file.

`gatewayd` is the company's internal HTTP gateway, in C++20 with the standard library only, built with CMake (build files unchanged) by Apple clang 17 locally and GCC 13 in CI with `-Wall -Wextra -Wconversion` treated as errors. Threads: `gateway::Server` (unchanged) runs 32 worker threads that call the handler concurrently, at up to about 3,000 requests per second, and rejects bodies over 16 MiB before the handler sees them; `Server::run` returns after SIGTERM once the workers have stopped. `util::FileWatcher` (unchanged) checks the file's modification time every 2 seconds on its own thread and calls its callback there; it takes the file's modification time at construction as its baseline. `gateway::AdminServer` (unchanged) serves port 9090, reachable from the ops network only, on its own thread and calls the registered handler for each POST; handlers can be registered with `on_post` after construction and apply to the requests that arrive after that. Both start their thread in the constructor and stop and join it in the destructor. Deploys replace the config file by writing a temporary file in the same directory and renaming it over the old one, so a read sees either the whole old file or the whole new one. Existing and unchanged: `Config` (`routes`, a list of `Route{prefix, upstream}`; `upstream_timeout` as `std::chrono::milliseconds`; `max_body_bytes` as `std::size_t`) and `Config::find_route` (longest matching prefix, `nullptr` when none matches); `parse_config(text, error)`, which checks syntax only (`route <prefix> <host:port>` lines with a prefix starting with `/`, `timeout_ms` as a decimal `uint32`, `max_body_bytes` as a decimal `size_t`, both required) and returns `std::nullopt` with `error` set otherwise; `util::read_file` (`std::nullopt` when the file cannot be opened); `util::log_info` / `util::log_error` (stderr, safe from any thread); `Upstream::forward(route, request, timeout)` (safe from any thread, blocks for up to the timeout). Apple clang 17's libc++ does not provide `std::atomic<std::shared_ptr<T>>` (it fails to compile there), and the team does not use the free `std::atomic_load` / `std::atomic_store` overloads for `shared_ptr`, deprecated in C++20, so shared state goes behind a `std::mutex`. An allocation failure terminates the process; `std::bad_alloc` is not caught anywhere in the gateway. Tests use the repo's existing helper `tests/check.hpp` (unchanged): `TEST(name)` registers a test, `CHECK(expr)` and `CHECK_EQ(a, b)` record a failure without stopping, and `tests/check_main.cpp` runs every registered test and exits non-zero on any failure.

Requirements

Files touched

Review this PR

C++ practice