@@ -68,7 +68,18 @@ void HandlerBase::setCnx(const ClientConnectionPtr& cnx) {
6868 connection_ = cnx;
6969}
7070
71- void HandlerBase::grabCnx () {
71+ void HandlerBase::grabCnx () { grabCnx (boost::none); }
72+
73+ Future<Result, ClientConnectionPtr> HandlerBase::getConnection (
74+ const ClientImplPtr& client, const boost::optional<std::string>& assignedBrokerUrl) {
75+ if (assignedBrokerUrl && client->getLookupCount () > 0 ) {
76+ return client->connect (assignedBrokerUrl.get (), connectionKeySuffix_);
77+ } else {
78+ return client->getConnection (topic (), connectionKeySuffix_);
79+ }
80+ }
81+
82+ void HandlerBase::grabCnx (const boost::optional<std::string>& assignedBrokerUrl) {
7283 bool expectedState = false ;
7384 if (!reconnectionPending_.compare_exchange_strong (expectedState, true )) {
7485 LOG_INFO (getName () << " Ignoring reconnection attempt since there's already a pending reconnection" );
@@ -90,7 +101,7 @@ void HandlerBase::grabCnx() {
90101 return ;
91102 }
92103 auto self = shared_from_this ();
93- auto cnxFuture = client-> getConnection (topic (), connectionKeySuffix_ );
104+ auto cnxFuture = getConnection (client, assignedBrokerUrl );
94105 cnxFuture.addListener ([this , self](Result result, const ClientConnectionPtr& cnx) {
95106 if (result == ResultOk) {
96107 LOG_DEBUG (getName () << " Connected to broker: " << cnx->cnxString ());
@@ -141,37 +152,37 @@ void HandlerBase::handleDisconnection(Result result, const ClientConnectionPtr&
141152 break ;
142153 }
143154}
144-
145- void HandlerBase::scheduleReconnection () {
155+ void HandlerBase::scheduleReconnection () { scheduleReconnection (boost::none); }
156+ void HandlerBase::scheduleReconnection (const boost::optional<std::string>& assignedBrokerUrl ) {
146157 const auto state = state_.load ();
147158
148159 if (state == Pending || state == Ready) {
149- TimeDuration delay = backoff_.next ();
160+ TimeDuration delay = assignedBrokerUrl ? std::chrono::milliseconds ( 0 ) : backoff_.next ();
150161
151162 LOG_INFO (getName () << " Schedule reconnection in " << (toMillis (delay) / 1000.0 ) << " s" );
152163 timer_->expires_from_now (delay);
153164 // passing shared_ptr here since time_ will get destroyed, so tasks will be cancelled
154165 // so we will not run into the case where grabCnx is invoked on out of scope handler
155166 auto name = getName ();
156167 std::weak_ptr<HandlerBase> weakSelf{shared_from_this ()};
157- timer_->async_wait ([name, weakSelf](const ASIO_ERROR& ec) {
168+ timer_->async_wait ([name, weakSelf, assignedBrokerUrl ](const ASIO_ERROR& ec) {
158169 auto self = weakSelf.lock ();
159170 if (self) {
160- self->handleTimeout (ec);
171+ self->handleTimeout (ec, assignedBrokerUrl );
161172 } else {
162173 LOG_WARN (name << " Cancel the reconnection since the handler is destroyed" );
163174 }
164175 });
165176 }
166177}
167178
168- void HandlerBase::handleTimeout (const ASIO_ERROR& ec) {
179+ void HandlerBase::handleTimeout (const ASIO_ERROR& ec, const boost::optional<std::string>& assignedBrokerUrl ) {
169180 if (ec) {
170181 LOG_DEBUG (getName () << " Ignoring timer cancelled event, code[" << ec << " ]" );
171182 return ;
172183 } else {
173184 epoch_++;
174- grabCnx ();
185+ grabCnx (assignedBrokerUrl );
175186 }
176187}
177188
0 commit comments