nghttpx: Use std::shared_ptr to share cached time

To ensure that cached time buffer is not altered while referencing it,
we rely on inerlocking in std::shared_ptr to acheive this.
This commit is contained in:
Tatsuhiro Tsujikawa 2014-07-06 13:27:51 +09:00
parent 933e24d412
commit a02624c9ed
3 changed files with 15 additions and 22 deletions

View File

@ -276,16 +276,9 @@ void reopen_log_signal_cb(evutil_socket_t sig, short events, void *arg)
} // namespace
namespace {
char time_cache_buf[2][64];
size_t time_cache_buf_idx = 0;
} // namespace
namespace {
const char* cache_time(size_t idx)
std::unique_ptr<std::string> generate_time()
{
auto buf = time_cache_buf[idx];
buf[0] = '\0';
char buf[32];
// Format data like this:
// 03/Jul/2014:00:19:38 +0900
@ -293,24 +286,21 @@ const char* cache_time(size_t idx)
auto now = time(nullptr);
if(localtime_r(&now, &tms) == nullptr) {
return buf;
return util::make_unique<std::string>("");
}
if(strftime(buf, sizeof(time_cache_buf[0]), "%d/%b/%Y:%T %z", &tms) == 0) {
buf[0] = '\0';
return buf;
if(strftime(buf, sizeof(buf), "%d/%b/%Y:%T %z", &tms) == 0) {
return util::make_unique<std::string>("");
}
return buf;
return util::make_unique<std::string>(buf);
}
} // namespace
namespace {
void refresh_cb(evutil_socket_t sig, short events, void *arg)
{
time_cache_buf_idx ^= 1;
mod_config()->cached_time = cache_time(time_cache_buf_idx);
mod_config()->cached_time = generate_time();
}
} // namespace
@ -569,8 +559,7 @@ void fill_default_config()
(mod_config()->http2_option, 1);
mod_config()->tls_proto_mask = 0;
mod_config()->cached_time.store(cache_time(time_cache_buf_idx),
std::memory_order_release);
mod_config()->cached_time = generate_time();
}
} // namespace

View File

@ -153,6 +153,7 @@ struct Config {
std::vector<AltSvc> altsvcs;
std::vector<std::pair<std::string, std::string>> add_response_headers;
std::vector<unsigned char> alpn_prefs;
std::shared_ptr<std::string> cached_time;
sockaddr_union downstream_addr;
// binary form of http proxy host and port
sockaddr_union downstream_http_proxy_addr;
@ -200,7 +201,6 @@ struct Config {
FILE *http2_upstream_dump_request_header;
FILE *http2_upstream_dump_response_header;
nghttp2_option *http2_option;
std::atomic<const char*> cached_time;
size_t downstream_addrlen;
size_t num_worker;
size_t http2_max_concurrent_streams;

View File

@ -117,9 +117,11 @@ Log::~Log()
char buf[4096];
auto tty = worker_config.errorlog_tty;
auto cached_time = get_config()->cached_time;
rv = snprintf(buf, sizeof(buf),
"%s PID%d [%s%s%s] %s\n %s(%s:%d)%s\n",
get_config()->cached_time.load(std::memory_order_acquire),
cached_time->c_str(),
getpid(),
tty ? SEVERITY_COLOR[severity_] : "",
SEVERITY_STR[severity_],
@ -181,9 +183,11 @@ void upstream_accesslog(const std::string& client_ip, unsigned int status_code,
static const char fmt[] =
"%s - - [%s] \"%s %s HTTP/%u.%u\" %u %lld \"-\" \"%s\"\n";
auto cached_time = get_config()->cached_time;
rv = snprintf(buf, sizeof(buf), fmt,
client_ip.c_str(),
get_config()->cached_time.load(std::memory_order_acquire),
cached_time->c_str(),
method,
path,
major,