include/boost/burl/detail/connection_pool.hpp

91.7% Lines (22/24) 76.9% List of functions (10/13) 50.0% Branches (3/6)
connection_pool.hpp
f(x) Functions (13)
Function Calls Lines Branches Blocks
boost::capy::task<boost::capy::io_result<unsigned long> > boost::burl::detail::connection::read_some<boost::capy::mutable_buffer>(boost::capy::mutable_buffer) :44 1x 100.0% 100.0% 100.0% boost::capy::task<boost::capy::io_result<unsigned long> > boost::burl::detail::connection::read_some<boost::span<boost::capy::mutable_buffer const, 18446744073709551615ul> >(boost::span<boost::capy::mutable_buffer const, 18446744073709551615ul>) :44 35x 100.0% 100.0% 100.0% boost::capy::task<boost::capy::io_result<unsigned long> > boost::burl::detail::connection::read_some<std::span<boost::capy::mutable_buffer const, 18446744073709551615ul> >(std::span<boost::capy::mutable_buffer const, 18446744073709551615ul>) :44 0 0.0% 0.0% 0.0% boost::capy::task<boost::capy::io_result<unsigned long> > boost::burl::detail::connection::write_some<boost::capy::detail::slice_impl<boost::capy::const_buffer>::data_view>(boost::capy::detail::slice_impl<boost::capy::const_buffer>::data_view) :52 36x 100.0% 100.0% 100.0% boost::capy::task<boost::capy::io_result<unsigned long> > boost::burl::detail::connection::write_some<boost::span<boost::capy::const_buffer const, 18446744073709551615ul> >(boost::span<boost::capy::const_buffer const, 18446744073709551615ul>) :52 2x 100.0% 100.0% 100.0% boost::capy::task<boost::capy::io_result<unsigned long> > boost::burl::detail::connection::write_some<std::span<boost::capy::const_buffer const, 18446744073709551615ul> >(std::span<boost::capy::const_buffer const, 18446744073709551615ul>) :52 0 0.0% 0.0% 0.0% boost::burl::detail::connection::~connection() :64 49x 100.0% 100.0% boost::burl::detail::pooled_connection::pooled_connection() :86 7x 80.0% 100.0% boost::capy::task<boost::capy::io_result<unsigned long> > boost::burl::detail::pooled_connection::read_some<boost::capy::mutable_buffer>(boost::capy::mutable_buffer) :90 1x 75.0% 25.0% 43.0% boost::capy::task<boost::capy::io_result<unsigned long> > boost::burl::detail::pooled_connection::read_some<boost::span<boost::capy::mutable_buffer const, 18446744073709551615ul> >(boost::span<boost::capy::mutable_buffer const, 18446744073709551615ul>) :90 35x 75.0% 25.0% 43.0% boost::capy::task<boost::capy::io_result<unsigned long> > boost::burl::detail::pooled_connection::read_some<std::span<boost::capy::mutable_buffer const, 18446744073709551615ul> >(std::span<boost::capy::mutable_buffer const, 18446744073709551615ul>) :90 0 0.0% 0.0% 0.0% boost::burl::detail::pooled_connection::operator bool() const :107 120x 100.0% 100.0% boost::burl::detail::pooled_connection::pooled_connection(std::unique_ptr<boost::burl::detail::connection, std::default_delete<boost::burl::detail::connection> >, std::weak_ptr<boost::burl::detail::connection_pool>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::optional<std::chrono::duration<long, std::ratio<1l, 1000000000l> > >) :117 54x 100.0% 100.0%
Line Branch TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Mohammad Nejati
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/cppalliance/burl
8 //
9
10 #ifndef BOOST_BURL_DETAIL_CONNECTION_POOL_HPP
11 #define BOOST_BURL_DETAIL_CONNECTION_POOL_HPP
12
13 #include <boost/burl/detail/config.hpp>
14 #include <boost/capy/buffers.hpp>
15 #include <boost/capy/detail/buffer_array.hpp>
16 #include <boost/capy/io_task.hpp>
17 #include <boost/capy/timeout.hpp>
18
19 #include <chrono>
20 #include <cstddef>
21 #include <memory>
22 #include <optional>
23 #include <span>
24 #include <string>
25 #include <utility>
26
27 namespace boost
28 {
29 namespace burl
30 {
31 namespace detail
32 {
33
34 class connection_pool;
35
36 class connection
37 {
38 capy::detail::buffer_array<8, false> rba_; // TODO
39 capy::detail::buffer_array<8, true> wba_; // TODO
40
41 public:
42 template<capy::MutableBufferSequence MB>
43 capy::io_task<std::size_t>
44 36x read_some(MB buffers)
45 {
46 36x rba_ = buffers;
47
1/1
✓ Branch 2 taken 36 times.
36x return do_read_some(rba_);
48 }
49
50 template<capy::ConstBufferSequence CB>
51 capy::io_task<std::size_t>
52 38x write_some(CB buffers)
53 {
54 38x wba_ = buffers;
55
1/1
✓ Branch 2 taken 38 times.
38x return do_write_some(wba_);
56 }
57
58 virtual bool
59 is_open() const noexcept = 0;
60
61 virtual capy::io_task<>
62 shutdown() = 0;
63
64 49x virtual ~connection() = default;
65
66 private:
67 virtual capy::io_task<std::size_t>
68 do_read_some(std::span<capy::mutable_buffer const> buffers) = 0;
69
70 virtual capy::io_task<std::size_t>
71 do_write_some(std::span<capy::const_buffer const> buffers) = 0;
72 };
73
74 class pooled_connection
75 {
76 friend class connection_pool;
77
78 using duration = std::chrono::steady_clock::duration;
79
80 std::unique_ptr<connection> conn_;
81 std::weak_ptr<connection_pool> pool_;
82 std::string key_;
83 std::optional<duration> io_timeout_;
84
85 public:
86 7x pooled_connection() = default;
87
88 template<capy::MutableBufferSequence MB>
89 capy::io_task<std::size_t>
90 36x read_some(MB buffers)
91 {
92
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
36x if(io_timeout_)
93 return capy::timeout(conn_->read_some(buffers), *io_timeout_);
94 36x return conn_->read_some(buffers);
95 }
96
97 template<capy::ConstBufferSequence CB>
98 capy::io_task<std::size_t>
99 38x write_some(CB buffers)
100 {
101 38x if(io_timeout_)
102 return capy::timeout(conn_->write_some(buffers), *io_timeout_);
103 38x return conn_->write_some(buffers);
104 }
105
106 explicit
107 120x operator bool() const noexcept
108 {
109 120x return conn_ != nullptr;
110 }
111
112 BOOST_BURL_DECL
113 void
114 return_to_pool();
115
116 private:
117 54x pooled_connection(
118 std::unique_ptr<connection> conn,
119 std::weak_ptr<connection_pool> pool,
120 std::string key,
121 std::optional<duration> io_timeout = std::nullopt)
122 54x : conn_(std::move(conn))
123 54x , pool_(std::move(pool))
124 54x , key_(std::move(key))
125 54x , io_timeout_(io_timeout)
126 {
127 54x }
128 };
129
130 } // namespace detail
131 } // namespace burl
132 } // namespace boost
133
134 #endif
135