asio: Avoid repeated call of io_service::post

This commit is contained in:
Tatsuhiro Tsujikawa 2016-10-20 22:12:31 +09:00
parent f09c5c4bf9
commit 6c882e1ece
2 changed files with 10 additions and 2 deletions

View File

@ -245,6 +245,7 @@ http2_handler::http2_handler(boost::asio::io_service &io_service,
buf_(nullptr), buf_(nullptr),
buflen_(0), buflen_(0),
inside_callback_(false), inside_callback_(false),
write_signaled_(false),
tstamp_cached_(time(nullptr)), tstamp_cached_(time(nullptr)),
formatted_date_(util::http_date(tstamp_cached_)) {} formatted_date_(util::http_date(tstamp_cached_)) {}
@ -403,13 +404,17 @@ void http2_handler::stream_error(int32_t stream_id, uint32_t error_code) {
} }
void http2_handler::signal_write() { void http2_handler::signal_write() {
if (!inside_callback_) { if (!inside_callback_ && !write_signaled_) {
write_signaled_ = true;
auto self = shared_from_this(); auto self = shared_from_this();
io_service_.post([self]() { self->initiate_write(); }); io_service_.post([self]() { self->initiate_write(); });
} }
} }
void http2_handler::initiate_write() { writefun_(); } void http2_handler::initiate_write() {
write_signaled_ = false;
writefun_();
}
void http2_handler::resume(stream &strm) { void http2_handler::resume(stream &strm) {
nghttp2_session_resume_data(session_, strm.get_stream_id()); nghttp2_session_resume_data(session_, strm.get_stream_id());

View File

@ -160,6 +160,9 @@ private:
const uint8_t *buf_; const uint8_t *buf_;
std::size_t buflen_; std::size_t buflen_;
bool inside_callback_; bool inside_callback_;
// true if we have pending on_write call. This avoids repeated call
// of io_service::post.
bool write_signaled_;
time_t tstamp_cached_; time_t tstamp_cached_;
std::string formatted_date_; std::string formatted_date_;
}; };