]> git.ipfire.org Git - thirdparty/squid.git/blob - src/pconn.h
SourceFormat Enforcement
[thirdparty/squid.git] / src / pconn.h
1 /*
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
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
9 #ifndef SQUID_PCONN_H
10 #define SQUID_PCONN_H
11
12 #include "base/CbcPointer.h"
13 #include <set>
14
15 /**
16 \defgroup PConnAPI Persistent Connection API
17 \ingroup Component
18 *
19 \todo CLEANUP: Break multiple classes out of the generic pconn.h header
20 */
21
22 class PconnPool;
23 class PeerPoolMgr;
24
25 #include "cbdata.h"
26 #include "hash.h"
27 /* for IOCB */
28 #include "comm.h"
29
30 /// \ingroup PConnAPI
31 #define PCONN_HIST_SZ (1<<16)
32
33 /** \ingroup PConnAPI
34 * A list of connections currently open to a particular destination end-point.
35 */
36 class IdleConnList
37 {
38 CBDATA_CLASS(IdleConnList);
39
40 public:
41 IdleConnList(const char *key, PconnPool *parent);
42 ~IdleConnList();
43
44 /// Pass control of the connection to the idle list.
45 void push(const Comm::ConnectionPointer &conn);
46
47 /// get first conn which is not pending read fd.
48 Comm::ConnectionPointer pop();
49
50 /** Search the list for a connection which matches the 'key' details
51 * and pop it off the list.
52 * The list is created based on remote IP:port hash. This further filters
53 * the choices based on specific local-end details requested.
54 * If nothing usable is found the a nil pointer is returned.
55 */
56 Comm::ConnectionPointer findUseable(const Comm::ConnectionPointer &key);
57
58 void clearHandlers(const Comm::ConnectionPointer &conn);
59
60 int count() const { return size_; }
61 void closeN(size_t count);
62
63 private:
64 bool isAvailable(int i) const;
65 bool removeAt(int index);
66 int findIndexOf(const Comm::ConnectionPointer &conn) const;
67 void findAndClose(const Comm::ConnectionPointer &conn);
68 static IOCB Read;
69 static CTCB Timeout;
70
71 public:
72 hash_link hash; /** must be first */
73
74 private:
75 /** List of connections we are holding.
76 * Sorted as FIFO list for most efficient speeds on pop() and findUsable()
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 */
80 Comm::ConnectionPointer *theList_;
81
82 /// Number of entries theList can currently hold without re-allocating (capacity).
83 int capacity_;
84 ///< Number of in-use entries in theList
85 int size_;
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 */
92 PconnPool *parent_;
93
94 char fakeReadBuf_[4096]; // TODO: kill magic number.
95 };
96
97 #include "ip/forward.h"
98
99 class StoreEntry;
100 class IdleConnLimit;
101
102 /* for hash_table */
103 #include "hash.h"
104
105 /** \ingroup PConnAPI
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.
111 */
112 class PconnPool
113 {
114
115 public:
116 PconnPool(const char *aDescription, const CbcPointer<PeerPoolMgr> &aMgr);
117 ~PconnPool();
118
119 void moduleInit();
120 void push(const Comm::ConnectionPointer &serverConn, const char *domain);
121
122 /**
123 * Returns either a pointer to a popped connection to dest or nil.
124 * Closes the connection before returning its pointer unless keepOpen.
125 *
126 * A caller with a non-retriable transaction should set keepOpen to false
127 * and call pop() anyway, even though the caller does not want a pconn.
128 * This forces us to close an available persistent connection, avoiding
129 * creating a growing number of open connections when many transactions
130 * create (and push) persistent connections but are not retriable and,
131 * hence, do not need to pop a connection.
132 */
133 Comm::ConnectionPointer pop(const Comm::ConnectionPointer &dest, const char *domain, bool keepOpen);
134 void count(int uses);
135 void dumpHist(StoreEntry *e) const;
136 void dumpHash(StoreEntry *e) const;
137 void unlinkList(IdleConnList *list);
138 void noteUses(int uses);
139 /// closes any n connections, regardless of their destination
140 void closeN(int n);
141 int count() const { return theCount; }
142 void noteConnectionAdded() { ++theCount; }
143 void noteConnectionRemoved() { assert(theCount > 0); --theCount; }
144
145 // sends an async message to the pool manager, if any
146 void notifyManager(const char *reason);
147
148 private:
149
150 static const char *key(const Comm::ConnectionPointer &destLink, const char *domain);
151
152 int hist[PCONN_HIST_SZ];
153 hash_table *table;
154 const char *descr;
155 CbcPointer<PeerPoolMgr> mgr; ///< optional pool manager (for notifications)
156 int theCount; ///< the number of pooled connections
157 };
158
159 class StoreEntry;
160 class PconnPool;
161
162 /** \ingroup PConnAPI
163 * The global registry of persistent connection pools.
164 */
165 class PconnModule
166 {
167
168 public:
169 /** the module is a singleton until we have instance based cachemanager
170 * management
171 */
172 static PconnModule * GetInstance();
173 /** A thunk to the still C like CacheManager callback api. */
174 static void DumpWrapper(StoreEntry *e);
175
176 PconnModule();
177 void registerWithCacheManager(void);
178
179 void add(PconnPool *);
180 void remove(PconnPool *); ///< unregister and forget about this pool object
181
182 OBJH dump;
183
184 private:
185 typedef std::set<PconnPool*> Pools; ///< unordered PconnPool collection
186 Pools pools; ///< all live pools
187
188 static PconnModule * instance;
189 };
190
191 #endif /* SQUID_PCONN_H */
192