nghttpx: Reduce epoll_ctl call
This commit is contained in:
parent
23dd428d65
commit
83309b6391
|
@ -256,7 +256,7 @@ ClientHandler::ClientHandler(bufferevent *bev,
|
|||
CLOG(FATAL, this) << "bufferevent_add_to_rate_limit_group() failed";
|
||||
}
|
||||
|
||||
bufferevent_enable(bev_, EV_READ | EV_WRITE);
|
||||
util::bev_enable_unless(bev_, EV_READ | EV_WRITE);
|
||||
bufferevent_setwatermark(bev_, EV_READ, 0, SHRPX_READ_WATERMARK);
|
||||
set_upstream_timeouts(&get_config()->upstream_read_timeout,
|
||||
&get_config()->upstream_write_timeout);
|
||||
|
@ -298,7 +298,7 @@ ClientHandler::~ClientHandler()
|
|||
|
||||
bufferevent_remove_from_rate_limit_group(bev_);
|
||||
|
||||
bufferevent_disable(bev_, EV_READ | EV_WRITE);
|
||||
util::bev_disable_unless(bev_, EV_READ | EV_WRITE);
|
||||
bufferevent_free(bev_);
|
||||
|
||||
if(ssl_) {
|
||||
|
|
|
@ -89,7 +89,7 @@ int Http2Session::disconnect()
|
|||
}
|
||||
if(bev_) {
|
||||
int fd = bufferevent_getfd(bev_);
|
||||
bufferevent_disable(bev_, EV_READ | EV_WRITE);
|
||||
util::bev_disable_unless(bev_, EV_READ | EV_WRITE);
|
||||
bufferevent_free(bev_);
|
||||
bev_ = nullptr;
|
||||
if(fd != -1) {
|
||||
|
@ -219,7 +219,7 @@ int Http2Session::init_notification()
|
|||
close(sockpair[1]);
|
||||
return -1;
|
||||
}
|
||||
bufferevent_enable(rdbev_, EV_READ);
|
||||
util::bev_enable_unless(rdbev_, EV_READ);
|
||||
bufferevent_setcb(rdbev_, notify_readcb, nullptr, notify_eventcb, this);
|
||||
return 0;
|
||||
}
|
||||
|
@ -417,7 +417,7 @@ int Http2Session::initiate_connection()
|
|||
close(fd);
|
||||
return SHRPX_ERR_NETWORK;
|
||||
}
|
||||
bufferevent_enable(bev_, EV_READ);
|
||||
util::bev_enable_unless(bev_, EV_READ);
|
||||
bufferevent_set_timeouts(bev_, &get_config()->downstream_read_timeout,
|
||||
&get_config()->downstream_write_timeout);
|
||||
|
||||
|
@ -534,7 +534,7 @@ int Http2Session::initiate_connection()
|
|||
}
|
||||
|
||||
bufferevent_setwatermark(bev_, EV_READ, 0, SHRPX_READ_WATERMARK);
|
||||
bufferevent_enable(bev_, EV_READ);
|
||||
util::bev_enable_unless(bev_, EV_READ);
|
||||
bufferevent_setcb(bev_, readcb, writecb, eventcb, this);
|
||||
// Set timeout for HTTP2 session
|
||||
reset_timeouts();
|
||||
|
|
|
@ -54,7 +54,7 @@ HttpDownstreamConnection::HttpDownstreamConnection
|
|||
HttpDownstreamConnection::~HttpDownstreamConnection()
|
||||
{
|
||||
if(bev_) {
|
||||
bufferevent_disable(bev_, EV_READ | EV_WRITE);
|
||||
util::bev_disable_unless(bev_, EV_READ | EV_WRITE);
|
||||
bufferevent_free(bev_);
|
||||
}
|
||||
// Downstream and DownstreamConnection may be deleted
|
||||
|
@ -127,7 +127,7 @@ int HttpDownstreamConnection::attach_downstream(Downstream *downstream)
|
|||
response_htp_.data = downstream_;
|
||||
|
||||
bufferevent_setwatermark(bev_, EV_READ, 0, SHRPX_READ_WATERMARK);
|
||||
bufferevent_enable(bev_, EV_READ);
|
||||
util::bev_enable_unless(bev_, EV_READ);
|
||||
bufferevent_setcb(bev_,
|
||||
upstream->get_downstream_readcb(),
|
||||
upstream->get_downstream_writecb(),
|
||||
|
@ -369,7 +369,7 @@ void HttpDownstreamConnection::detach_downstream(Downstream *downstream)
|
|||
}
|
||||
downstream_ = nullptr;
|
||||
ioctrl_.force_resume_read();
|
||||
bufferevent_enable(bev_, EV_READ);
|
||||
util::bev_enable_unless(bev_, EV_READ);
|
||||
bufferevent_setcb(bev_, 0, 0, idle_eventcb, this);
|
||||
// On idle state, just enable read timeout. Normally idle downstream
|
||||
// connection will get EOF from the downstream server and closed.
|
||||
|
|
|
@ -26,6 +26,10 @@
|
|||
|
||||
#include <algorithm>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
using namespace nghttp2;
|
||||
|
||||
namespace shrpx {
|
||||
|
||||
IOControl::IOControl(bufferevent *bev)
|
||||
|
@ -45,7 +49,7 @@ void IOControl::pause_read(IOCtrlReason reason)
|
|||
{
|
||||
rdbits_ |= reason;
|
||||
if(bev_) {
|
||||
bufferevent_disable(bev_, EV_READ);
|
||||
util::bev_disable_unless(bev_, EV_READ);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -54,7 +58,7 @@ bool IOControl::resume_read(IOCtrlReason reason)
|
|||
rdbits_ &= ~reason;
|
||||
if(rdbits_ == 0) {
|
||||
if(bev_) {
|
||||
bufferevent_enable(bev_, EV_READ);
|
||||
util::bev_enable_unless(bev_, EV_READ);
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
|
@ -66,7 +70,7 @@ void IOControl::force_resume_read()
|
|||
{
|
||||
rdbits_ = 0;
|
||||
if(bev_) {
|
||||
bufferevent_enable(bev_, EV_READ);
|
||||
util::bev_enable_unless(bev_, EV_READ);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -112,7 +112,7 @@ void worker_writecb(bufferevent *bev, void *ptr)
|
|||
|
||||
// Disable bev so that this won' be called accidentally in the
|
||||
// future.
|
||||
bufferevent_disable(bev, EV_READ | EV_WRITE);
|
||||
util::bev_disable_unless(bev, EV_READ | EV_WRITE);
|
||||
}
|
||||
} // namespace
|
||||
#endif // NOTHREADS
|
||||
|
|
|
@ -113,7 +113,7 @@ void Worker::run()
|
|||
http2session.get(),
|
||||
http1_connect_blocker.get());
|
||||
|
||||
bufferevent_enable(bev.get(), EV_READ);
|
||||
util::bev_enable_unless(bev.get(), EV_READ);
|
||||
bufferevent_setcb(bev.get(), readcb, nullptr, eventcb, receiver.get());
|
||||
|
||||
event_base_loop(evbase.get(), 0);
|
||||
|
|
18
src/util.cc
18
src/util.cc
|
@ -657,6 +657,24 @@ char* get_exec_path(int argc, char **const argv, const char *cwd)
|
|||
return path;
|
||||
}
|
||||
|
||||
void bev_enable_unless(bufferevent *bev, int events)
|
||||
{
|
||||
if((bufferevent_get_enabled(bev) & events) == events) {
|
||||
return;
|
||||
}
|
||||
|
||||
bufferevent_enable(bev, events);
|
||||
}
|
||||
|
||||
void bev_disable_unless(bufferevent *bev, int events)
|
||||
{
|
||||
if((bufferevent_get_enabled(bev) & events) == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
bufferevent_disable(bev, events);
|
||||
}
|
||||
|
||||
} // namespace util
|
||||
|
||||
} // namespace nghttp2
|
||||
|
|
|
@ -38,6 +38,7 @@
|
|||
#include <memory>
|
||||
|
||||
#include <event2/buffer.h>
|
||||
#include <event2/bufferevent.h>
|
||||
|
||||
#include "http-parser/http_parser.h"
|
||||
|
||||
|
@ -481,6 +482,12 @@ std::string ascii_dump(const uint8_t *data, size_t len);
|
|||
// it.
|
||||
char* get_exec_path(int argc, char **const argv, const char *cwd);
|
||||
|
||||
// These functions are provided to reduce epoll_ctl syscall. Avoid
|
||||
// calling bufferevent_enable/disable() unless it is required by
|
||||
// sniffing current enabled events.
|
||||
void bev_enable_unless(bufferevent *bev, int events);
|
||||
void bev_disable_unless(bufferevent *bev, int events);
|
||||
|
||||
} // namespace util
|
||||
|
||||
} // namespace nghttp2
|
||||
|
|
Loading…
Reference in New Issue