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