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
- `gatewayd` picks up a new config without a restart: when the config file changes (the existing `util::FileWatcher` checks it every 2 seconds) and on `POST /reload` on the admin port.
- A new config is used only if the file can be read, parses, and passes validation: at least one route, no two routes with the same prefix, `timeout_ms` from 1 to 30000, and `max_body_bytes` from 1 to 16777216 (16 MiB). The file read at startup goes through the same validation, and the gateway exits with status 1 when it fails.
- A rejected reload keeps the config in use and is logged with the reason; the admin endpoint answers 422 without the details.
- Every request is served by one config from start to finish (route, body limit and upstream timeout all from the same config), even when a reload is published while it runs.
- Each published config has a generation number: 1 at startup, one more for each published reload; a rejected reload does not change it. The log line and the admin reply name the generation in use after that reload.
- Request threads never wait for a file read or a parse.
Files touched
- src/gateway/config_store.hpp
- src/gateway/config_store.cpp
- src/gateway/handler.hpp
- src/gateway/handler.cpp
- src/main.cpp
- tests/config_store_test.cpp