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.
This commit is contained in:
Tatsuhiro Tsujikawa 2016-07-31 19:01:29 +09:00
parent 22570b7260
commit 8c3e864989
3 changed files with 18 additions and 26 deletions

View File

@ -2571,20 +2571,19 @@ void reload_config(WorkerProcess *wp) {
// fork_worker_process and forked child process assumes new // fork_worker_process and forked child process assumes new
// configuration can be obtained from get_config(). // 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); auto pid = fork_worker_process(ipc_fd, iaddrs);
if (pid == -1) { if (pid == -1) {
LOG(ERROR) << "Failed to process new configuration"; LOG(ERROR) << "Failed to process new configuration";
new_config = replace_config(std::move(old_config));
close_not_inherited_fd(new_config.get(), iaddrs); close_not_inherited_fd(new_config.get(), iaddrs);
replace_config(old_config);
return; return;
} }
new_config.release();
delete_config(old_config);
close_unused_inherited_addr(iaddrs); close_unused_inherited_addr(iaddrs);
// Send last worker process a graceful shutdown notice // Send last worker process a graceful shutdown notice

View File

@ -60,41 +60,35 @@
namespace shrpx { namespace shrpx {
namespace { namespace {
Config *config = nullptr; std::unique_ptr<Config> config;
} // namespace } // namespace
constexpr auto SHRPX_UNIX_PATH_PREFIX = StringRef::from_lit("unix:"); 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::unique_ptr<Config> replace_config(std::unique_ptr<Config> another) {
std::swap(config, new_config); config.swap(another);
return new_config; return another;
} }
void create_config() { config = new Config(); } void create_config() { config = make_unique<Config>(); }
void delete_config(Config *config) { Config::~Config() {
if (config == nullptr) { auto &upstreamconf = http2.upstream;
return;
}
auto &http2conf = config->http2;
auto &upstreamconf = http2conf.upstream;
nghttp2_option_del(upstreamconf.option); nghttp2_option_del(upstreamconf.option);
nghttp2_option_del(upstreamconf.alt_mode_option); nghttp2_option_del(upstreamconf.alt_mode_option);
nghttp2_session_callbacks_del(upstreamconf.callbacks); nghttp2_session_callbacks_del(upstreamconf.callbacks);
auto &downstreamconf = http2conf.downstream; auto &downstreamconf = http2.downstream;
nghttp2_option_del(downstreamconf.option); nghttp2_option_del(downstreamconf.option);
nghttp2_session_callbacks_del(downstreamconf.callbacks); nghttp2_session_callbacks_del(downstreamconf.callbacks);
auto &dumpconf = http2conf.upstream.debug.dump; auto &dumpconf = http2.upstream.debug.dump;
if (dumpconf.request_header) { if (dumpconf.request_header) {
fclose(dumpconf.request_header); fclose(dumpconf.request_header);
@ -103,8 +97,6 @@ void delete_config(Config *config) {
if (dumpconf.response_header) { if (dumpconf.response_header) {
fclose(dumpconf.response_header); fclose(dumpconf.response_header);
} }
delete config;
} }
TicketKeys::~TicketKeys() { TicketKeys::~TicketKeys() {

View File

@ -706,6 +706,8 @@ struct APIConfig {
}; };
struct Config { struct Config {
~Config();
HttpProxy downstream_http_proxy; HttpProxy downstream_http_proxy;
HttpConfig http; HttpConfig http;
Http2Config http2; Http2Config http2;
@ -734,9 +736,8 @@ const Config *get_config();
Config *mod_config(); Config *mod_config();
// Replaces the current config with given |new_config|. The old config is // Replaces the current config with given |new_config|. The old config is
// returned. // returned.
Config *replace_config(Config *new_config); std::unique_ptr<Config> replace_config(std::unique_ptr<Config> new_config);
void create_config(); void create_config();
void delete_config(Config *config);
// generated by gennghttpxfun.py // generated by gennghttpxfun.py
enum { enum {