@@ -240,7 +240,7 @@ class cluster_impl : public std::enable_shared_from_this<cluster_impl>
240
240
241
241
void open (const std::string& connection_string,
242
242
const cluster_options& options,
243
- cluster_connect_handler && handler)
243
+ core::utils::movable_function< void (error, cluster)> && handler)
244
244
{
245
245
core_.open (
246
246
options_to_origin (connection_string, options),
@@ -512,39 +512,52 @@ cluster::search(std::string index_name,
512
512
return barrier->get_future ();
513
513
}
514
514
515
- auto
516
- cluster::connect (const std::string& connection_string,
517
- const cluster_options& options) -> std::future<std::pair<error, cluster>>
515
+ namespace
518
516
{
519
- auto barrier = std::make_shared<std::promise<std::pair<error, cluster>>>();
520
- auto future = barrier->get_future ();
521
- connect (connection_string, options, [barrier](auto err, auto c) {
522
- barrier->set_value ({ std::move (err), std::move (c) });
523
- });
524
- return future;
525
- }
526
-
527
517
void
528
- cluster::connect (const std::string& connection_string,
529
- const cluster_options& options,
530
- cluster_connect_handler && handler)
518
+ connect_in_background (const std::string& connection_string,
519
+ const cluster_options& options,
520
+ core::utils::movable_function< void (error, cluster)> && handler)
531
521
{
532
522
// Spawn new thread for connection to ensure that cluster_impl pointer will
533
523
// not be deallocated in IO thread in case of error.
534
524
std::thread ([connection_string, options, handler = std::move (handler)]() {
535
- auto barrier = std::make_shared<std:: promise<std::pair<error, cluster>>>() ;
536
- auto future = barrier-> get_future ();
525
+ std::promise<std::pair<error, cluster>> barrier ;
526
+ auto future = barrier. get_future ();
537
527
{
538
528
auto impl = std::make_shared<cluster_impl>();
539
- impl->open (connection_string, options, [barrier](auto err, auto c) {
540
- barrier->set_value ({ std::move (err), std::move (c) });
541
- });
529
+ impl->open (
530
+ connection_string, options, [barrier = std::move (barrier)](auto err, auto c) mutable {
531
+ barrier.set_value ({ std::move (err), std::move (c) });
532
+ });
542
533
}
543
534
544
535
auto [err, c] = future.get ();
545
536
handler (std::move (err), std::move (c));
546
537
}).detach ();
547
538
}
539
+ } // namespace
540
+
541
+ auto
542
+ cluster::connect (const std::string& connection_string,
543
+ const cluster_options& options) -> std::future<std::pair<error, cluster>>
544
+ {
545
+ std::promise<std::pair<error, cluster>> barrier;
546
+ auto future = barrier.get_future ();
547
+ connect_in_background (
548
+ connection_string, options, [barrier = std::move (barrier)](auto err, auto c) mutable {
549
+ barrier.set_value ({ std::move (err), std::move (c) });
550
+ });
551
+ return future;
552
+ }
553
+
554
+ void
555
+ cluster::connect (const std::string& connection_string,
556
+ const cluster_options& options,
557
+ cluster_connect_handler&& handler)
558
+ {
559
+ connect_in_background (connection_string, options, std::move (handler));
560
+ }
548
561
549
562
auto
550
563
cluster::notify_fork (fork_event event) -> void
0 commit comments