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

Avoid double attempt at reconnecting #310

Merged
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
14 changes: 13 additions & 1 deletion lib/HandlerBase.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ HandlerBase::HandlerBase(const ClientImplPtr& client, const std::string& topic,
state_(NotStarted),
backoff_(backoff),
epoch_(0),
timer_(executor_->createDeadlineTimer()) {}
timer_(executor_->createDeadlineTimer()),
reconnectionPending_(false) {}

HandlerBase::~HandlerBase() { timer_->cancel(); }

Expand Down Expand Up @@ -69,6 +70,13 @@ void HandlerBase::grabCnx() {
LOG_INFO(getName() << "Ignoring reconnection request since we're already connected");
return;
}

bool expectedState = false;
if (!reconnectionPending_.compare_exchange_strong(expectedState, true)) {
LOG_DEBUG(getName() << "Ignoring reconnection attempt since there's already a pending reconnection");
return;
}

LOG_INFO(getName() << "Getting connection from pool");
ClientImplPtr client = client_.lock();
Future<Result, ClientConnectionWeakPtr> future = client->getConnection(*topic_);
Expand All @@ -83,6 +91,9 @@ void HandlerBase::handleNewConnection(Result result, ClientConnectionWeakPtr con
LOG_DEBUG("HandlerBase Weak reference is not valid anymore");
return;
}

handler->reconnectionPending_ = false;

if (result == ResultOk) {
ClientConnectionPtr conn = connection.lock();
if (conn) {
Expand Down Expand Up @@ -140,6 +151,7 @@ void HandlerBase::handleDisconnection(Result result, ClientConnectionWeakPtr con

void HandlerBase::scheduleReconnection(HandlerBasePtr handler) {
const auto state = handler->state_.load();

if (state == Pending || state == Ready) {
TimeDuration delay = handler->backoff_.next();

Expand Down
1 change: 1 addition & 0 deletions lib/HandlerBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class HandlerBase {
DeadlineTimerPtr timer_;

mutable std::mutex connectionMutex_;
std::atomic<bool> reconnectionPending_;
ClientConnectionWeakPtr connection_;
friend class ClientConnection;
friend class PulsarFriend;
Expand Down