PeerPoolMgr.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 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/AsyncCallbacks.h"
13#include "CachePeer.h"
14#include "CachePeers.h"
15#include "comm/Connection.h"
16#include "comm/ConnOpener.h"
17#include "debug/Stream.h"
18#include "fd.h"
19#include "FwdState.h"
20#include "globals.h"
21#include "HttpRequest.h"
22#include "MasterXaction.h"
23#include "neighbors.h"
24#include "pconn.h"
25#include "PeerPoolMgr.h"
27#include "SquidConfig.h"
28
30
32 peer(cbdataReference(aPeer)),
33 request(),
34 transportWait(),
35 encryptionWait(),
36 addrUsed(0)
37{
38}
39
41{
43}
44
45void
47{
49
50 const auto mx = MasterXaction::MakePortless<XactionInitiator::initPeerPool>();
51 // ErrorState, getOutgoingAddress(), and other APIs may require a request.
52 // We fake one. TODO: Optionally send this request to peers?
55
56 checkpoint("peer initialized");
57}
58
59void
61{
63}
64
65bool
67{
69}
70
71bool
73{
74 return !(validPeer() && peer->standby.limit) && AsyncJob::doneAll();
75}
76
77void
79{
81
82 if (!validPeer()) {
83 debugs(48, 3, "peer gone");
84 if (params.conn != nullptr)
85 params.conn->close();
86 return;
87 }
88
89 if (params.flag != Comm::OK) {
91 checkpoint("conn opening failure"); // may retry
92 return;
93 }
94
95 Must(params.conn != nullptr);
96
97 // Handle TLS peers.
99 // XXX: Exceptions orphan params.conn
100 const auto callback = asyncCallback(48, 4, PeerPoolMgr::handleSecuredPeer, this);
101
102 const auto peerTimeout = peer->connectTimeout();
103 const int timeUsed = squid_curtime - params.conn->startTime();
104 // Use positive timeout when less than one second is left for conn.
105 const int timeLeft = positiveTimeout(peerTimeout - timeUsed);
106 const auto connector = new Security::BlindPeerConnector(request, params.conn, callback, nullptr, timeLeft);
107 encryptionWait.start(connector, callback);
108 return;
109 }
110
111 pushNewConnection(params.conn);
112}
113
114void
116{
117 Must(validPeer());
119 peer->standby.pool->push(conn, nullptr /* domain */);
120 // push() will trigger a checkpoint()
121}
122
123void
125{
127
128 if (!validPeer()) {
129 debugs(48, 3, "peer gone");
130 if (answer.conn != nullptr)
131 answer.conn->close();
132 return;
133 }
134
135 assert(!answer.tunneled);
136 if (answer.error.get()) {
137 assert(!answer.conn);
138 // PeerConnector calls NoteOutgoingConnectionFailure() for us
139 checkpoint("conn securing failure"); // may retry
140 return;
141 }
142
143 assert(answer.conn);
144
145 // The socket could get closed while our callback was queued. Sync
146 // Connection. XXX: Connection::fd may already be stale/invalid here.
147 if (answer.conn->isOpen() && fd_table[answer.conn->fd].closing()) {
148 answer.conn->noteClosure();
149 checkpoint("external connection closure"); // may retry
150 return;
151 }
152
153 pushNewConnection(answer.conn);
154}
155
156void
158{
159 // KISS: Do nothing else when we are already doing something.
161 debugs(48, 7, "busy: " << transportWait << '|' << encryptionWait << '|' << shutting_down);
162 return; // there will be another checkpoint when we are done opening/securing
163 }
164
165 // Do not talk to a peer until it is ready.
166 if (!neighborUp(peer)) // provides debugging
167 return; // there will be another checkpoint when peer is up
168
169 // Do not violate peer limits.
170 if (!peerCanOpenMore(peer)) { // provides debugging
171 peer->standby.waitingForClose = true; // may already be true
172 return; // there will be another checkpoint when a peer conn closes
173 }
174
175 // Do not violate global restrictions.
176 if (fdUsageHigh()) {
177 debugs(48, 7, "overwhelmed");
178 peer->standby.waitingForClose = true; // may already be true
179 // There will be another checkpoint when a peer conn closes OR when
180 // a future pop() fails due to an empty pool. See PconnPool::pop().
181 return;
182 }
183
185
187 Must(peer->n_addresses); // guaranteed by neighborUp() above
188 // cycle through all available IP addresses
189 conn->remote = peer->addresses[addrUsed++ % peer->n_addresses];
190 conn->remote.port(peer->http_port);
191 conn->peerType = STANDBY_POOL; // should be reset by peerSelect()
192 conn->setPeer(peer);
195
196 const auto ctimeout = peer->connectTimeout();
199 const auto cs = new Comm::ConnOpener(conn, callback, ctimeout);
200 transportWait.start(cs, callback);
201}
202
203void
205{
206 debugs(48, 8, howMany);
207 peer->standby.pool->closeN(howMany);
208}
209
210void
211PeerPoolMgr::checkpoint(const char *reason)
212{
213 if (!validPeer()) {
214 debugs(48, 3, reason << " and peer gone");
215 return; // nothing to do after our owner dies; the job will quit
216 }
217
218 const int count = peer->standby.pool->count();
219 const int limit = peer->standby.limit;
220 debugs(48, 7, reason << " with " << count << " ? " << limit);
221
222 if (count < limit)
224 else if (count > limit)
225 closeOldConnections(count - limit);
226}
227
228void
229PeerPoolMgr::Checkpoint(const Pointer &mgr, const char *reason)
230{
231 CallJobHere1(48, 5, mgr, PeerPoolMgr, checkpoint, reason);
232}
233
236{
237public:
238 /* RegisteredRunner API */
239 void useConfig() override { syncConfig(); }
240 void syncConfig() override;
241};
242
244
245void
247{
248 for (const auto &peer: CurrentCachePeers()) {
249 const auto p = peer.get();
250 // On reconfigure, Squid deletes the old config (and old peers in it),
251 // so should always be dealing with a brand new configuration.
252 assert(!p->standby.mgr);
253 assert(!p->standby.pool);
254 if (p->standby.limit) {
255 p->standby.mgr = new PeerPoolMgr(p);
256 p->standby.pool = new PconnPool(p->name, p->standby.mgr);
257 AsyncJob::Start(p->standby.mgr.get());
258 }
259 }
260}
261
#define asyncCallback(dbgSection, dbgLevel, method, object)
#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 NoteOutgoingConnectionFailure(CachePeer *const peer, const Http::StatusCode code)
Definition: CachePeer.h:243
const CachePeers & CurrentCachePeers()
Definition: CachePeers.cc:41
void getOutgoingAddress(HttpRequest *request, const Comm::ConnectionPointer &conn)
Definition: FwdState.cc:1476
void GetMarkingsToServer(HttpRequest *request, Comm::Connection &conn)
Definition: FwdState.cc:1555
DefineRunnerRegistrator(PeerPoolMgrsRr)
time_t squid_curtime
Definition: stub_libtime.cc:20
#define Must(condition)
Definition: TextException.h:75
int conn
the current server connection FD
Definition: Transport.cc:26
#define assert(EX)
Definition: assert.h:17
int cbdataReferenceValid(const void *p)
Definition: cbdata.cc:265
#define cbdataReferenceDone(var)
Definition: cbdata.h:352
#define cbdataReference(var)
Definition: cbdata.h:343
#define CBDATA_CLASS_INIT(type)
Definition: cbdata.h:320
void host(const char *src)
Definition: Uri.cc:100
static void Start(const Pointer &job)
Definition: AsyncJob.cc:37
virtual bool doneAll() const
whether positive goal has been reached
Definition: AsyncJob.cc:112
virtual void start()
called by AsyncStart; do not call directly
Definition: AsyncJob.cc:59
virtual void swanSong()
Definition: AsyncJob.h:61
int n_addresses
Definition: CachePeer.h:181
unsigned short http_port
Definition: CachePeer.h:103
char * host
Definition: CachePeer.h:65
Security::PeerOptions secure
security settings for peer connection
Definition: CachePeer.h:219
int limit
the limit itself
Definition: CachePeer.h:212
PconnPool * pool
idle connection pool for this peer
Definition: CachePeer.h:210
bool waitingForClose
a conn must close before we open a standby conn
Definition: CachePeer.h:213
struct CachePeer::@33 standby
optional "cache_peer standby=limit" feature
time_t connectTimeout() const
Definition: CachePeer.cc:119
Ip::Address addresses[10]
Definition: CachePeer.h:180
Cbc * get() const
a temporary valid raw Cbc pointer or NULL
Definition: CbcPointer.h:159
Comm::Flag flag
comm layer result status.
Definition: CommCalls.h:82
Comm::ConnectionPointer conn
Definition: CommCalls.h:80
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
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
void checkpoint(const char *reason)
Definition: PeerPoolMgr.cc:211
void handleSecuredPeer(Security::EncryptorAnswer &answer)
Security::PeerConnector callback.
Definition: PeerPoolMgr.cc:124
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:157
void swanSong() override
Definition: PeerPoolMgr.cc:60
void start() override
called by AsyncStart; do not call directly
Definition: PeerPoolMgr.cc:46
~PeerPoolMgr() override
Definition: PeerPoolMgr.cc:40
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:78
void closeOldConnections(const int howMany)
closes 'howMany' standby connections
Definition: PeerPoolMgr.cc:204
static void Checkpoint(const Pointer &mgr, const char *reason)
Definition: PeerPoolMgr.cc:229
bool doneAll() const override
whether positive goal has been reached
Definition: PeerPoolMgr.cc:72
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:115
bool validPeer() const
whether the peer is still out there and in a valid state we can safely use
Definition: PeerPoolMgr.cc:66
PeerPoolMgr(CachePeer *aPeer)
Definition: PeerPoolMgr.cc:31
launches PeerPoolMgrs for peers configured with standby.limit
Definition: PeerPoolMgr.cc:236
void syncConfig() override
Definition: PeerPoolMgr.cc:246
void useConfig() override
Definition: PeerPoolMgr.cc:239
C * getRaw() const
Definition: RefCount.h:89
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
bool encryptTransport
whether transport encryption (TLS/SSL) is to be used on connections to the peer
Definition: PeerOptions.h:147
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:194
int fdUsageHigh(void)
Definition: fd.cc:271
#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
@ scNone
Definition: StatusCode.h:21
@ METHOD_OPTIONS
Definition: MethodType.h:31
SSL Connection
Definition: Session.h:45
int neighborUp(const CachePeer *p)
Definition: neighbors.cc:1060
time_t positiveTimeout(const time_t timeout)
Definition: neighbors.cc:1095
bool peerCanOpenMore(const CachePeer *p)
Whether we can open new connections to the peer (e.g., despite max-conn)
Definition: neighbors.cc:222

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors