1 /*
2  * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #ifndef SQUID_PEERPOOLMGR_H
10 #define SQUID_PEERPOOLMGR_H
11 
12 #include "base/AsyncJob.h"
13 #include "comm/forward.h"
14 #include "security/forward.h"
15 
16 class HttpRequest;
17 class CachePeer;
18 class CommConnectCbParams;
19 
20 /// Maintains an fixed-size "standby" PconnPool for a single CachePeer.
21 class PeerPoolMgr: public AsyncJob
22 {
23     CBDATA_CLASS(PeerPoolMgr);
24 
25 public:
26     typedef CbcPointer<PeerPoolMgr> Pointer;
27 
28     // syncs mgr state whenever connection-related peer or pool state changes
29     static void Checkpoint(const Pointer &mgr, const char *reason);
30 
31     explicit PeerPoolMgr(CachePeer *aPeer);
32     virtual ~PeerPoolMgr();
33 
34 protected:
35     /* AsyncJob API */
36     virtual void start();
37     virtual void swanSong();
38     virtual bool doneAll() const;
39 
40     /// whether the peer is still out there and in a valid state we can safely use
41     bool validPeer() const;
42 
43     /// Starts new connection, or closes the excess connections
44     /// according pool configuration
45     void checkpoint(const char *reason);
46     /// starts the process of opening a new standby connection (if possible)
47     void openNewConnection();
48     /// closes 'howMany' standby connections
49     void closeOldConnections(const int howMany);
50 
51     /// Comm::ConnOpener calls this when done opening a connection for us
52     void handleOpenedConnection(const CommConnectCbParams &params);
53 
54     /// Security::PeerConnector callback
55     void handleSecuredPeer(Security::EncryptorAnswer &answer);
56 
57     /// called when the connection we are trying to secure is closed by a 3rd party
58     void handleSecureClosure(const CommCloseCbParams &params);
59 
60     /// the final step in connection opening (and, optionally, securing) sequence
61     void pushNewConnection(const Comm::ConnectionPointer &conn);
62 
63 private:
64     CachePeer *peer; ///< the owner of the pool we manage
65     RefCount<HttpRequest> request; ///< fake HTTP request for conn opening code
66     AsyncCall::Pointer opener; ///< whether we are opening a connection
67     AsyncCall::Pointer securer; ///< whether we are securing a connection
68     AsyncCall::Pointer closer; ///< monitors conn while we are securing it
69     unsigned int addrUsed; ///< counter for cycling through peer addresses
70 };
71 
72 #endif /* SQUID_PEERPOOLMGR_H */
73 
74