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