]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/pconn.h
b3b35c42d116f8364c811193bf1a0b79b0354428
2 * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
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.
9 #ifndef SQUID_SRC_PCONN_H
10 #define SQUID_SRC_PCONN_H
12 #include "base/CbcPointer.h"
13 #include "base/RunnersRegistry.h"
14 #include "mgr/forward.h"
20 \defgroup PConnAPI Persistent Connection API
33 #define PCONN_HIST_SZ (1<<16)
36 * A list of connections currently open to a particular destination end-point.
38 class IdleConnList
: public hash_link
, private IndependentRunner
40 CBDATA_CLASS(IdleConnList
);
43 IdleConnList(const char *key
, PconnPool
*parent
);
44 ~IdleConnList() override
;
46 /// Pass control of the connection to the idle list.
47 void push(const Comm::ConnectionPointer
&conn
);
49 /// get first conn which is not pending read fd.
50 Comm::ConnectionPointer
pop();
52 /** Search the list for a connection which matches the 'key' details
53 * and pop it off the list.
54 * The list is created based on remote IP:port hash. This further filters
55 * the choices based on specific local-end details requested.
56 * If nothing usable is found the a nil pointer is returned.
58 Comm::ConnectionPointer
findUseable(const Comm::ConnectionPointer
&key
);
60 void clearHandlers(const Comm::ConnectionPointer
&conn
);
62 // TODO: Upgrade to return size_t
63 int count() const { return size_
; }
65 void closeN(size_t count
);
67 // IndependentRunner API
68 void endingShutdown() override
;
70 bool isAvailable(int i
) const;
71 bool removeAt(size_t index
);
72 int findIndexOf(const Comm::ConnectionPointer
&conn
) const;
73 void findAndClose(const Comm::ConnectionPointer
&conn
);
78 /** List of connections we are holding.
79 * Sorted as FIFO list for most efficient speeds on pop() and findUsable()
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.
83 Comm::ConnectionPointer
*theList_
;
85 /// Number of entries theList can currently hold without re-allocating (capacity).
87 ///< Number of in-use entries in theList
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.
97 char fakeReadBuf_
[4096]; // TODO: kill magic number.
100 #include "ip/forward.h"
108 /** \ingroup PConnAPI
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.
119 PconnPool(const char *aDescription
, const CbcPointer
<PeerPoolMgr
> &aMgr
);
123 void push(const Comm::ConnectionPointer
&serverConn
, const char *domain
);
126 * Returns either a pointer to a popped connection to dest or nil.
127 * Closes the connection before returning its pointer unless keepOpen.
128 * For connection going to a cache_peer, supports standby connection pools.
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
133 * creating a growing number of open connections when many transactions
134 * create (and push) persistent connections but are not retriable and,
135 * hence, do not need to pop a connection.
137 Comm::ConnectionPointer
pop(const Comm::ConnectionPointer
&dest
, const char *domain
, bool keepOpen
);
138 void count(int uses
);
139 void dump(std::ostream
&) const;
140 void unlinkList(IdleConnList
*list
);
141 void noteUses(int uses
);
142 /// closes any n connections, regardless of their destination
144 int count() const { return theCount
; }
145 void noteConnectionAdded() { ++theCount
; }
146 void noteConnectionRemoved() { assert(theCount
> 0); --theCount
; }
148 // sends an async message to the pool manager, if any
149 void notifyManager(const char *reason
);
153 static const char *key(const Comm::ConnectionPointer
&destLink
, const char *domain
);
155 Comm::ConnectionPointer
popStored(const Comm::ConnectionPointer
&dest
, const char *domain
, const bool keepOpen
);
156 void dumpHist(std::ostream
&) const;
157 void dumpHash(std::ostream
&) const;
159 int hist
[PCONN_HIST_SZ
];
162 CbcPointer
<PeerPoolMgr
> mgr
; ///< optional pool manager (for notifications)
163 int theCount
; ///< the number of pooled connections
169 /** \ingroup PConnAPI
170 * The global registry of persistent connection pools.
176 /** the module is a singleton until we have instance based cachemanager
179 static PconnModule
* GetInstance();
180 /** A thunk to the still C like CacheManager callback api. */
181 static void DumpWrapper(StoreEntry
*e
);
184 void registerWithCacheManager(void);
186 void add(PconnPool
*);
187 void remove(PconnPool
*); ///< unregister and forget about this pool object
189 void dump(std::ostream
&yaml
);
192 typedef std::set
<PconnPool
*> Pools
; ///< unordered PconnPool collection
193 Pools pools
; ///< all live pools
195 static PconnModule
* instance
;
198 #endif /* SQUID_SRC_PCONN_H */