]> git.ipfire.org Git - thirdparty/squid.git/blob - src/PeerPoolMgr.cc
merge from trunk-r14768
[thirdparty/squid.git] / src / PeerPoolMgr.cc
1 /*
2 * Copyright (C) 1996-2016 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"
12 #include "base/RunnersRegistry.h"
13 #include "CachePeer.h"
14 #include "comm/Connection.h"
15 #include "comm/ConnOpener.h"
16 #include "Debug.h"
17 #include "fd.h"
18 #include "FwdState.h"
19 #include "globals.h"
20 #include "HttpRequest.h"
21 #include "neighbors.h"
22 #include "pconn.h"
23 #include "PeerPoolMgr.h"
24 #include "security/BlindPeerConnector.h"
25 #include "SquidConfig.h"
26 #include "SquidTime.h"
27
28 CBDATA_CLASS_INIT(PeerPoolMgr);
29
30 /// Gives Security::PeerConnector access to Answer in the PeerPoolMgr callback dialer.
31 class MyAnswerDialer: public UnaryMemFunT<PeerPoolMgr, Security::EncryptorAnswer, Security::EncryptorAnswer&>,
32 public Security::PeerConnector::CbDialer
33 {
34 public:
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
42 PeerPoolMgr::PeerPoolMgr(CachePeer *aPeer): AsyncJob("PeerPoolMgr"),
43 peer(cbdataReference(aPeer)),
44 request(),
45 opener(),
46 securer(),
47 closer(),
48 addrUsed(0)
49 {
50 }
51
52 PeerPoolMgr::~PeerPoolMgr()
53 {
54 cbdataReferenceDone(peer);
55 }
56
57 void
58 PeerPoolMgr::start()
59 {
60 AsyncJob::start();
61
62 // ErrorState, getOutgoingAddress(), and other APIs may require a request.
63 // We fake one. TODO: Optionally send this request to peers?
64 request = new HttpRequest(Http::METHOD_OPTIONS, AnyP::PROTO_HTTP, "*");
65 request->url.host(peer->host);
66
67 checkpoint("peer initialized");
68 }
69
70 void
71 PeerPoolMgr::swanSong()
72 {
73 AsyncJob::swanSong();
74 }
75
76 bool
77 PeerPoolMgr::validPeer() const
78 {
79 return peer && cbdataReferenceValid(peer) && peer->standby.pool;
80 }
81
82 bool
83 PeerPoolMgr::doneAll() const
84 {
85 return !(validPeer() && peer->standby.limit) && AsyncJob::doneAll();
86 }
87
88 void
89 PeerPoolMgr::handleOpenedConnection(const CommConnectCbParams &params)
90 {
91 opener = NULL;
92
93 if (!validPeer()) {
94 debugs(48, 3, "peer gone");
95 if (params.conn != NULL)
96 params.conn->close();
97 return;
98 }
99
100 if (params.flag != Comm::OK) {
101 /* it might have been a timeout with a partially open link */
102 if (params.conn != NULL)
103 params.conn->close();
104 peerConnectFailed(peer);
105 checkpoint("conn opening failure"); // may retry
106 return;
107 }
108
109 Must(params.conn != NULL);
110
111 // Handle TLS peers.
112 if (peer->secure.encryptTransport) {
113 typedef CommCbMemFunT<PeerPoolMgr, CommCloseCbParams> CloserDialer;
114 closer = JobCallback(48, 3, CloserDialer, this,
115 PeerPoolMgr::handleSecureClosure);
116 comm_add_close_handler(params.conn->fd, closer);
117
118 securer = asyncCall(48, 4, "PeerPoolMgr::handleSecuredPeer",
119 MyAnswerDialer(this, &PeerPoolMgr::handleSecuredPeer));
120
121 const int peerTimeout = peer->connect_timeout > 0 ?
122 peer->connect_timeout : Config.Timeout.peer_connect;
123 const int timeUsed = squid_curtime - params.conn->startTime();
124 // Use positive timeout when less than one second is left for conn.
125 const int timeLeft = max(1, (peerTimeout - timeUsed));
126 auto *connector = new Security::BlindPeerConnector(request, params.conn, securer, nullptr, timeLeft);
127 AsyncJob::Start(connector); // will call our callback
128 return;
129 }
130
131 pushNewConnection(params.conn);
132 }
133
134 void
135 PeerPoolMgr::pushNewConnection(const Comm::ConnectionPointer &conn)
136 {
137 Must(validPeer());
138 Must(Comm::IsConnOpen(conn));
139 peer->standby.pool->push(conn, NULL /* domain */);
140 // push() will trigger a checkpoint()
141 }
142
143 void
144 PeerPoolMgr::handleSecuredPeer(Security::EncryptorAnswer &answer)
145 {
146 Must(securer != NULL);
147 securer = NULL;
148
149 if (closer != NULL) {
150 if (answer.conn != NULL)
151 comm_remove_close_handler(answer.conn->fd, closer);
152 else
153 closer->cancel("securing completed");
154 closer = NULL;
155 }
156
157 if (!validPeer()) {
158 debugs(48, 3, "peer gone");
159 if (answer.conn != NULL)
160 answer.conn->close();
161 return;
162 }
163
164 if (answer.error.get()) {
165 if (answer.conn != NULL)
166 answer.conn->close();
167 // PeerConnector calls peerConnectFailed() for us;
168 checkpoint("conn securing failure"); // may retry
169 return;
170 }
171
172 pushNewConnection(answer.conn);
173 }
174
175 void
176 PeerPoolMgr::handleSecureClosure(const CommCloseCbParams &params)
177 {
178 Must(closer != NULL);
179 Must(securer != NULL);
180 securer->cancel("conn closed by a 3rd party");
181 securer = NULL;
182 closer = NULL;
183 // allow the closing connection to fully close before we check again
184 Checkpoint(this, "conn closure while securing");
185 }
186
187 void
188 PeerPoolMgr::openNewConnection()
189 {
190 // KISS: Do nothing else when we are already doing something.
191 if (opener != NULL || securer != NULL || shutting_down) {
192 debugs(48, 7, "busy: " << opener << '|' << securer << '|' << shutting_down);
193 return; // there will be another checkpoint when we are done opening/securing
194 }
195
196 // Do not talk to a peer until it is ready.
197 if (!neighborUp(peer)) // provides debugging
198 return; // there will be another checkpoint when peer is up
199
200 // Do not violate peer limits.
201 if (!peerCanOpenMore(peer)) { // provides debugging
202 peer->standby.waitingForClose = true; // may already be true
203 return; // there will be another checkpoint when a peer conn closes
204 }
205
206 // Do not violate global restrictions.
207 if (fdUsageHigh()) {
208 debugs(48, 7, "overwhelmed");
209 peer->standby.waitingForClose = true; // may already be true
210 // There will be another checkpoint when a peer conn closes OR when
211 // a future pop() fails due to an empty pool. See PconnPool::pop().
212 return;
213 }
214
215 peer->standby.waitingForClose = false;
216
217 Comm::ConnectionPointer conn = new Comm::Connection;
218 Must(peer->n_addresses); // guaranteed by neighborUp() above
219 // cycle through all available IP addresses
220 conn->remote = peer->addresses[addrUsed++ % peer->n_addresses];
221 conn->remote.port(peer->http_port);
222 conn->peerType = STANDBY_POOL; // should be reset by peerSelect()
223 conn->setPeer(peer);
224 getOutgoingAddress(request.getRaw(), conn);
225 GetMarkingsToServer(request.getRaw(), *conn);
226
227 const int ctimeout = peer->connect_timeout > 0 ?
228 peer->connect_timeout : Config.Timeout.peer_connect;
229 typedef CommCbMemFunT<PeerPoolMgr, CommConnectCbParams> Dialer;
230 opener = JobCallback(48, 5, Dialer, this, PeerPoolMgr::handleOpenedConnection);
231 Comm::ConnOpener *cs = new Comm::ConnOpener(conn, opener, ctimeout);
232 AsyncJob::Start(cs);
233 }
234
235 void
236 PeerPoolMgr::closeOldConnections(const int howMany)
237 {
238 debugs(48, 8, howMany);
239 peer->standby.pool->closeN(howMany);
240 }
241
242 void
243 PeerPoolMgr::checkpoint(const char *reason)
244 {
245 if (!validPeer()) {
246 debugs(48, 3, reason << " and peer gone");
247 return; // nothing to do after our owner dies; the job will quit
248 }
249
250 const int count = peer->standby.pool->count();
251 const int limit = peer->standby.limit;
252 debugs(48, 7, reason << " with " << count << " ? " << limit);
253
254 if (count < limit)
255 openNewConnection();
256 else if (count > limit)
257 closeOldConnections(count - limit);
258 }
259
260 void
261 PeerPoolMgr::Checkpoint(const Pointer &mgr, const char *reason)
262 {
263 CallJobHere1(48, 5, mgr, PeerPoolMgr, checkpoint, reason);
264 }
265
266 /// launches PeerPoolMgrs for peers configured with standby.limit
267 class PeerPoolMgrsRr: public RegisteredRunner
268 {
269 public:
270 /* RegisteredRunner API */
271 virtual void useConfig() { syncConfig(); }
272 virtual void syncConfig();
273 };
274
275 RunnerRegistrationEntry(PeerPoolMgrsRr);
276
277 void
278 PeerPoolMgrsRr::syncConfig()
279 {
280 for (CachePeer *p = Config.peers; p; p = p->next) {
281 // On reconfigure, Squid deletes the old config (and old peers in it),
282 // so should always be dealing with a brand new configuration.
283 assert(!p->standby.mgr);
284 assert(!p->standby.pool);
285 if (p->standby.limit) {
286 p->standby.mgr = new PeerPoolMgr(p);
287 p->standby.pool = new PconnPool(p->name, p->standby.mgr);
288 AsyncJob::Start(p->standby.mgr.get());
289 }
290 }
291 }
292