PeerPoolMgr.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2022 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#include "squid.h"
10#include "AccessLogEntry.h"
11#include "base/AsyncJobCalls.h"
13#include "CachePeer.h"
14#include "comm/Connection.h"
15#include "comm/ConnOpener.h"
16#include "debug/Stream.h"
17#include "fd.h"
18#include "FwdState.h"
19#include "globals.h"
20#include "HttpRequest.h"
21#include "MasterXaction.h"
22#include "neighbors.h"
23#include "pconn.h"
24#include "PeerPoolMgr.h"
26#include "SquidConfig.h"
27
29
31class MyAnswerDialer: public UnaryMemFunT<PeerPoolMgr, Security::EncryptorAnswer, Security::EncryptorAnswer&>,
33{
34public:
35 MyAnswerDialer(const JobPointer &aJob, Method aMethod):
36 UnaryMemFunT<PeerPoolMgr, Security::EncryptorAnswer, Security::EncryptorAnswer&>(aJob, aMethod, Security::EncryptorAnswer()) {}
37
38 /* Security::PeerConnector::CbDialer API */
39 virtual Security::EncryptorAnswer &answer() { return arg1; }
40};
41
43 peer(cbdataReference(aPeer)),
44 request(),
45 transportWait(),
46 encryptionWait(),
47 addrUsed(0)
48{
49}
50
52{
54}
55
56void
58{
60
61 const auto mx = MasterXaction::MakePortless<XactionInitiator::initPeerPool>();
62 // ErrorState, getOutgoingAddress(), and other APIs may require a request.
63 // We fake one. TODO: Optionally send this request to peers?
66
67 checkpoint("peer initialized");
68}
69
70void
72{
74}
75
76bool
78{
80}
81
82bool
84{
85 return !(validPeer() && peer->standby.limit) && AsyncJob::doneAll();
86}
87
88void
90{
92
93 if (!validPeer()) {
94 debugs(48, 3, "peer gone");
95 if (params.conn != nullptr)
96 params.conn->close();
97 return;
98 }
99
100 if (params.flag != Comm::OK) {
102 checkpoint("conn opening failure"); // may retry
103 return;
104 }
105
106 Must(params.conn != nullptr);
107
108 // Handle TLS peers.
110 // XXX: Exceptions orphan params.conn
111 AsyncCall::Pointer callback = asyncCall(48, 4, "PeerPoolMgr::handleSecuredPeer",
113
114 const auto peerTimeout = peer->connectTimeout();
115 const int timeUsed = squid_curtime - params.conn->startTime();
116 // Use positive timeout when less than one second is left for conn.
117 const int timeLeft = positiveTimeout(peerTimeout - timeUsed);
118 const auto connector = new Security::BlindPeerConnector(request, params.conn, callback, nullptr, timeLeft);
119 encryptionWait.start(connector, callback);
120 return;
121 }
122
123 pushNewConnection(params.conn);
124}
125
126void
128{
129 Must(validPeer());
131 peer->standby.pool->push(conn, nullptr /* domain */);
132 // push() will trigger a checkpoint()
133}
134
135void
137{
139
140 if (!validPeer()) {
141 debugs(48, 3, "peer gone");
142 if (answer.conn != nullptr)
143 answer.conn->close();
144 return;
145 }
146
147 assert(!answer.tunneled);
148 if (answer.error.get()) {
149 assert(!answer.conn);
150 // PeerConnector calls peerConnectFailed() for us;
151 checkpoint("conn securing failure"); // may retry
152 return;
153 }
154
155 assert(answer.conn);
156
157 // The socket could get closed while our callback was queued. Sync
158 // Connection. XXX: Connection::fd may already be stale/invalid here.
159 if (answer.conn->isOpen() && fd_table[answer.conn->fd].closing()) {
160 answer.conn->noteClosure();
161 checkpoint("external connection closure"); // may retry
162 return;
163 }
164
165 pushNewConnection(answer.conn);
166}
167
168void
170{
171 // KISS: Do nothing else when we are already doing something.
173 debugs(48, 7, "busy: " << transportWait << '|' << encryptionWait << '|' << shutting_down);
174 return; // there will be another checkpoint when we are done opening/securing
175 }
176
177 // Do not talk to a peer until it is ready.
178 if (!neighborUp(peer)) // provides debugging
179 return; // there will be another checkpoint when peer is up
180
181 // Do not violate peer limits.
182 if (!peerCanOpenMore(peer)) { // provides debugging
183 peer->standby.waitingForClose = true; // may already be true
184 return; // there will be another checkpoint when a peer conn closes
185 }
186
187 // Do not violate global restrictions.
188 if (fdUsageHigh()) {
189 debugs(48, 7, "overwhelmed");
190 peer->standby.waitingForClose = true; // may already be true
191 // There will be another checkpoint when a peer conn closes OR when
192 // a future pop() fails due to an empty pool. See PconnPool::pop().
193 return;
194 }
195
197
199 Must(peer->n_addresses); // guaranteed by neighborUp() above
200 // cycle through all available IP addresses
201 conn->remote = peer->addresses[addrUsed++ % peer->n_addresses];
202 conn->remote.port(peer->http_port);
203 conn->peerType = STANDBY_POOL; // should be reset by peerSelect()
204 conn->setPeer(peer);
207
208 const auto ctimeout = peer->connectTimeout();
211 const auto cs = new Comm::ConnOpener(conn, callback, ctimeout);
212 transportWait.start(cs, callback);
213}
214
215void
217{
218 debugs(48, 8, howMany);
219 peer->standby.pool->closeN(howMany);
220}
221
222void
223PeerPoolMgr::checkpoint(const char *reason)
224{
225 if (!validPeer()) {
226 debugs(48, 3, reason << " and peer gone");
227 return; // nothing to do after our owner dies; the job will quit
228 }
229
230 const int count = peer->standby.pool->count();
231 const int limit = peer->standby.limit;
232 debugs(48, 7, reason << " with " << count << " ? " << limit);
233
234 if (count < limit)
236 else if (count > limit)
237 closeOldConnections(count - limit);
238}
239
240void
241PeerPoolMgr::Checkpoint(const Pointer &mgr, const char *reason)
242{
243 CallJobHere1(48, 5, mgr, PeerPoolMgr, checkpoint, reason);
244}
245
248{
249public:
250 /* RegisteredRunner API */
251 virtual void useConfig() { syncConfig(); }
252 virtual void syncConfig();
253};
254
256
257void
259{
260 for (CachePeer *p = Config.peers; p; p = p->next) {
261 // On reconfigure, Squid deletes the old config (and old peers in it),
262 // so should always be dealing with a brand new configuration.
263 assert(!p->standby.mgr);
264 assert(!p->standby.pool);
265 if (p->standby.limit) {
266 p->standby.mgr = new PeerPoolMgr(p);
267 p->standby.pool = new PconnPool(p->name, p->standby.mgr);
268 AsyncJob::Start(p->standby.mgr.get());
269 }
270 }
271}
272
AsyncCall * asyncCall(int aDebugSection, int aDebugLevel, const char *aName, const Dialer &aDialer)
Definition: AsyncCall.h:154
#define JobCallback(dbgSection, dbgLevel, Dialer, job, method)
Convenience macro to create a Dialer-based job callback.
Definition: AsyncJobCalls.h:69
#define CallJobHere1(debugSection, debugLevel, job, Class, method, arg1)
Definition: AsyncJobCalls.h:63
void getOutgoingAddress(HttpRequest *request, const Comm::ConnectionPointer &conn)
Definition: FwdState.cc:1519
void GetMarkingsToServer(HttpRequest *request, Comm::Connection &conn)
Definition: FwdState.cc:1598
RunnerRegistrationEntry(PeerPoolMgrsRr)
time_t squid_curtime
Definition: stub_libtime.cc:20
class SquidConfig Config
Definition: SquidConfig.cc:12
#define Must(condition)
Definition: TextException.h:71
int conn
the current server connection FD
Definition: Transport.cc:26
#define assert(EX)
Definition: assert.h:19
int cbdataReferenceValid(const void *p)
Definition: cbdata.cc:398
#define cbdataReferenceDone(var)
Definition: cbdata.h:350
#define cbdataReference(var)
Definition: cbdata.h:341
#define CBDATA_CLASS_INIT(type)
Definition: cbdata.h:318
void host(const char *src)
Definition: Uri.cc:99
static void Start(const Pointer &job)
Definition: AsyncJob.cc:24
virtual bool doneAll() const
whether positive goal has been reached
Definition: AsyncJob.cc:97
virtual void start()
called by AsyncStart; do not call directly
Definition: AsyncJob.cc:44
virtual void swanSong()
Definition: AsyncJob.h:59
int n_addresses
Definition: CachePeer.h:151
unsigned short http_port
Definition: CachePeer.h:77
char * host
Definition: CachePeer.h:40
Security::PeerOptions secure
security settings for peer connection
Definition: CachePeer.h:190
int limit
the limit itself
Definition: CachePeer.h:183
CachePeer * next
Definition: CachePeer.h:153
PconnPool * pool
idle connection pool for this peer
Definition: CachePeer.h:181
bool waitingForClose
a conn must close before we open a standby conn
Definition: CachePeer.h:184
time_t connectTimeout() const
Definition: CachePeer.cc:51
Ip::Address addresses[10]
Definition: CachePeer.h:150
struct CachePeer::@38 standby
optional "cache_peer standby=limit" feature
Cbc * get() const
a temporary valid raw Cbc pointer or NULL
Definition: CbcPointer.h:162
Comm::Flag flag
comm layer result status.
Definition: CommCalls.h:87
Comm::ConnectionPointer conn
Definition: CommCalls.h:85
bool isOpen() const
Definition: Connection.h:101
time_t startTime() const
Definition: Connection.h:120
AnyP::Uri url
the request URI
Definition: HttpRequest.h:115
void finish()
Definition: JobWait.cc:44
void start(const JobPointer &aJob, const AsyncCall::Pointer &aCallback)
starts waiting for the given job to call the given callback
Definition: JobWait.h:69
Gives Security::PeerConnector access to Answer in the PeerPoolMgr callback dialer.
Definition: PeerPoolMgr.cc:33
MyAnswerDialer(const JobPointer &aJob, Method aMethod)
Definition: PeerPoolMgr.cc:35
virtual Security::EncryptorAnswer & answer()
gives PeerConnector access to the in-dialer answer
Definition: PeerPoolMgr.cc:39
void closeN(int n)
closes any n connections, regardless of their destination
Definition: pconn.cc:507
void count(int uses)
void push(const Comm::ConnectionPointer &serverConn, const char *domain)
Definition: pconn.cc:412
Maintains an fixed-size "standby" PconnPool for a single CachePeer.
Definition: PeerPoolMgr.h:23
JobWait< Security::BlindPeerConnector > encryptionWait
waits for the established transport connection to be secured/encrypted
Definition: PeerPoolMgr.h:69
virtual ~PeerPoolMgr()
Definition: PeerPoolMgr.cc:51
void checkpoint(const char *reason)
Definition: PeerPoolMgr.cc:223
void handleSecuredPeer(Security::EncryptorAnswer &answer)
Security::PeerConnector callback.
Definition: PeerPoolMgr.cc:136
unsigned int addrUsed
counter for cycling through peer addresses
Definition: PeerPoolMgr.h:71
void openNewConnection()
starts the process of opening a new standby connection (if possible)
Definition: PeerPoolMgr.cc:169
virtual void swanSong()
Definition: PeerPoolMgr.cc:71
virtual bool doneAll() const
whether positive goal has been reached
Definition: PeerPoolMgr.cc:83
virtual void start()
called by AsyncStart; do not call directly
Definition: PeerPoolMgr.cc:57
RefCount< HttpRequest > request
fake HTTP request for conn opening code
Definition: PeerPoolMgr.h:63
void handleOpenedConnection(const CommConnectCbParams &params)
Comm::ConnOpener calls this when done opening a connection for us.
Definition: PeerPoolMgr.cc:89
void closeOldConnections(const int howMany)
closes 'howMany' standby connections
Definition: PeerPoolMgr.cc:216
static void Checkpoint(const Pointer &mgr, const char *reason)
Definition: PeerPoolMgr.cc:241
CachePeer * peer
the owner of the pool we manage
Definition: PeerPoolMgr.h:62
JobWait< Comm::ConnOpener > transportWait
waits for a transport connection to the peer to be established/opened
Definition: PeerPoolMgr.h:66
void pushNewConnection(const Comm::ConnectionPointer &conn)
the final step in connection opening (and, optionally, securing) sequence
Definition: PeerPoolMgr.cc:127
bool validPeer() const
whether the peer is still out there and in a valid state we can safely use
Definition: PeerPoolMgr.cc:77
PeerPoolMgr(CachePeer *aPeer)
Definition: PeerPoolMgr.cc:42
launches PeerPoolMgrs for peers configured with standby.limit
Definition: PeerPoolMgr.cc:248
virtual void syncConfig()
Definition: PeerPoolMgr.cc:258
virtual void useConfig()
Definition: PeerPoolMgr.cc:251
C * getRaw() const
Definition: RefCount.h:80
A simple PeerConnector for SSL/TLS cache_peers. No SslBump capabilities.
CbcPointer< ErrorState > error
problem details (nil on success)
Comm::ConnectionPointer conn
peer connection (secured on success)
bool tunneled
whether we spliced the connections instead of negotiating encryption
Callback dialer API to allow PeerConnector to set the answer.
Definition: PeerConnector.h:57
bool encryptTransport
whether transport encryption (TLS/SSL) is to be used on connections to the peer
Definition: PeerOptions.h:145
CachePeer * peers
Definition: SquidConfig.h:245
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:196
int fdUsageHigh(void)
Definition: fd.cc:272
#define fd_table
Definition: fde.h:189
int shutting_down
@ STANDBY_POOL
Definition: hier_code.h:37
@ PROTO_HTTP
Definition: ProtocolType.h:25
bool IsConnOpen(const Comm::ConnectionPointer &conn)
Definition: Connection.cc:27
@ OK
Definition: Flag.h:16
@ METHOD_OPTIONS
Definition: MethodType.h:31
Network/connection security abstraction layer.
Definition: Connection.h:34
SSL Connection
Definition: Session.h:45
int neighborUp(const CachePeer *p)
Definition: neighbors.cc:1163
time_t positiveTimeout(const time_t timeout)
Definition: neighbors.cc:1195
bool peerCanOpenMore(const CachePeer *p)
Whether we can open new connections to the peer (e.g., despite max-conn)
Definition: neighbors.cc:223
void peerConnectFailed(CachePeer *p)
Definition: neighbors.cc:1298
struct _request * request(char *urlin)
Definition: tcp-banger2.c:291

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors