src: Try to write immediately after read

This commit is contained in:
Tatsuhiro Tsujikawa 2015-02-11 00:44:30 +09:00
parent f360b5c1e3
commit 4401f697e5
4 changed files with 31 additions and 9 deletions

View File

@ -104,15 +104,6 @@ Stats::Stats(size_t req_todo)
Stream::Stream() : status_success(-1) {} Stream::Stream() : status_success(-1) {}
namespace {
void readcb(struct ev_loop *loop, ev_io *w, int revents) {
auto client = static_cast<Client *>(w->data);
if (client->do_read() != 0) {
client->fail();
}
}
} // namespace
namespace { namespace {
void writecb(struct ev_loop *loop, ev_io *w, int revents) { void writecb(struct ev_loop *loop, ev_io *w, int revents) {
auto client = static_cast<Client *>(w->data); auto client = static_cast<Client *>(w->data);
@ -132,6 +123,20 @@ void writecb(struct ev_loop *loop, ev_io *w, int revents) {
} }
} // namespace } // namespace
namespace {
void readcb(struct ev_loop *loop, ev_io *w, int revents) {
auto client = static_cast<Client *>(w->data);
if (client->do_read() != 0) {
client->fail();
return;
}
if (ev_is_active(&client->wev)) {
writecb(loop, &client->wev, revents);
// client->disconnect() and client->fail() may be called
}
}
} // namespace
Client::Client(Worker *worker, size_t req_todo) Client::Client(Worker *worker, size_t req_todo)
: worker(worker), ssl(nullptr), next_addr(config.addrs), reqidx(0), : worker(worker), ssl(nullptr), next_addr(config.addrs), reqidx(0),
state(CLIENT_IDLE), req_todo(req_todo), req_started(0), req_done(0), state(CLIENT_IDLE), req_todo(req_todo), req_started(0), req_done(0),

View File

@ -82,6 +82,12 @@ void readcb(struct ev_loop *loop, ev_io *w, int revents) {
delete handler; delete handler;
return; return;
} }
if (ev_is_active(handler->get_wev())) {
if (handler->do_write() != 0) {
delete handler;
return;
}
}
} }
} // namespace } // namespace
@ -729,4 +735,6 @@ void ClientHandler::signal_write() { conn_.wlimit.startw(); }
RateLimit *ClientHandler::get_rlimit() { return &conn_.rlimit; } RateLimit *ClientHandler::get_rlimit() { return &conn_.rlimit; }
RateLimit *ClientHandler::get_wlimit() { return &conn_.wlimit; } RateLimit *ClientHandler::get_wlimit() { return &conn_.wlimit; }
ev_io *ClientHandler::get_wev() { return &conn_.wev; }
} // namespace shrpx } // namespace shrpx

View File

@ -129,6 +129,7 @@ public:
RateLimit *get_wlimit(); RateLimit *get_wlimit();
void signal_write(); void signal_write();
ev_io *get_wev();
private: private:
Connection conn_; Connection conn_;

View File

@ -95,6 +95,14 @@ void readcb(struct ev_loop *loop, ev_io *w, int revents) {
rv = http2session->do_read(); rv = http2session->do_read();
if (rv != 0) { if (rv != 0) {
http2session->disconnect(http2session->should_hard_fail()); http2session->disconnect(http2session->should_hard_fail());
return;
}
if (ev_is_active(http2session->get_wev())) {
rv = http2session->do_write();
if (rv != 0) {
http2session->disconnect(http2session->should_hard_fail());
return;
}
} }
} }
} // namespace } // namespace