Skip to content

Commit

Permalink
client: defer notification of connect() failure
Browse files Browse the repository at this point in the history
... to bevEvent() callback to handle early failure
the same as later disconnect.
  • Loading branch information
mdavidsaver committed Dec 22, 2024
1 parent 614e0b7 commit c3e91f6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/clientconn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,16 @@ void Connection::startConnecting()
timeval tmo(totv(context->effective.tcpTimeout));
bufferevent_set_timeouts(bev.get(), &tmo, &tmo);

if(bufferevent_socket_connect(bev.get(), const_cast<sockaddr*>(&peerAddr->sa), peerAddr.size()))
throw std::runtime_error("Unable to begin connecting");
if(bufferevent_socket_connect(bev.get(), const_cast<sockaddr*>(&peerAddr->sa), peerAddr.size())) {
// non-blocking connect() failed immediately.
// try to defer notification.
state = Disconnected;
constexpr timeval immediate{0, 0};
if(event_add(echoTimer.get(), &immediate))
throw std::runtime_error(SB()<<"Unable to begin connecting or schedule deferred notification "<<peerName);
log_warn_printf(io, "Unable to connect() to %s\n", peerName.c_str());
return;
}

connect(std::move(bev));

Expand Down Expand Up @@ -480,6 +488,11 @@ void Connection::tickEcho()

startConnecting();

}else if(state==Disconnected) {
// deferred notification of early connect() failure.
// TODO: avoid a misleading "closed by peer" error
bevEvent(BEV_EVENT_EOF);

} else {
log_debug_printf(io, "Server %s ping\n", peerName.c_str());

Expand Down
1 change: 1 addition & 0 deletions src/clientimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ struct Connection final : public ConnBase, public std::enable_shared_from_this<C

// While HoldOff, the time until re-connection
// While Connected, periodic Echo
// After early connect() failure, deferred notification
const evevent echoTimer;

bool ready = false;
Expand Down

0 comments on commit c3e91f6

Please sign in to comment.