Stroika Library 3.0d25
 
Loading...
Searching...
No Matches
ConnectionPool.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4#ifndef _Stroika_Foundation_IO_Network_Transfer_ConnectionPool_h_
5#define _Stroika_Foundation_IO_Network_Transfer_ConnectionPool_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include "Stroika/Foundation/IO/Network/Transfer/Connection.h"
10
11/**
12 *
13 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
14 *
15 *
16 * TODO:
17 * @todo Add an auto-timeout feature, so connections disappear when not used for a certain
18 * period of time (to save memory, and networking resources).
19 */
20
22
23 /**
24 * Simple connection factory object. If you don't care what backend to use for remote connections, use this API
25 * to construct an unconnected object.
26 *
27 * \par Example Usage
28 * \code
29 * ConnectionPool connectionPool {ConnectionPool::Options{3}};
30 * auto&& connection = connectionPool.New (URI{"http://myexternalip.com/});
31 * auto&& response = connection.GET ("http://myexternalip.com/raw");
32 * nw.fExternalIPAddress = IO::Network::InternetAddress{response.GetDataTextInputStream ().ReadAll ()};
33 * \endcode
34 */
36 public:
37 /**
38 */
39 struct Options {
40 Options (const optional<unsigned int>& maxConnections = nullopt,
41 const function<Connection::Ptr ()>& connectionFactory = (Connection::Ptr (*) ())&Connection::New);
42
43 /**
44 * The most connections the pool holds - handed out, or idle in it; nullopt: no limit.
45 */
46 optional<unsigned int> fMaxConnections;
47
48 /**
49 * Factory to use for underlying connectionpool connections
50 *
51 * \todo figure out why cast needed on New on g++ and clang++
52 */
53 function<Connection::Ptr ()> fConnectionFactory;
54 };
55
56 public:
57 /**
58 * A ConnectionPool is a fixed, not copyable/movable, object, containing a bunch of other
59 * (typically http)IO::Transfer::Connection objects. The idea is that its cheaper to
60 * re-use these objects if you happen to want to connect to an HTTP endpoint that is already connected.
61 */
62 ConnectionPool (const Options& options = {});
63 ConnectionPool (const ConnectionPool&) = delete;
65
66 public:
67 nonvirtual ConnectionPool& operator= (const ConnectionPool&) = delete;
68
69 public:
70 /**
71 * Only schemeAndAuthority is looked at from (optional) hint.
72 *
73 * If timeout allocating connection (because all busy/in use),
74 * throws a timeout - a std::system_error whose code () == errc::timed_out
75 * (@see Execution::ThrowError (errc::timed_out))
76 * UNLESS
77 * If AllocateGloballyIfTimeout given argument, then instead of throwing, allocate a global connection object (Connection::New ())
78 * Caller cannot really tell for the most part, unless they call get, but if they just say what they want, they cannot tell.
79 */
80 nonvirtual Connection::Ptr New (URI hint = {});
81 nonvirtual Connection::Ptr New (const Time::Duration& timeout, URI hint = {});
82 enum AllocateGloballyIfTimeout {
83 eAllocateGloballyIfTimeout
84 };
85 nonvirtual Connection::Ptr New (AllocateGloballyIfTimeout, const Time::Duration& timeout, URI hint = {});
86
87 private:
88 class Rep_;
89 unique_ptr<Rep_> fRep_;
90 };
91
92}
93
94/*
95 ********************************************************************************
96 ***************************** Implementation Details ***************************
97 ********************************************************************************
98 */
99#include "ConnectionPool.inl"
100
101#endif /*_Stroika_Foundation_IO_Network_Transfer_ConnectionPool_h_*/
Duration is a chrono::duration<double> (=.
Definition Duration.h:96