diff --git a/src/asio_server.cc b/src/asio_server.cc index 2d468e6c..323a8991 100644 --- a/src/asio_server.cc +++ b/src/asio_server.cc @@ -36,8 +36,6 @@ #include "asio_server.h" -#include - #include "asio_server_connection.h" #include "util.h" @@ -46,9 +44,7 @@ namespace asio_http2 { namespace server { server::server(std::size_t io_service_pool_size) - : io_service_pool_(io_service_pool_size), - tick_timer_(io_service_pool_.get_io_service(), - boost::posix_time::seconds(1)) {} + : io_service_pool_(io_service_pool_size) {} boost::system::error_code server::listen_and_serve(boost::system::error_code &ec, @@ -69,8 +65,6 @@ server::listen_and_serve(boost::system::error_code &ec, } } - start_timer(); - io_service_pool_.run(); return ec; @@ -123,18 +117,6 @@ boost::system::error_code server::bind_and_listen(boost::system::error_code &ec, return ec; } -std::shared_ptr cached_date; - -void server::start_timer() { - cached_date = std::make_shared(util::http_date(time(nullptr))); - - tick_timer_.async_wait([this](const boost::system::error_code &e) { - tick_timer_.expires_at(tick_timer_.expires_at() + - boost::posix_time::seconds(1)); - start_timer(); - }); -} - void server::start_accept(boost::asio::ssl::context &tls_context, tcp::acceptor &acceptor, serve_mux &mux) { auto new_connection = std::make_shared>( diff --git a/src/asio_server.h b/src/asio_server.h index 9f120a80..bb4159e9 100644 --- a/src/asio_server.h +++ b/src/asio_server.h @@ -84,14 +84,10 @@ private: const std::string &port, int backlog); - void start_timer(); - /// The pool of io_service objects used to perform asynchronous /// operations. io_service_pool io_service_pool_; - boost::asio::deadline_timer tick_timer_; - /// Acceptor used to listen for incoming connections. std::vector acceptors_; diff --git a/src/asio_server_http2_handler.cc b/src/asio_server_http2_handler.cc index af7bdce7..76436aec 100644 --- a/src/asio_server_http2_handler.cc +++ b/src/asio_server_http2_handler.cc @@ -41,8 +41,6 @@ namespace asio_http2 { namespace server { -extern std::shared_ptr cached_date; - namespace { int stream_error(nghttp2_session *session, int32_t stream_id, uint32_t error_code) { @@ -229,10 +227,21 @@ int on_frame_not_send_callback(nghttp2_session *session, http2_handler::http2_handler(boost::asio::io_service &io_service, connection_write writefun, serve_mux &mux) : writefun_(writefun), mux_(mux), io_service_(io_service), - session_(nullptr), buf_(nullptr), buflen_(0), inside_callback_(false) {} + session_(nullptr), buf_(nullptr), buflen_(0), inside_callback_(false), + tstamp_cached_(time(nullptr)), + formatted_date_(util::http_date(tstamp_cached_)) {} http2_handler::~http2_handler() { nghttp2_session_del(session_); } +const std::string &http2_handler::http_date() { + auto t = time(nullptr); + if (t != tstamp_cached_) { + tstamp_cached_ = t; + formatted_date_ = util::http_date(t); + } + return formatted_date_; +} + int http2_handler::start() { int rv; @@ -317,9 +326,9 @@ int http2_handler::start_response(stream &strm) { auto nva = std::vector(); nva.reserve(2 + header.size()); auto status = util::utos(res.status_code()); - auto date = cached_date; + auto date = http_date(); nva.push_back(nghttp2::http2::make_nv_ls(":status", status)); - nva.push_back(nghttp2::http2::make_nv_ls("date", *date)); + nva.push_back(nghttp2::http2::make_nv_ls("date", date)); for (auto &hd : header) { nva.push_back(nghttp2::http2::make_nv(hd.first, hd.second.value, hd.second.sensitive)); diff --git a/src/asio_server_http2_handler.h b/src/asio_server_http2_handler.h index d08475a2..2f5d69b9 100644 --- a/src/asio_server_http2_handler.h +++ b/src/asio_server_http2_handler.h @@ -87,6 +87,8 @@ public: boost::asio::io_service &io_service(); + const std::string &http_date(); + template int on_read(const boost::array &buffer, std::size_t len) { callback_guard cg(*this); @@ -152,6 +154,8 @@ private: const uint8_t *buf_; std::size_t buflen_; bool inside_callback_; + time_t tstamp_cached_; + std::string formatted_date_; }; } // namespace server