Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client: defer notification of connect() failure #94

Merged
merged 1 commit into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading