]>
Commit | Line | Data |
---|---|---|
bbc27441 | 1 | /* |
b8ae064d | 2 | * Copyright (C) 1996-2023 The Squid Software Foundation and contributors |
bbc27441 AJ |
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 | ||
781ce8ff | 9 | #ifndef SQUID_PCONN_H |
10 | #define SQUID_PCONN_H | |
11 | ||
e8dca475 | 12 | #include "base/CbcPointer.h" |
a27fcaed | 13 | #include "base/RunnersRegistry.h" |
5ce18e2a AJ |
14 | #include "mgr/forward.h" |
15 | ||
e8dca475 CT |
16 | #include <set> |
17 | ||
e1f7507e AJ |
18 | /** |
19 | \defgroup PConnAPI Persistent Connection API | |
20 | \ingroup Component | |
e1f7507e | 21 | */ |
62ee09ca | 22 | |
781ce8ff | 23 | class PconnPool; |
e8dca475 | 24 | class PeerPoolMgr; |
781ce8ff | 25 | |
e1f7507e | 26 | #include "cbdata.h" |
e1f7507e AJ |
27 | #include "hash.h" |
28 | /* for IOCB */ | |
29 | #include "comm.h" | |
30 | ||
e1f7507e | 31 | /// \ingroup PConnAPI |
62ee09ca | 32 | #define PCONN_HIST_SZ (1<<16) |
33 | ||
855150a4 AJ |
34 | /** \ingroup PConnAPI |
35 | * A list of connections currently open to a particular destination end-point. | |
855150a4 | 36 | */ |
a27fcaed | 37 | class IdleConnList: public hash_link, private IndependentRunner |
781ce8ff | 38 | { |
5c2f68b7 AJ |
39 | CBDATA_CLASS(IdleConnList); |
40 | ||
781ce8ff | 41 | public: |
42 | IdleConnList(const char *key, PconnPool *parent); | |
337b9aa4 | 43 | ~IdleConnList() override; |
68720ca2 | 44 | |
80463bb4 | 45 | /// Pass control of the connection to the idle list. |
642a305c | 46 | void push(const Comm::ConnectionPointer &conn); |
139d9221 | 47 | |
9815f129 AJ |
48 | /// get first conn which is not pending read fd. |
49 | Comm::ConnectionPointer pop(); | |
781ce8ff | 50 | |
80463bb4 AJ |
51 | /** Search the list for a connection which matches the 'key' details |
52 | * and pop it off the list. | |
139d9221 AJ |
53 | * The list is created based on remote IP:port hash. This further filters |
54 | * the choices based on specific local-end details requested. | |
80463bb4 | 55 | * If nothing usable is found the a nil pointer is returned. |
139d9221 AJ |
56 | */ |
57 | Comm::ConnectionPointer findUseable(const Comm::ConnectionPointer &key); | |
80463bb4 | 58 | |
642a305c | 59 | void clearHandlers(const Comm::ConnectionPointer &conn); |
781ce8ff | 60 | |
983983ce | 61 | int count() const { return size_; } |
9815f129 | 62 | void closeN(size_t count); |
2dba5b8e | 63 | |
a27fcaed | 64 | // IndependentRunner API |
337b9aa4 | 65 | void endingShutdown() override; |
781ce8ff | 66 | private: |
a8c28b85 | 67 | bool isAvailable(int i) const; |
80463bb4 AJ |
68 | bool removeAt(int index); |
69 | int findIndexOf(const Comm::ConnectionPointer &conn) const; | |
a8c28b85 | 70 | void findAndClose(const Comm::ConnectionPointer &conn); |
80463bb4 | 71 | static IOCB Read; |
8d77a37c | 72 | static CTCB Timeout; |
781ce8ff | 73 | |
781ce8ff | 74 | private: |
80463bb4 | 75 | /** List of connections we are holding. |
50847dca | 76 | * Sorted as FIFO list for most efficient speeds on pop() and findUsable() |
80463bb4 AJ |
77 | * The worst-case pop() and scans occur on timeout and link closure events |
78 | * where timing is less critical. Occasional slow additions are okay. | |
79 | */ | |
e3981652 | 80 | Comm::ConnectionPointer *theList_; |
80463bb4 AJ |
81 | |
82 | /// Number of entries theList can currently hold without re-allocating (capacity). | |
e3981652 | 83 | int capacity_; |
80463bb4 | 84 | ///< Number of in-use entries in theList |
e3981652 | 85 | int size_; |
80463bb4 AJ |
86 | |
87 | /** The pool containing this sub-list. | |
88 | * The parent performs all stats accounting, and | |
89 | * will delete us when it dies. It persists for the | |
90 | * full duration of our existence. | |
91 | */ | |
e3981652 | 92 | PconnPool *parent_; |
80463bb4 | 93 | |
e3981652 | 94 | char fakeReadBuf_[4096]; // TODO: kill magic number. |
781ce8ff | 95 | }; |
96 | ||
e06b8df8 HN |
97 | #include "ip/forward.h" |
98 | ||
e1f7507e AJ |
99 | class StoreEntry; |
100 | class IdleConnLimit; | |
101 | ||
102 | /* for hash_table */ | |
103 | #include "hash.h" | |
104 | ||
855150a4 | 105 | /** \ingroup PConnAPI |
01eb26fc AJ |
106 | * Manages idle persistent connections to a caller-defined set of |
107 | * servers (e.g., all HTTP servers). Uses a collection of IdleConnLists | |
108 | * internally to list the individual open connections to each server. | |
109 | * Controls lists existence and limits the total number of | |
110 | * idle connections across the collection. | |
855150a4 | 111 | */ |
781ce8ff | 112 | class PconnPool |
113 | { | |
114 | ||
115 | public: | |
e8dca475 | 116 | PconnPool(const char *aDescription, const CbcPointer<PeerPoolMgr> &aMgr); |
7a56c509 | 117 | ~PconnPool(); |
781ce8ff | 118 | |
119 | void moduleInit(); | |
642a305c AJ |
120 | void push(const Comm::ConnectionPointer &serverConn, const char *domain); |
121 | ||
122 | /** | |
e8dca475 CT |
123 | * Returns either a pointer to a popped connection to dest or nil. |
124 | * Closes the connection before returning its pointer unless keepOpen. | |
55622953 | 125 | * For connection going to a cache_peer, supports standby connection pools. |
642a305c | 126 | * |
e8dca475 CT |
127 | * A caller with a non-retriable transaction should set keepOpen to false |
128 | * and call pop() anyway, even though the caller does not want a pconn. | |
129 | * This forces us to close an available persistent connection, avoiding | |
e2849af8 | 130 | * creating a growing number of open connections when many transactions |
e8dca475 CT |
131 | * create (and push) persistent connections but are not retriable and, |
132 | * hence, do not need to pop a connection. | |
642a305c | 133 | */ |
e8dca475 | 134 | Comm::ConnectionPointer pop(const Comm::ConnectionPointer &dest, const char *domain, bool keepOpen); |
781ce8ff | 135 | void count(int uses); |
642a305c AJ |
136 | void dumpHist(StoreEntry *e) const; |
137 | void dumpHash(StoreEntry *e) const; | |
2dba5b8e | 138 | void unlinkList(IdleConnList *list); |
983983ce | 139 | void noteUses(int uses); |
e8dca475 CT |
140 | /// closes any n connections, regardless of their destination |
141 | void closeN(int n); | |
2dba5b8e CT |
142 | int count() const { return theCount; } |
143 | void noteConnectionAdded() { ++theCount; } | |
144 | void noteConnectionRemoved() { assert(theCount > 0); --theCount; } | |
781ce8ff | 145 | |
e8dca475 CT |
146 | // sends an async message to the pool manager, if any |
147 | void notifyManager(const char *reason); | |
148 | ||
781ce8ff | 149 | private: |
150 | ||
642a305c | 151 | static const char *key(const Comm::ConnectionPointer &destLink, const char *domain); |
781ce8ff | 152 | |
55622953 CT |
153 | Comm::ConnectionPointer popStored(const Comm::ConnectionPointer &dest, const char *domain, const bool keepOpen); |
154 | ||
781ce8ff | 155 | int hist[PCONN_HIST_SZ]; |
156 | hash_table *table; | |
157 | const char *descr; | |
e8dca475 | 158 | CbcPointer<PeerPoolMgr> mgr; ///< optional pool manager (for notifications) |
2dba5b8e | 159 | int theCount; ///< the number of pooled connections |
781ce8ff | 160 | }; |
161 | ||
e1f7507e AJ |
162 | class StoreEntry; |
163 | class PconnPool; | |
164 | ||
855150a4 AJ |
165 | /** \ingroup PConnAPI |
166 | * The global registry of persistent connection pools. | |
167 | */ | |
781ce8ff | 168 | class PconnModule |
169 | { | |
170 | ||
171 | public: | |
e1f7507e | 172 | /** the module is a singleton until we have instance based cachemanager |
62ee09ca | 173 | * management |
174 | */ | |
175 | static PconnModule * GetInstance(); | |
e1f7507e | 176 | /** A thunk to the still C like CacheManager callback api. */ |
62ee09ca | 177 | static void DumpWrapper(StoreEntry *e); |
178 | ||
781ce8ff | 179 | PconnModule(); |
15fab853 | 180 | void registerWithCacheManager(void); |
781ce8ff | 181 | |
e1f7507e | 182 | void add(PconnPool *); |
e8dca475 | 183 | void remove(PconnPool *); ///< unregister and forget about this pool object |
781ce8ff | 184 | |
185 | OBJH dump; | |
186 | ||
187 | private: | |
e8dca475 CT |
188 | typedef std::set<PconnPool*> Pools; ///< unordered PconnPool collection |
189 | Pools pools; ///< all live pools | |
781ce8ff | 190 | |
62ee09ca | 191 | static PconnModule * instance; |
781ce8ff | 192 | }; |
193 | ||
194 | #endif /* SQUID_PCONN_H */ | |
f53969cc | 195 |