From 8c3e864989ae6773e960206332f5d6326a7456d7 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 31 Jul 2016 19:01:29 +0900 Subject: [PATCH] nghttpx: Define ~Config for automatic clean up with std::unique_ptr Now config global is backed with std::unique_ptr. configuration swapping dance is now a bit cleaner, but YMMV. --- src/shrpx.cc | 9 ++++----- src/shrpx_config.cc | 30 +++++++++++------------------- src/shrpx_config.h | 5 +++-- 3 files changed, 18 insertions(+), 26 deletions(-) diff --git a/src/shrpx.cc b/src/shrpx.cc index d0a97b6b..6b330da2 100644 --- a/src/shrpx.cc +++ b/src/shrpx.cc @@ -2571,20 +2571,19 @@ void reload_config(WorkerProcess *wp) { // fork_worker_process and forked child process assumes new // configuration can be obtained from get_config(). - auto old_config = replace_config(new_config.get()); + auto old_config = replace_config(std::move(new_config)); auto pid = fork_worker_process(ipc_fd, iaddrs); if (pid == -1) { LOG(ERROR) << "Failed to process new configuration"; + + new_config = replace_config(std::move(old_config)); close_not_inherited_fd(new_config.get(), iaddrs); - replace_config(old_config); + return; } - new_config.release(); - delete_config(old_config); - close_unused_inherited_addr(iaddrs); // Send last worker process a graceful shutdown notice diff --git a/src/shrpx_config.cc b/src/shrpx_config.cc index 8caa35bd..121ebbab 100644 --- a/src/shrpx_config.cc +++ b/src/shrpx_config.cc @@ -60,41 +60,35 @@ namespace shrpx { namespace { -Config *config = nullptr; +std::unique_ptr config; } // namespace constexpr auto SHRPX_UNIX_PATH_PREFIX = StringRef::from_lit("unix:"); -const Config *get_config() { return config; } +const Config *get_config() { return config.get(); } -Config *mod_config() { return config; } +Config *mod_config() { return config.get(); } -Config *replace_config(Config *new_config) { - std::swap(config, new_config); - return new_config; +std::unique_ptr replace_config(std::unique_ptr another) { + config.swap(another); + return another; } -void create_config() { config = new Config(); } +void create_config() { config = make_unique(); } -void delete_config(Config *config) { - if (config == nullptr) { - return; - } - - auto &http2conf = config->http2; - - auto &upstreamconf = http2conf.upstream; +Config::~Config() { + auto &upstreamconf = http2.upstream; nghttp2_option_del(upstreamconf.option); nghttp2_option_del(upstreamconf.alt_mode_option); nghttp2_session_callbacks_del(upstreamconf.callbacks); - auto &downstreamconf = http2conf.downstream; + auto &downstreamconf = http2.downstream; nghttp2_option_del(downstreamconf.option); nghttp2_session_callbacks_del(downstreamconf.callbacks); - auto &dumpconf = http2conf.upstream.debug.dump; + auto &dumpconf = http2.upstream.debug.dump; if (dumpconf.request_header) { fclose(dumpconf.request_header); @@ -103,8 +97,6 @@ void delete_config(Config *config) { if (dumpconf.response_header) { fclose(dumpconf.response_header); } - - delete config; } TicketKeys::~TicketKeys() { diff --git a/src/shrpx_config.h b/src/shrpx_config.h index e4a4c36b..d0645ff3 100644 --- a/src/shrpx_config.h +++ b/src/shrpx_config.h @@ -706,6 +706,8 @@ struct APIConfig { }; struct Config { + ~Config(); + HttpProxy downstream_http_proxy; HttpConfig http; Http2Config http2; @@ -734,9 +736,8 @@ const Config *get_config(); Config *mod_config(); // Replaces the current config with given |new_config|. The old config is // returned. -Config *replace_config(Config *new_config); +std::unique_ptr replace_config(std::unique_ptr new_config); void create_config(); -void delete_config(Config *config); // generated by gennghttpxfun.py enum {