]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/PeerPoolMgr.cc
2 #include "base/AsyncJobCalls.h"
3 #include "base/RunnersRegistry.h"
5 #include "comm/Connection.h"
6 #include "comm/ConnOpener.h"
11 #include "HttpRequest.h"
12 #include "neighbors.h"
14 #include "PeerPoolMgr.h"
15 #include "SquidConfig.h"
16 #include "SquidTime.h"
18 #include "ssl/PeerConnector.h"
21 CBDATA_CLASS_INIT(PeerPoolMgr
);
24 /// Gives Ssl::PeerConnector access to Answer in the PeerPoolMgr callback dialer.
25 class MyAnswerDialer
: public UnaryMemFunT
<PeerPoolMgr
, Ssl::PeerConnectorAnswer
, Ssl::PeerConnectorAnswer
&>,
26 public Ssl::PeerConnector::CbDialer
29 MyAnswerDialer(const JobPointer
&aJob
, Method aMethod
):
30 UnaryMemFunT
<PeerPoolMgr
, Ssl::PeerConnectorAnswer
, Ssl::PeerConnectorAnswer
&>(aJob
, aMethod
, Ssl::PeerConnectorAnswer()) {}
32 /* Ssl::PeerConnector::CbDialer API */
33 virtual Ssl::PeerConnectorAnswer
&answer() { return arg1
; }
37 PeerPoolMgr::PeerPoolMgr(CachePeer
*aPeer
): AsyncJob("PeerPoolMgr"),
38 peer(cbdataReference(aPeer
)),
47 PeerPoolMgr::~PeerPoolMgr()
49 cbdataReferenceDone(peer
);
57 // ErrorState, getOutgoingAddress(), and other APIs may require a request.
58 // We fake one. TODO: Optionally send this request to peers?
59 request
= new HttpRequest(Http::METHOD_OPTIONS
, AnyP::PROTO_HTTP
, "*");
60 request
->SetHost(peer
->host
);
62 checkpoint("peer initialized");
66 PeerPoolMgr::swanSong()
72 PeerPoolMgr::validPeer() const
74 return peer
&& cbdataReferenceValid(peer
) && peer
->standby
.pool
;
78 PeerPoolMgr::doneAll() const
80 return !(validPeer() && peer
->standby
.limit
) && AsyncJob::doneAll();
84 PeerPoolMgr::handleOpenedConnection(const CommConnectCbParams
¶ms
)
89 debugs(48, 3, "peer gone");
90 if (params
.conn
!= NULL
)
95 if (params
.flag
!= Comm::OK
) {
96 /* it might have been a timeout with a partially open link */
97 if (params
.conn
!= NULL
)
99 peerConnectFailed(peer
);
100 checkpoint("conn opening failure"); // may retry
104 Must(params
.conn
!= NULL
);
109 typedef CommCbMemFunT
<PeerPoolMgr
, CommCloseCbParams
> CloserDialer
;
110 closer
= JobCallback(48, 3, CloserDialer
, this,
111 PeerPoolMgr::handleSecureClosure
);
112 comm_add_close_handler(params
.conn
->fd
, closer
);
114 securer
= asyncCall(48, 4, "PeerPoolMgr::handleSecuredPeer",
115 MyAnswerDialer(this, &PeerPoolMgr::handleSecuredPeer
));
117 const int peerTimeout
= peer
->connect_timeout
> 0 ?
118 peer
->connect_timeout
: Config
.Timeout
.peer_connect
;
119 const int timeUsed
= squid_curtime
- params
.conn
->startTime();
120 // Use positive timeout when less than one second is left for conn.
121 const int timeLeft
= max(1, (peerTimeout
- timeUsed
));
122 Ssl::PeerConnector
*connector
=
123 new Ssl::PeerConnector(request
, params
.conn
, NULL
, securer
, timeLeft
);
124 AsyncJob::Start(connector
); // will call our callback
129 pushNewConnection(params
.conn
);
133 PeerPoolMgr::pushNewConnection(const Comm::ConnectionPointer
&conn
)
136 Must(Comm::IsConnOpen(conn
));
137 peer
->standby
.pool
->push(conn
, NULL
/* domain */);
138 // push() will trigger a checkpoint()
143 PeerPoolMgr::handleSecuredPeer(Ssl::PeerConnectorAnswer
&answer
)
145 Must(securer
!= NULL
);
148 if (closer
!= NULL
) {
149 if (answer
.conn
!= NULL
)
150 comm_remove_close_handler(answer
.conn
->fd
, closer
);
152 closer
->cancel("securing completed");
157 debugs(48, 3, "peer gone");
158 if (answer
.conn
!= NULL
)
159 answer
.conn
->close();
163 if (answer
.error
.get()) {
164 if (answer
.conn
!= NULL
)
165 answer
.conn
->close();
166 // PeerConnector calls peerConnectFailed() for us;
167 checkpoint("conn securing failure"); // may retry
171 pushNewConnection(answer
.conn
);
175 PeerPoolMgr::handleSecureClosure(const CommCloseCbParams
¶ms
)
177 Must(closer
!= NULL
);
178 Must(securer
!= NULL
);
179 securer
->cancel("conn closed by a 3rd party");
182 // allow the closing connection to fully close before we check again
183 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());