]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/PeerPoolMgr.cc
2 * Copyright (C) 1996-2014 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"
26 #include "ssl/PeerConnector.h"
29 CBDATA_CLASS_INIT(PeerPoolMgr
);
32 /// Gives Ssl::PeerConnector access to Answer in the PeerPoolMgr callback dialer.
33 class MyAnswerDialer
: public UnaryMemFunT
<PeerPoolMgr
, Ssl::PeerConnectorAnswer
, Ssl::PeerConnectorAnswer
&>,
34 public Ssl::PeerConnector::CbDialer
37 MyAnswerDialer(const JobPointer
&aJob
, Method aMethod
):
38 UnaryMemFunT
<PeerPoolMgr
, Ssl::PeerConnectorAnswer
, Ssl::PeerConnectorAnswer
&>(aJob
, aMethod
, Ssl::PeerConnectorAnswer()) {}
40 /* Ssl::PeerConnector::CbDialer API */
41 virtual Ssl::PeerConnectorAnswer
&answer() { return arg1
; }
45 PeerPoolMgr::PeerPoolMgr(CachePeer
*aPeer
): AsyncJob("PeerPoolMgr"),
46 peer(cbdataReference(aPeer
)),
55 PeerPoolMgr::~PeerPoolMgr()
57 cbdataReferenceDone(peer
);
65 // ErrorState, getOutgoingAddress(), and other APIs may require a request.
66 // We fake one. TODO: Optionally send this request to peers?
67 request
= new HttpRequest(Http::METHOD_OPTIONS
, AnyP::PROTO_HTTP
, "*");
68 request
->SetHost(peer
->host
);
70 checkpoint("peer initialized");
74 PeerPoolMgr::swanSong()
80 PeerPoolMgr::validPeer() const
82 return peer
&& cbdataReferenceValid(peer
) && peer
->standby
.pool
;
86 PeerPoolMgr::doneAll() const
88 return !(validPeer() && peer
->standby
.limit
) && AsyncJob::doneAll();
92 PeerPoolMgr::handleOpenedConnection(const CommConnectCbParams
¶ms
)
97 debugs(48, 3, "peer gone");
98 if (params
.conn
!= NULL
)
103 if (params
.flag
!= Comm::OK
) {
104 /* it might have been a timeout with a partially open link */
105 if (params
.conn
!= NULL
)
106 params
.conn
->close();
107 peerConnectFailed(peer
);
108 checkpoint("conn opening failure"); // may retry
112 Must(params
.conn
!= NULL
);
117 typedef CommCbMemFunT
<PeerPoolMgr
, CommCloseCbParams
> CloserDialer
;
118 closer
= JobCallback(48, 3, CloserDialer
, this,
119 PeerPoolMgr::handleSecureClosure
);
120 comm_add_close_handler(params
.conn
->fd
, closer
);
122 securer
= asyncCall(48, 4, "PeerPoolMgr::handleSecuredPeer",
123 MyAnswerDialer(this, &PeerPoolMgr::handleSecuredPeer
));
125 const int peerTimeout
= peer
->connect_timeout
> 0 ?
126 peer
->connect_timeout
: Config
.Timeout
.peer_connect
;
127 const int timeUsed
= squid_curtime
- params
.conn
->startTime();
128 // Use positive timeout when less than one second is left for conn.
129 const int timeLeft
= max(1, (peerTimeout
- timeUsed
));
130 Ssl::PeerConnector
*connector
=
131 new Ssl::PeerConnector(request
, params
.conn
, NULL
, securer
, timeLeft
);
132 AsyncJob::Start(connector
); // will call our callback
137 pushNewConnection(params
.conn
);
141 PeerPoolMgr::pushNewConnection(const Comm::ConnectionPointer
&conn
)
144 Must(Comm::IsConnOpen(conn
));
145 peer
->standby
.pool
->push(conn
, NULL
/* domain */);
146 // push() will trigger a checkpoint()
151 PeerPoolMgr::handleSecuredPeer(Ssl::PeerConnectorAnswer
&answer
)
153 Must(securer
!= NULL
);
156 if (closer
!= NULL
) {
157 if (answer
.conn
!= NULL
)
158 comm_remove_close_handler(answer
.conn
->fd
, closer
);
160 closer
->cancel("securing completed");
165 debugs(48, 3, "peer gone");
166 if (answer
.conn
!= NULL
)
167 answer
.conn
->close();
171 if (answer
.error
.get()) {
172 if (answer
.conn
!= NULL
)
173 answer
.conn
->close();
174 // PeerConnector calls peerConnectFailed() for us;
175 checkpoint("conn securing failure"); // may retry
179 pushNewConnection(answer
.conn
);
183 PeerPoolMgr::handleSecureClosure(const CommCloseCbParams
¶ms
)
185 Must(closer
!= NULL
);
186 Must(securer
!= NULL
);
187 securer
->cancel("conn closed by a 3rd party");
190 // allow the closing connection to fully close before we check again
191 Checkpoint(this, "conn closure while securing");
196 PeerPoolMgr::openNewConnection()
198 // KISS: Do nothing else when we are already doing something.
199 if (opener
!= NULL
|| securer
!= NULL
|| shutting_down
) {
200 debugs(48, 7, "busy: " << opener
<< '|' << securer
<< '|' << shutting_down
);
201 return; // there will be another checkpoint when we are done opening/securing
204 // Do not talk to a peer until it is ready.
205 if (!neighborUp(peer
)) // provides debugging
206 return; // there will be another checkpoint when peer is up
208 // Do not violate peer limits.
209 if (!peerCanOpenMore(peer
)) { // provides debugging
210 peer
->standby
.waitingForClose
= true; // may already be true
211 return; // there will be another checkpoint when a peer conn closes
214 // Do not violate global restrictions.
216 debugs(48, 7, "overwhelmed");
217 peer
->standby
.waitingForClose
= true; // may already be true
218 // There will be another checkpoint when a peer conn closes OR when
219 // a future pop() fails due to an empty pool. See PconnPool::pop().
223 peer
->standby
.waitingForClose
= false;
225 Comm::ConnectionPointer conn
= new Comm::Connection
;
226 Must(peer
->n_addresses
); // guaranteed by neighborUp() above
227 // cycle through all available IP addresses
228 conn
->remote
= peer
->addresses
[addrUsed
++ % peer
->n_addresses
];
229 conn
->remote
.port(peer
->http_port
);
230 conn
->peerType
= STANDBY_POOL
; // should be reset by peerSelect()
232 getOutgoingAddress(request
.getRaw(), conn
);
233 GetMarkingsToServer(request
.getRaw(), *conn
);
235 const int ctimeout
= peer
->connect_timeout
> 0 ?
236 peer
->connect_timeout
: Config
.Timeout
.peer_connect
;
237 typedef CommCbMemFunT
<PeerPoolMgr
, CommConnectCbParams
> Dialer
;
238 opener
= JobCallback(48, 5, Dialer
, this, PeerPoolMgr::handleOpenedConnection
);
239 Comm::ConnOpener
*cs
= new Comm::ConnOpener(conn
, opener
, ctimeout
);
244 PeerPoolMgr::closeOldConnections(const int howMany
)
246 debugs(48, 8, howMany
);
247 peer
->standby
.pool
->closeN(howMany
);
251 PeerPoolMgr::checkpoint(const char *reason
)
254 debugs(48, 3, reason
<< " and peer gone");
255 return; // nothing to do after our owner dies; the job will quit
258 const int count
= peer
->standby
.pool
->count();
259 const int limit
= peer
->standby
.limit
;
260 debugs(48, 7, reason
<< " with " << count
<< " ? " << limit
);
264 else if (count
> limit
)
265 closeOldConnections(count
- limit
);
269 PeerPoolMgr::Checkpoint(const Pointer
&mgr
, const char *reason
)
271 CallJobHere1(48, 5, mgr
, PeerPoolMgr
, checkpoint
, reason
);
274 /// launches PeerPoolMgrs for peers configured with standby.limit
275 class PeerPoolMgrsRr
: public RegisteredRunner
278 /* RegisteredRunner API */
279 virtual void useConfig() { syncConfig(); }
280 virtual void syncConfig();
283 RunnerRegistrationEntry(PeerPoolMgrsRr
);
286 PeerPoolMgrsRr::syncConfig()
288 for (CachePeer
*p
= Config
.peers
; p
; p
= p
->next
) {
289 // On reconfigure, Squid deletes the old config (and old peers in it),
290 // so should always be dealing with a brand new configuration.
291 assert(!p
->standby
.mgr
);
292 assert(!p
->standby
.pool
);
293 if (p
->standby
.limit
) {
294 p
->standby
.mgr
= new PeerPoolMgr(p
);
295 p
->standby
.pool
= new PconnPool(p
->name
, p
->standby
.mgr
);
296 AsyncJob::Start(p
->standby
.mgr
.get());