2525#include < any>
2626#include < atomic>
2727#include < cstdint>
28+ #include < future>
29+ #include < optional>
2830#ifdef USE_ASIO
2931#include < asio/bind_executor.hpp>
3032#include < asio/io_context.hpp>
@@ -156,11 +158,8 @@ class PULSAR_PUBLIC ClientConnection : public std::enable_shared_from_this<Clien
156158 * Close the connection.
157159 *
158160 * @param result all pending futures will complete with this result
159- * @param detach remove it from the pool if it's true
160- *
161- * `detach` should only be false when the connection pool is closed.
162161 */
163- void close (Result result = ResultConnectError, bool detach = true );
162+ const std::future< void >& close (Result result = ResultConnectError);
164163
165164 bool isClosed () const ;
166165
@@ -193,7 +192,7 @@ class PULSAR_PUBLIC ClientConnection : public std::enable_shared_from_this<Clien
193192
194193 const std::string& brokerAddress () const ;
195194
196- const std::string& cnxString () const ;
195+ auto cnxString () const { return * std::atomic_load (&cnxStringPtr_); }
197196
198197 int getServerProtocolVersion () const ;
199198
@@ -219,28 +218,51 @@ class PULSAR_PUBLIC ClientConnection : public std::enable_shared_from_this<Clien
219218 mockingRequests_.store (true , std::memory_order_release);
220219 }
221220
222- void handleKeepAliveTimeout ();
221+ void handleKeepAliveTimeout (const ASIO_ERROR& ec );
223222
224223 private:
225224 struct PendingRequestData {
226225 Promise<Result, ResponseData> promise;
227226 DeadlineTimerPtr timer;
228227 std::shared_ptr<std::atomic_bool> hasGotResponse{std::make_shared<std::atomic_bool>(false )};
228+
229+ void fail (Result result) {
230+ cancelTimer (*timer);
231+ ;
232+ promise.setFailed (result);
233+ }
229234 };
230235
231236 struct LookupRequestData {
232237 LookupDataResultPromisePtr promise;
233238 DeadlineTimerPtr timer;
239+
240+ void fail (Result result) {
241+ cancelTimer (*timer);
242+ ;
243+ promise->setFailed (result);
244+ }
234245 };
235246
236247 struct LastMessageIdRequestData {
237248 GetLastMessageIdResponsePromisePtr promise;
238249 DeadlineTimerPtr timer;
250+
251+ void fail (Result result) {
252+ cancelTimer (*timer);
253+ ;
254+ promise->setFailed (result);
255+ }
239256 };
240257
241258 struct GetSchemaRequest {
242259 Promise<Result, SchemaInfo> promise;
243260 DeadlineTimerPtr timer;
261+
262+ void fail (Result result) {
263+ cancelTimer (*timer);
264+ promise.setFailed (result);
265+ }
244266 };
245267
246268 /*
@@ -297,26 +319,26 @@ class PULSAR_PUBLIC ClientConnection : public std::enable_shared_from_this<Clien
297319 }
298320
299321 template <typename ConstBufferSequence, typename WriteHandler>
300- inline void asyncWrite (const ConstBufferSequence& buffers, WriteHandler handler) {
322+ inline void asyncWrite (const ConstBufferSequence& buffers, WriteHandler&& handler) {
301323 if (isClosed ()) {
302324 return ;
303325 }
304326 if (tlsSocket_) {
305- ASIO::async_write (*tlsSocket_, buffers, ASIO::bind_executor (strand_, handler));
327+ ASIO::async_write (*tlsSocket_, buffers, std::forward<WriteHandler>( handler));
306328 } else {
307329 ASIO::async_write (*socket_, buffers, handler);
308330 }
309331 }
310332
311333 template <typename MutableBufferSequence, typename ReadHandler>
312- inline void asyncReceive (const MutableBufferSequence& buffers, ReadHandler handler) {
334+ inline void asyncReceive (const MutableBufferSequence& buffers, ReadHandler&& handler) {
313335 if (isClosed ()) {
314336 return ;
315337 }
316338 if (tlsSocket_) {
317- tlsSocket_->async_read_some (buffers, ASIO::bind_executor (strand_, handler));
339+ tlsSocket_->async_read_some (buffers, std::forward<ReadHandler>( handler));
318340 } else {
319- socket_->async_receive (buffers, handler);
341+ socket_->async_receive (buffers, std::forward<ReadHandler>( handler) );
320342 }
321343 }
322344
@@ -337,7 +359,6 @@ class PULSAR_PUBLIC ClientConnection : public std::enable_shared_from_this<Clien
337359 */
338360 SocketPtr socket_;
339361 TlsSocketPtr tlsSocket_;
340- ASIO::strand<ASIO::io_context::executor_type> strand_;
341362
342363 const std::string logicalAddress_;
343364 /*
@@ -350,7 +371,7 @@ class PULSAR_PUBLIC ClientConnection : public std::enable_shared_from_this<Clien
350371 ClientConfiguration::ProxyProtocol proxyProtocol_;
351372
352373 // Represent both endpoint of the tcp connection. eg: [client:1234 -> server:6650]
353- std::string cnxString_ ;
374+ std::shared_ptr<std:: string> cnxStringPtr_ ;
354375
355376 /*
356377 * indicates if async connection establishment failed
@@ -419,6 +440,7 @@ class PULSAR_PUBLIC ClientConnection : public std::enable_shared_from_this<Clien
419440 const std::string clientVersion_;
420441 ConnectionPool& pool_;
421442 const size_t poolIndex_;
443+ std::optional<std::future<void >> closeFuture_;
422444
423445 friend class PulsarFriend ;
424446 friend class ConsumerTest ;
0 commit comments