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