]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/PeerPoolMgr.cc
2 * Copyright (C) 1996-2016 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 "base/AsyncJobCalls.h"
11 #include "base/RunnersRegistry.h"
12 #include "CachePeer.h"
13 #include "comm/Connection.h"
14 #include "comm/ConnOpener.h"
19 #include "HttpRequest.h"
20 #include "neighbors.h"
22 #include "PeerPoolMgr.h"
23 #include "SquidConfig.h"
24 #include "SquidTime.h"
25 #include "ssl/BlindPeerConnector.h"
27 CBDATA_CLASS_INIT(PeerPoolMgr
);
30 /// Gives Ssl::PeerConnector access to Answer in the PeerPoolMgr callback dialer.
31 class MyAnswerDialer
: public UnaryMemFunT
<PeerPoolMgr
, Security::EncryptorAnswer
, Security::EncryptorAnswer
&>,
32 public Ssl::PeerConnector::CbDialer
35 MyAnswerDialer(const JobPointer
&aJob
, Method aMethod
):
36 UnaryMemFunT
<PeerPoolMgr
, Security::EncryptorAnswer
, Security::EncryptorAnswer
&>(aJob
, aMethod
, Security::EncryptorAnswer()) {}
38 /* Ssl::PeerConnector::CbDialer API */
39 virtual Security::EncryptorAnswer
&answer() { return arg1
; }
43 PeerPoolMgr::PeerPoolMgr(CachePeer
*aPeer
): AsyncJob("PeerPoolMgr"),
44 peer(cbdataReference(aPeer
)),
53 PeerPoolMgr::~PeerPoolMgr()
55 cbdataReferenceDone(peer
);
63 // ErrorState, getOutgoingAddress(), and other APIs may require a request.
64 // We fake one. TODO: Optionally send this request to peers?
65 request
= new HttpRequest(Http::METHOD_OPTIONS
, AnyP::PROTO_HTTP
, "*");
66 request
->url
.host(peer
->host
);
68 checkpoint("peer initialized");
72 PeerPoolMgr::swanSong()
78 PeerPoolMgr::validPeer() const
80 return peer
&& cbdataReferenceValid(peer
) && peer
->standby
.pool
;
84 PeerPoolMgr::doneAll() const
86 return !(validPeer() && peer
->standby
.limit
) && AsyncJob::doneAll();
90 PeerPoolMgr::handleOpenedConnection(const CommConnectCbParams
¶ms
)
95 debugs(48, 3, "peer gone");
96 if (params
.conn
!= NULL
)
101 if (params
.flag
!= Comm::OK
) {
102 /* it might have been a timeout with a partially open link */
103 if (params
.conn
!= NULL
)
104 params
.conn
->close();
105 peerConnectFailed(peer
);
106 checkpoint("conn opening failure"); // may retry
110 Must(params
.conn
!= NULL
);
114 if (peer
->secure
.encryptTransport
) {
115 typedef CommCbMemFunT
<PeerPoolMgr
, CommCloseCbParams
> CloserDialer
;
116 closer
= JobCallback(48, 3, CloserDialer
, this,
117 PeerPoolMgr::handleSecureClosure
);
118 comm_add_close_handler(params
.conn
->fd
, closer
);
120 securer
= asyncCall(48, 4, "PeerPoolMgr::handleSecuredPeer",
121 MyAnswerDialer(this, &PeerPoolMgr::handleSecuredPeer
));
123 const int peerTimeout
= peer
->connect_timeout
> 0 ?
124 peer
->connect_timeout
: Config
.Timeout
.peer_connect
;
125 const int timeUsed
= squid_curtime
- params
.conn
->startTime();
126 // Use positive timeout when less than one second is left for conn.
127 const int timeLeft
= max(1, (peerTimeout
- timeUsed
));
128 Ssl::BlindPeerConnector
*connector
=
129 new Ssl::BlindPeerConnector(request
, params
.conn
, securer
, timeLeft
);
130 AsyncJob::Start(connector
); // will call our callback
135 pushNewConnection(params
.conn
);
139 PeerPoolMgr::pushNewConnection(const Comm::ConnectionPointer
&conn
)
142 Must(Comm::IsConnOpen(conn
));
143 peer
->standby
.pool
->push(conn
, NULL
/* domain */);
144 // push() will trigger a checkpoint()
148 PeerPoolMgr::handleSecuredPeer(Security::EncryptorAnswer
&answer
)
150 Must(securer
!= NULL
);
153 if (closer
!= NULL
) {
154 if (answer
.conn
!= NULL
)
155 comm_remove_close_handler(answer
.conn
->fd
, closer
);
157 closer
->cancel("securing completed");
162 debugs(48, 3, "peer gone");
163 if (answer
.conn
!= NULL
)
164 answer
.conn
->close();
168 if (answer
.error
.get()) {
169 if (answer
.conn
!= NULL
)
170 answer
.conn
->close();
171 // PeerConnector calls peerConnectFailed() for us;
172 checkpoint("conn securing failure"); // may retry
176 pushNewConnection(answer
.conn
);
180 PeerPoolMgr::handleSecureClosure(const CommCloseCbParams
¶ms
)
182 Must(closer
!= NULL
);
183 Must(securer
!= NULL
);
184 securer
->cancel("conn closed by a 3rd party");
187 // allow the closing connection to fully close before we check again
188 Checkpoint(this, "conn closure while securing");
192 PeerPoolMgr::openNewConnection()
194 // KISS: Do nothing else when we are already doing something.
195 if (opener
!= NULL
|| securer
!= NULL
|| shutting_down
) {
196 debugs(48, 7, "busy: " << opener
<< '|' << securer
<< '|' << shutting_down
);
197 return; // there will be another checkpoint when we are done opening/securing
200 // Do not talk to a peer until it is ready.
201 if (!neighborUp(peer
)) // provides debugging
202 return; // there will be another checkpoint when peer is up
204 // Do not violate peer limits.
205 if (!peerCanOpenMore(peer
)) { // provides debugging
206 peer
->standby
.waitingForClose
= true; // may already be true
207 return; // there will be another checkpoint when a peer conn closes
210 // Do not violate global restrictions.
212 debugs(48, 7, "overwhelmed");
213 peer
->standby
.waitingForClose
= true; // may already be true
214 // There will be another checkpoint when a peer conn closes OR when
215 // a future pop() fails due to an empty pool. See PconnPool::pop().
219 peer
->standby
.waitingForClose
= false;
221 Comm::ConnectionPointer conn
= new Comm::Connection
;
222 Must(peer
->n_addresses
); // guaranteed by neighborUp() above
223 // cycle through all available IP addresses
224 conn
->remote
= peer
->addresses
[addrUsed
++ % peer
->n_addresses
];
225 conn
->remote
.port(peer
->http_port
);
226 conn
->peerType
= STANDBY_POOL
; // should be reset by peerSelect()
228 getOutgoingAddress(request
.getRaw(), conn
);
229 GetMarkingsToServer(request
.getRaw(), *conn
);
231 const int ctimeout
= peer
->connect_timeout
> 0 ?
232 peer
->connect_timeout
: Config
.Timeout
.peer_connect
;
233 typedef CommCbMemFunT
<PeerPoolMgr
, CommConnectCbParams
> Dialer
;
234 opener
= JobCallback(48, 5, Dialer
, this, PeerPoolMgr::handleOpenedConnection
);
235 Comm::ConnOpener
*cs
= new Comm::ConnOpener(conn
, opener
, ctimeout
);
240 PeerPoolMgr::closeOldConnections(const int howMany
)
242 debugs(48, 8, howMany
);
243 peer
->standby
.pool
->closeN(howMany
);
247 PeerPoolMgr::checkpoint(const char *reason
)
250 debugs(48, 3, reason
<< " and peer gone");
251 return; // nothing to do after our owner dies; the job will quit
254 const int count
= peer
->standby
.pool
->count();
255 const int limit
= peer
->standby
.limit
;
256 debugs(48, 7, reason
<< " with " << count
<< " ? " << limit
);
260 else if (count
> limit
)
261 closeOldConnections(count
- limit
);
265 PeerPoolMgr::Checkpoint(const Pointer
&mgr
, const char *reason
)
267 CallJobHere1(48, 5, mgr
, PeerPoolMgr
, checkpoint
, reason
);
270 /// launches PeerPoolMgrs for peers configured with standby.limit
271 class PeerPoolMgrsRr
: public RegisteredRunner
274 /* RegisteredRunner API */
275 virtual void useConfig() { syncConfig(); }
276 virtual void syncConfig();
279 RunnerRegistrationEntry(PeerPoolMgrsRr
);
282 PeerPoolMgrsRr::syncConfig()
284 for (CachePeer
*p
= Config
.peers
; p
; p
= p
->next
) {
285 // On reconfigure, Squid deletes the old config (and old peers in it),
286 // so should always be dealing with a brand new configuration.
287 assert(!p
->standby
.mgr
);
288 assert(!p
->standby
.pool
);
289 if (p
->standby
.limit
) {
290 p
->standby
.mgr
= new PeerPoolMgr(p
);
291 p
->standby
.pool
= new PconnPool(p
->name
, p
->standby
.mgr
);
292 AsyncJob::Start(p
->standby
.mgr
.get());