Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
ConnectionPool.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. 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 "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 * Default options for each connection
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 * throw TimeoutException
75 * UNLESS
76 * If AllocateGloballyIfTimeout given argument, then instead of throwing, allocate a global connection object (Connection::New ())
77 * Caller cannot really tell for the most part, unless they call get, but if they just say what they want, they cannot tell.
78 */
79 nonvirtual Connection::Ptr New (URI hint = {});
80 nonvirtual Connection::Ptr New (const Time::Duration& timeout, URI hint = {});
81 enum AllocateGloballyIfTimeout {
82 eAllocateGloballyIfTimeout
83 };
84 nonvirtual Connection::Ptr New (AllocateGloballyIfTimeout, const Time::Duration& timeout, URI hint = {});
85
86 private:
87 class Rep_;
88 unique_ptr<Rep_> fRep_;
89 };
90
91}
92
93/*
94 ********************************************************************************
95 ***************************** Implementation Details ***************************
96 ********************************************************************************
97 */
98#include "ConnectionPool.inl"
99
100#endif /*_Stroika_Foundation_IO_Network_Transfer_ConnectionPool_h_*/
Duration is a chrono::duration<double> (=.
Definition Duration.h:96