]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/PeerPoolMgr.cc
2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
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.
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"
20 #include "HttpRequest.h"
21 #include "neighbors.h"
23 #include "PeerPoolMgr.h"
24 #include "security/BlindPeerConnector.h"
25 #include "SquidConfig.h"
26 #include "SquidTime.h"
28 CBDATA_CLASS_INIT(PeerPoolMgr
);
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
35 MyAnswerDialer(const JobPointer
&aJob
, Method aMethod
):
36 UnaryMemFunT
<PeerPoolMgr
, Security::EncryptorAnswer
, Security::EncryptorAnswer
&>(aJob
, aMethod
, Security::EncryptorAnswer()) {}
38 /* Security::PeerConnector::CbDialer API */
39 virtual Security::EncryptorAnswer
&answer() { return arg1
; }
42 PeerPoolMgr::PeerPoolMgr(CachePeer
*aPeer
): AsyncJob("PeerPoolMgr"),
43 peer(cbdataReference(aPeer
)),
52 PeerPoolMgr::~PeerPoolMgr()
54 cbdataReferenceDone(peer
);
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
, "http", "*");
65 request
->url
.host(peer
->host
);
67 checkpoint("peer initialized");
71 PeerPoolMgr::swanSong()
77 PeerPoolMgr::validPeer() const
79 return peer
&& cbdataReferenceValid(peer
) && peer
->standby
.pool
;
83 PeerPoolMgr::doneAll() const
85 return !(validPeer() && peer
->standby
.limit
) && AsyncJob::doneAll();
89 PeerPoolMgr::handleOpenedConnection(const CommConnectCbParams
¶ms
)
94 debugs(48, 3, "peer gone");
95 if (params
.conn
!= NULL
)
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
109 Must(params
.conn
!= NULL
);
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
);
118 securer
= asyncCall(48, 4, "PeerPoolMgr::handleSecuredPeer",
119 MyAnswerDialer(this, &PeerPoolMgr::handleSecuredPeer
));
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
131 pushNewConnection(params
.conn
);
135 PeerPoolMgr::pushNewConnection(const Comm::ConnectionPointer
&conn
)
138 Must(Comm::IsConnOpen(conn
));
139 peer
->standby
.pool
->push(conn
, NULL
/* domain */);
140 // push() will trigger a checkpoint()
144 PeerPoolMgr::handleSecuredPeer(Security::EncryptorAnswer
&answer
)
146 Must(securer
!= NULL
);
149 if (closer
!= NULL
) {
150 if (answer
.conn
!= NULL
)
151 comm_remove_close_handler(answer
.conn
->fd
, closer
);
153 closer
->cancel("securing completed");
158 debugs(48, 3, "peer gone");
159 if (answer
.conn
!= NULL
)
160 answer
.conn
->close();
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
172 pushNewConnection(answer
.conn
);
176 PeerPoolMgr::handleSecureClosure(const CommCloseCbParams
¶ms
)
178 Must(closer
!= NULL
);
179 Must(securer
!= NULL
);
180 securer
->cancel("conn closed by a 3rd party");
183 // allow the closing connection to fully close before we check again
184 Checkpoint(this, "conn closure while securing");
188 PeerPoolMgr::openNewConnection()
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
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
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
206 // Do not violate global restrictions.
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().
215 peer
->standby
.waitingForClose
= false;
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()
224 getOutgoingAddress(request
.getRaw(), conn
);
225 GetMarkingsToServer(request
.getRaw(), *conn
);
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
);
236 PeerPoolMgr::closeOldConnections(const int howMany
)
238 debugs(48, 8, howMany
);
239 peer
->standby
.pool
->closeN(howMany
);
243 PeerPoolMgr::checkpoint(const char *reason
)
246 debugs(48, 3, reason
<< " and peer gone");
247 return; // nothing to do after our owner dies; the job will quit
250 const int count
= peer
->standby
.pool
->count();
251 const int limit
= peer
->standby
.limit
;
252 debugs(48, 7, reason
<< " with " << count
<< " ? " << limit
);
256 else if (count
> limit
)
257 closeOldConnections(count
- limit
);
261 PeerPoolMgr::Checkpoint(const Pointer
&mgr
, const char *reason
)
263 CallJobHere1(48, 5, mgr
, PeerPoolMgr
, checkpoint
, reason
);
266 /// launches PeerPoolMgrs for peers configured with standby.limit
267 class PeerPoolMgrsRr
: public RegisteredRunner
270 /* RegisteredRunner API */
271 virtual void useConfig() { syncConfig(); }
272 virtual void syncConfig();
275 RunnerRegistrationEntry(PeerPoolMgrsRr
);
278 PeerPoolMgrsRr::syncConfig()
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());