]> git.ipfire.org Git - thirdparty/squid.git/blame - src/pconn.h
Simplify appending SBuf to String (#2108)
[thirdparty/squid.git] / src / pconn.h
CommitLineData
bbc27441 1/*
1f7b830e 2 * Copyright (C) 1996-2025 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
91d1cfb1 62 // TODO: Upgrade to return size_t
983983ce 63 int count() const { return size_; }
91d1cfb1 64
9815f129 65 void closeN(size_t count);
2dba5b8e 66
a27fcaed 67 // IndependentRunner API
337b9aa4 68 void endingShutdown() override;
781ce8ff 69private:
a8c28b85 70 bool isAvailable(int i) const;
91d1cfb1 71 bool removeAt(size_t index);
80463bb4 72 int findIndexOf(const Comm::ConnectionPointer &conn) const;
a8c28b85 73 void findAndClose(const Comm::ConnectionPointer &conn);
80463bb4 74 static IOCB Read;
8d77a37c 75 static CTCB Timeout;
781ce8ff 76
781ce8ff 77private:
80463bb4 78 /** List of connections we are holding.
50847dca 79 * Sorted as FIFO list for most efficient speeds on pop() and findUsable()
80463bb4
AJ
80 * The worst-case pop() and scans occur on timeout and link closure events
81 * where timing is less critical. Occasional slow additions are okay.
82 */
e3981652 83 Comm::ConnectionPointer *theList_;
80463bb4
AJ
84
85 /// Number of entries theList can currently hold without re-allocating (capacity).
91d1cfb1 86 size_t capacity_;
80463bb4 87 ///< Number of in-use entries in theList
91d1cfb1 88 size_t size_;
80463bb4
AJ
89
90 /** The pool containing this sub-list.
91 * The parent performs all stats accounting, and
92 * will delete us when it dies. It persists for the
93 * full duration of our existence.
94 */
e3981652 95 PconnPool *parent_;
80463bb4 96
e3981652 97 char fakeReadBuf_[4096]; // TODO: kill magic number.
781ce8ff 98};
99
e06b8df8
HN
100#include "ip/forward.h"
101
e1f7507e
AJ
102class StoreEntry;
103class IdleConnLimit;
104
105/* for hash_table */
106#include "hash.h"
107
855150a4 108/** \ingroup PConnAPI
01eb26fc
AJ
109 * Manages idle persistent connections to a caller-defined set of
110 * servers (e.g., all HTTP servers). Uses a collection of IdleConnLists
111 * internally to list the individual open connections to each server.
112 * Controls lists existence and limits the total number of
113 * idle connections across the collection.
855150a4 114 */
781ce8ff 115class PconnPool
116{
117
118public:
e8dca475 119 PconnPool(const char *aDescription, const CbcPointer<PeerPoolMgr> &aMgr);
7a56c509 120 ~PconnPool();
781ce8ff 121
122 void moduleInit();
642a305c
AJ
123 void push(const Comm::ConnectionPointer &serverConn, const char *domain);
124
125 /**
e8dca475
CT
126 * Returns either a pointer to a popped connection to dest or nil.
127 * Closes the connection before returning its pointer unless keepOpen.
55622953 128 * For connection going to a cache_peer, supports standby connection pools.
642a305c 129 *
e8dca475
CT
130 * A caller with a non-retriable transaction should set keepOpen to false
131 * and call pop() anyway, even though the caller does not want a pconn.
132 * This forces us to close an available persistent connection, avoiding
e2849af8 133 * creating a growing number of open connections when many transactions
e8dca475
CT
134 * create (and push) persistent connections but are not retriable and,
135 * hence, do not need to pop a connection.
642a305c 136 */
e8dca475 137 Comm::ConnectionPointer pop(const Comm::ConnectionPointer &dest, const char *domain, bool keepOpen);
781ce8ff 138 void count(int uses);
2a580c13 139 void dump(std::ostream &) const;
2dba5b8e 140 void unlinkList(IdleConnList *list);
983983ce 141 void noteUses(int uses);
e8dca475
CT
142 /// closes any n connections, regardless of their destination
143 void closeN(int n);
2dba5b8e
CT
144 int count() const { return theCount; }
145 void noteConnectionAdded() { ++theCount; }
146 void noteConnectionRemoved() { assert(theCount > 0); --theCount; }
781ce8ff 147
e8dca475
CT
148 // sends an async message to the pool manager, if any
149 void notifyManager(const char *reason);
150
781ce8ff 151private:
152
642a305c 153 static const char *key(const Comm::ConnectionPointer &destLink, const char *domain);
781ce8ff 154
55622953 155 Comm::ConnectionPointer popStored(const Comm::ConnectionPointer &dest, const char *domain, const bool keepOpen);
2a580c13
FC
156 void dumpHist(std::ostream &) const;
157 void dumpHash(std::ostream &) const;
55622953 158
781ce8ff 159 int hist[PCONN_HIST_SZ];
160 hash_table *table;
161 const char *descr;
e8dca475 162 CbcPointer<PeerPoolMgr> mgr; ///< optional pool manager (for notifications)
2dba5b8e 163 int theCount; ///< the number of pooled connections
781ce8ff 164};
165
e1f7507e
AJ
166class StoreEntry;
167class PconnPool;
168
855150a4
AJ
169/** \ingroup PConnAPI
170 * The global registry of persistent connection pools.
171 */
781ce8ff 172class PconnModule
173{
174
175public:
e1f7507e 176 /** the module is a singleton until we have instance based cachemanager
62ee09ca 177 * management
178 */
179 static PconnModule * GetInstance();
e1f7507e 180 /** A thunk to the still C like CacheManager callback api. */
62ee09ca 181 static void DumpWrapper(StoreEntry *e);
182
781ce8ff 183 PconnModule();
15fab853 184 void registerWithCacheManager(void);
781ce8ff 185
e1f7507e 186 void add(PconnPool *);
e8dca475 187 void remove(PconnPool *); ///< unregister and forget about this pool object
781ce8ff 188
2a580c13 189 void dump(std::ostream &yaml);
781ce8ff 190
191private:
e8dca475
CT
192 typedef std::set<PconnPool*> Pools; ///< unordered PconnPool collection
193 Pools pools; ///< all live pools
781ce8ff 194
62ee09ca 195 static PconnModule * instance;
781ce8ff 196};
197
ff9d9458 198#endif /* SQUID_SRC_PCONN_H */
f53969cc 199