]>
Commit | Line | Data |
---|---|---|
774c051c | 1 | /* |
507d0a78 | 2 | * DEBUG: section 93 ICAP (RFC 3507) Client |
774c051c | 3 | */ |
4 | ||
582c2af2 | 5 | #include "squid.h" |
1adcebc3 | 6 | #include "adaptation/Answer.h" |
3d93a84d AJ |
7 | #include "adaptation/icap/Config.h" |
8 | #include "adaptation/icap/ModXact.h" | |
26cc52cb AR |
9 | #include "adaptation/icap/Options.h" |
10 | #include "adaptation/icap/OptXact.h" | |
3d93a84d AJ |
11 | #include "adaptation/icap/ServiceRep.h" |
12 | #include "base/TextException.h" | |
983983ce | 13 | #include "comm/Connection.h" |
774c051c | 14 | #include "ConfigParser.h" |
582c2af2 FC |
15 | #include "Debug.h" |
16 | #include "fde.h" | |
af69c635 | 17 | #include "globals.h" |
3d93a84d | 18 | #include "HttpReply.h" |
582c2af2 | 19 | #include "ip/tools.h" |
985c86bc | 20 | #include "SquidTime.h" |
774c051c | 21 | |
26cc52cb | 22 | CBDATA_NAMESPACED_CLASS_INIT(Adaptation::Icap, ServiceRep); |
774c051c | 23 | |
6666da11 | 24 | Adaptation::Icap::ServiceRep::ServiceRep(const ServiceConfigPointer &svcCfg): |
b0365bd9 | 25 | AsyncJob("Adaptation::Icap::ServiceRep"), Adaptation::Service(svcCfg), |
98a74c05 | 26 | theOptions(NULL), theOptionsFetcher(0), theLastUpdate(0), |
2dba5b8e CT |
27 | theBusyConns(0), |
28 | theAllWaiters(0), | |
29 | connOverloadReported(false), | |
fb505fa1 | 30 | theIdleConns(NULL), |
8277060a | 31 | isSuspended(0), notifying(false), |
76fc7e57 AJ |
32 | updateScheduled(false), |
33 | wasAnnouncedUp(true), // do not announce an "up" service at startup | |
34 | isDetached(false) | |
2dba5b8e CT |
35 | { |
36 | setMaxConnections(); | |
fb505fa1 | 37 | theIdleConns = new IdleConnList("ICAP Service", NULL); |
2dba5b8e | 38 | } |
774c051c | 39 | |
26cc52cb | 40 | Adaptation::Icap::ServiceRep::~ServiceRep() |
774c051c | 41 | { |
fb505fa1 | 42 | delete theIdleConns; |
98a74c05 | 43 | Must(!theOptionsFetcher); |
76fc7e57 | 44 | delete theOptions; |
62c7f90e | 45 | } |
774c051c | 46 | |
62c7f90e | 47 | void |
26cc52cb | 48 | Adaptation::Icap::ServiceRep::finalize() |
62c7f90e | 49 | { |
9e008dda | 50 | Adaptation::Service::finalize(); |
774c051c | 51 | |
a68cf076 | 52 | // use /etc/services or default port if needed |
9e008dda | 53 | const bool have_port = cfg().port >= 0; |
a68cf076 | 54 | if (!have_port) { |
774c051c | 55 | struct servent *serv = getservbyname("icap", "tcp"); |
56 | ||
57 | if (serv) { | |
d81a31f1 | 58 | writeableCfg().port = htons(serv->s_port); |
774c051c | 59 | } else { |
d81a31f1 | 60 | writeableCfg().port = 1344; |
774c051c | 61 | } |
62 | } | |
8277060a CT |
63 | |
64 | theSessionFailures.configure(TheConfig.oldest_service_failure > 0 ? | |
a459e80a | 65 | TheConfig.oldest_service_failure : -1); |
a68cf076 | 66 | } |
774c051c | 67 | |
26cc52cb | 68 | void Adaptation::Icap::ServiceRep::noteFailure() |
9e008dda | 69 | { |
8277060a CT |
70 | const int failures = theSessionFailures.count(1); |
71 | debugs(93,4, HERE << " failure " << failures << " out of " << | |
72 | TheConfig.service_failure_limit << " allowed in " << | |
73 | TheConfig.oldest_service_failure << "sec " << status()); | |
1299ecbf | 74 | |
75 | if (isSuspended) | |
76 | return; | |
c99de607 | 77 | |
26cc52cb | 78 | if (TheConfig.service_failure_limit >= 0 && |
8277060a | 79 | failures > TheConfig.service_failure_limit) |
c99de607 | 80 | suspend("too many failures"); |
81 | ||
82 | // TODO: Should bypass setting affect how much Squid tries to talk to | |
9e008dda AJ |
83 | // the ICAP service that is currently unusable and is likely to remain |
84 | // so for some time? The current code says "no". Perhaps the answer | |
c99de607 | 85 | // should be configurable. |
86 | } | |
87 | ||
2dba5b8e | 88 | // returns a persistent or brand new connection; negative int on failures |
983983ce AJ |
89 | Comm::ConnectionPointer |
90 | Adaptation::Icap::ServiceRep::getConnection(bool retriableXact, bool &reused) | |
2dba5b8e | 91 | { |
9815f129 | 92 | Comm::ConnectionPointer connection; |
f915be8d AJ |
93 | |
94 | /* 2011-06-17: rousskov: | |
95 | * There are two things that happen at the same time in pop(). Both are important. | |
96 | * 1) Ensure that we can use a pconn for this transaction. | |
97 | * 2) Ensure that the number of idle pconns does not grow without bounds. | |
98 | * | |
99 | * Both happen in the beginning of the transaction. Both are dictated by real-world problems. | |
100 | * retriable means you can repeat the request if you suspect the first try failed due to a pconn race. | |
101 | * HTTP and ICAP rules prohibit the use of pconns for non-retriable requests. | |
102 | * | |
103 | * If there are zero idle connections, (2) is irrelevant. (2) is only relevant when there are many | |
104 | * idle connections and we should not open more connections without closing some idle ones, | |
105 | * or instead of just opening a new connection and leaving idle connections as is. | |
106 | * In other words, (2) tells us to close one FD for each new one we open due to retriable. | |
107 | */ | |
108 | if (retriableXact) | |
fb505fa1 | 109 | connection = theIdleConns->pop(); |
f915be8d | 110 | else |
fb505fa1 | 111 | theIdleConns->closeN(1); |
2dba5b8e | 112 | |
fb505fa1 CT |
113 | reused = Comm::IsConnOpen(connection); |
114 | ++theBusyConns; | |
115 | debugs(93,3, HERE << "got connection: " << connection); | |
2dba5b8e CT |
116 | return connection; |
117 | } | |
118 | ||
119 | // pools connection if it is reusable or closes it | |
a32c060f | 120 | void Adaptation::Icap::ServiceRep::putConnection(const Comm::ConnectionPointer &conn, bool isReusable, bool sendReset, const char *comment) |
2dba5b8e | 121 | { |
983983ce | 122 | Must(Comm::IsConnOpen(conn)); |
2dba5b8e CT |
123 | // do not pool an idle connection if we owe connections |
124 | if (isReusable && excessConnections() == 0) { | |
125 | debugs(93, 3, HERE << "pushing pconn" << comment); | |
983983ce | 126 | commUnsetConnTimeout(conn); |
fb505fa1 | 127 | theIdleConns->push(conn); |
2dba5b8e | 128 | } else { |
a32c060f AR |
129 | debugs(93, 3, HERE << (sendReset ? "RST" : "FIN") << "-closing " << |
130 | comment); | |
131 | // comm_close called from Connection::close will clear timeout | |
132 | // TODO: add "bool sendReset = false" to Connection::close()? | |
133 | if (sendReset) | |
134 | comm_reset_close(conn); | |
135 | else | |
136 | conn->close(); | |
2dba5b8e CT |
137 | } |
138 | ||
139 | Must(theBusyConns > 0); | |
140 | --theBusyConns; | |
141 | // a connection slot released. Check if there are waiters.... | |
142 | busyCheckpoint(); | |
143 | } | |
144 | ||
145 | // a wrapper to avoid exposing theIdleConns | |
983983ce | 146 | void Adaptation::Icap::ServiceRep::noteConnectionUse(const Comm::ConnectionPointer &conn) |
2dba5b8e | 147 | { |
983983ce | 148 | Must(Comm::IsConnOpen(conn)); |
9815f129 | 149 | fd_table[conn->fd].noteUse(NULL); // pconn re-use but not via PconnPool API |
2dba5b8e CT |
150 | } |
151 | ||
fb505fa1 CT |
152 | void Adaptation::Icap::ServiceRep::noteConnectionFailed(const char *comment) |
153 | { | |
154 | debugs(93, 3, HERE << "Connection failed: " << comment); | |
155 | --theBusyConns; | |
156 | } | |
157 | ||
2dba5b8e CT |
158 | void Adaptation::Icap::ServiceRep::setMaxConnections() |
159 | { | |
160 | if (cfg().maxConn >= 0) | |
161 | theMaxConnections = cfg().maxConn; | |
162 | else if (theOptions && theOptions->max_connections >= 0) | |
163 | theMaxConnections = theOptions->max_connections; | |
164 | else { | |
165 | theMaxConnections = -1; | |
166 | return; | |
167 | } | |
168 | ||
169 | if (::Config.workers > 1 ) | |
170 | theMaxConnections /= ::Config.workers; | |
171 | } | |
172 | ||
173 | int Adaptation::Icap::ServiceRep::availableConnections() const | |
174 | { | |
175 | if (theMaxConnections < 0) | |
176 | return -1; | |
177 | ||
178 | // we are available if we can open or reuse connections | |
179 | // in other words, if we will not create debt | |
180 | int available = max(0, theMaxConnections - theBusyConns); | |
181 | ||
182 | if (!available && !connOverloadReported) { | |
183 | debugs(93, DBG_IMPORTANT, "WARNING: ICAP Max-Connections limit " << | |
184 | "exceeded for service " << cfg().uri << ". Open connections now: " << | |
fb505fa1 CT |
185 | theBusyConns + theIdleConns->count() << ", including " << |
186 | theIdleConns->count() << " idle persistent connections."); | |
2dba5b8e CT |
187 | connOverloadReported = true; |
188 | } | |
189 | ||
190 | if (cfg().onOverload == srvForce) | |
191 | return -1; | |
192 | ||
193 | return available; | |
194 | } | |
195 | ||
196 | // The number of connections which excess the Max-Connections limit | |
197 | int Adaptation::Icap::ServiceRep::excessConnections() const | |
198 | { | |
199 | if (theMaxConnections < 0) | |
200 | return 0; | |
201 | ||
202 | // Waiters affect the number of needed connections but a needed | |
203 | // connection may still be excessive from Max-Connections p.o.v. | |
204 | // so we should not account for waiting transaction needs here. | |
fb505fa1 | 205 | const int debt = theBusyConns + theIdleConns->count() - theMaxConnections; |
2dba5b8e CT |
206 | if (debt > 0) |
207 | return debt; | |
208 | else | |
209 | return 0; | |
210 | } | |
211 | ||
212 | void Adaptation::Icap::ServiceRep::noteGoneWaiter() | |
213 | { | |
a2f5277a | 214 | --theAllWaiters; |
2dba5b8e CT |
215 | |
216 | // in case the notified transaction did not take the connection slot | |
217 | busyCheckpoint(); | |
218 | } | |
219 | ||
220 | // called when a connection slot may become available | |
221 | void Adaptation::Icap::ServiceRep::busyCheckpoint() | |
222 | { | |
223 | if (theNotificationWaiters.empty()) // nobody is waiting for a slot | |
224 | return; | |
225 | ||
226 | int freed = 0; | |
227 | int available = availableConnections(); | |
228 | ||
229 | if (available < 0) { | |
230 | // It is possible to have waiters when no limit on connections exist in | |
231 | // case of reconfigure or because new Options received. | |
232 | // In this case, notify all waiting transactions. | |
233 | freed = theNotificationWaiters.size(); | |
234 | } else { | |
235 | // avoid notifying more waiters than there will be available slots | |
236 | const int notifiedWaiters = theAllWaiters - theNotificationWaiters.size(); | |
237 | freed = available - notifiedWaiters; | |
238 | } | |
239 | ||
240 | debugs(93,7, HERE << "Available connections: " << available << | |
241 | " freed slots: " << freed << | |
242 | " waiting in queue: " << theNotificationWaiters.size()); | |
243 | ||
244 | while (freed > 0 && !theNotificationWaiters.empty()) { | |
245 | Client i = theNotificationWaiters.front(); | |
246 | theNotificationWaiters.pop_front(); | |
247 | ScheduleCallHere(i.callback); | |
248 | i.callback = NULL; | |
249 | --freed; | |
250 | } | |
251 | } | |
252 | ||
26cc52cb | 253 | void Adaptation::Icap::ServiceRep::suspend(const char *reason) |
9e008dda | 254 | { |
c99de607 | 255 | if (isSuspended) { |
192378eb | 256 | debugs(93,4, HERE << "keeping suspended, also for " << reason); |
c99de607 | 257 | } else { |
258 | isSuspended = reason; | |
e0236918 | 259 | debugs(93, DBG_IMPORTANT, "suspending ICAP service for " << reason); |
26cc52cb | 260 | scheduleUpdate(squid_curtime + TheConfig.service_revival_delay); |
c99de607 | 261 | announceStatusChange("suspended", true); |
262 | } | |
263 | } | |
264 | ||
26cc52cb | 265 | bool Adaptation::Icap::ServiceRep::probed() const |
c99de607 | 266 | { |
267 | return theLastUpdate != 0; | |
268 | } | |
269 | ||
26cc52cb | 270 | bool Adaptation::Icap::ServiceRep::hasOptions() const |
9e008dda | 271 | { |
c99de607 | 272 | return theOptions && theOptions->valid() && theOptions->fresh(); |
273 | } | |
274 | ||
26cc52cb | 275 | bool Adaptation::Icap::ServiceRep::up() const |
774c051c | 276 | { |
76fc7e57 | 277 | return !isSuspended && hasOptions(); |
c99de607 | 278 | } |
279 | ||
2dba5b8e CT |
280 | bool Adaptation::Icap::ServiceRep::availableForNew() const |
281 | { | |
282 | Must(up()); | |
283 | int available = availableConnections(); | |
284 | if (available < 0) | |
285 | return true; | |
286 | else | |
287 | return (available - theAllWaiters > 0); | |
288 | } | |
289 | ||
290 | bool Adaptation::Icap::ServiceRep::availableForOld() const | |
291 | { | |
292 | Must(up()); | |
293 | ||
294 | int available = availableConnections(); | |
295 | return (available != 0); // it is -1 (no limit) or has available slots | |
296 | } | |
297 | ||
26cc52cb | 298 | bool Adaptation::Icap::ServiceRep::wantsUrl(const String &urlPath) const |
c99de607 | 299 | { |
300 | Must(hasOptions()); | |
26cc52cb | 301 | return theOptions->transferKind(urlPath) != Adaptation::Icap::Options::xferIgnore; |
774c051c | 302 | } |
303 | ||
26cc52cb | 304 | bool Adaptation::Icap::ServiceRep::wantsPreview(const String &urlPath, size_t &wantedSize) const |
774c051c | 305 | { |
c99de607 | 306 | Must(hasOptions()); |
774c051c | 307 | |
308 | if (theOptions->preview < 0) | |
309 | return false; | |
310 | ||
26cc52cb | 311 | if (theOptions->transferKind(urlPath) != Adaptation::Icap::Options::xferPreview) |
c99de607 | 312 | return false; |
313 | ||
774c051c | 314 | wantedSize = theOptions->preview; |
315 | ||
316 | return true; | |
317 | } | |
318 | ||
26cc52cb | 319 | bool Adaptation::Icap::ServiceRep::allows204() const |
774c051c | 320 | { |
c99de607 | 321 | Must(hasOptions()); |
774c051c | 322 | return true; // in the future, we may have ACLs to prevent 204s |
323 | } | |
324 | ||
83c51da9 CT |
325 | bool Adaptation::Icap::ServiceRep::allows206() const |
326 | { | |
327 | Must(hasOptions()); | |
328 | if (theOptions->allow206) | |
329 | return true; // in the future, we may have ACLs to prevent 206s | |
330 | return false; | |
331 | } | |
332 | ||
774c051c | 333 | static |
26cc52cb | 334 | void ServiceRep_noteTimeToUpdate(void *data) |
774c051c | 335 | { |
26cc52cb | 336 | Adaptation::Icap::ServiceRep *service = static_cast<Adaptation::Icap::ServiceRep*>(data); |
774c051c | 337 | Must(service); |
338 | service->noteTimeToUpdate(); | |
339 | } | |
340 | ||
26cc52cb | 341 | void Adaptation::Icap::ServiceRep::noteTimeToUpdate() |
774c051c | 342 | { |
76fc7e57 | 343 | if (!detached()) |
c99de607 | 344 | updateScheduled = false; |
345 | ||
4299f876 | 346 | if (detached() || theOptionsFetcher.set()) { |
192378eb | 347 | debugs(93,5, HERE << "ignores options update " << status()); |
774c051c | 348 | return; |
349 | } | |
350 | ||
192378eb | 351 | debugs(93,5, HERE << "performs a regular options update " << status()); |
774c051c | 352 | startGettingOptions(); |
353 | } | |
354 | ||
bd7f2ede | 355 | #if 0 |
774c051c | 356 | static |
26cc52cb | 357 | void Adaptation::Icap::ServiceRep_noteTimeToNotify(void *data) |
774c051c | 358 | { |
26cc52cb | 359 | Adaptation::Icap::ServiceRep *service = static_cast<Adaptation::Icap::ServiceRep*>(data); |
774c051c | 360 | Must(service); |
361 | service->noteTimeToNotify(); | |
362 | } | |
bd7f2ede | 363 | #endif |
774c051c | 364 | |
26cc52cb | 365 | void Adaptation::Icap::ServiceRep::noteTimeToNotify() |
774c051c | 366 | { |
367 | Must(!notifying); | |
368 | notifying = true; | |
192378eb | 369 | debugs(93,7, HERE << "notifies " << theClients.size() << " clients " << |
774c051c | 370 | status()); |
371 | ||
372 | // note: we must notify even if we are invalidated | |
373 | ||
374 | Pointer us = NULL; | |
375 | ||
376 | while (!theClients.empty()) { | |
377 | Client i = theClients.pop_back(); | |
9e008dda AJ |
378 | ScheduleCallHere(i.callback); |
379 | i.callback = 0; | |
774c051c | 380 | } |
381 | ||
382 | notifying = false; | |
383 | } | |
384 | ||
2dba5b8e CT |
385 | void Adaptation::Icap::ServiceRep::callWhenAvailable(AsyncCall::Pointer &cb, bool priority) |
386 | { | |
387 | debugs(93,8, "ICAPServiceRep::callWhenAvailable"); | |
388 | Must(cb!=NULL); | |
389 | Must(up()); | |
fb505fa1 | 390 | Must(!theIdleConns->count()); // or we should not be waiting |
2dba5b8e CT |
391 | |
392 | Client i; | |
393 | i.service = Pointer(this); | |
394 | i.callback = cb; | |
395 | if (priority) | |
396 | theNotificationWaiters.push_front(i); | |
397 | else | |
398 | theNotificationWaiters.push_back(i); | |
399 | ||
400 | busyCheckpoint(); | |
401 | } | |
402 | ||
26cc52cb | 403 | void Adaptation::Icap::ServiceRep::callWhenReady(AsyncCall::Pointer &cb) |
774c051c | 404 | { |
bd7f2ede | 405 | Must(cb!=NULL); |
406 | ||
26cc52cb | 407 | debugs(93,5, HERE << "Adaptation::Icap::Service is asked to call " << *cb << |
9e008dda | 408 | " when ready " << status()); |
5f8252d2 | 409 | |
c99de607 | 410 | Must(!broken()); // we do not wait for a broken service |
774c051c | 411 | |
412 | Client i; | |
76fc7e57 | 413 | i.service = Pointer(this); // TODO: is this really needed? |
774c051c | 414 | i.callback = cb; |
774c051c | 415 | theClients.push_back(i); |
416 | ||
4299f876 | 417 | if (theOptionsFetcher.set() || notifying) |
774c051c | 418 | return; // do nothing, we will be picked up in noteTimeToNotify() |
419 | ||
420 | if (needNewOptions()) | |
421 | startGettingOptions(); | |
422 | else | |
423 | scheduleNotification(); | |
424 | } | |
425 | ||
26cc52cb | 426 | void Adaptation::Icap::ServiceRep::scheduleNotification() |
774c051c | 427 | { |
192378eb | 428 | debugs(93,7, HERE << "will notify " << theClients.size() << " clients"); |
4299f876 | 429 | CallJobHere(93, 5, this, Adaptation::Icap::ServiceRep, noteTimeToNotify); |
774c051c | 430 | } |
431 | ||
26cc52cb | 432 | bool Adaptation::Icap::ServiceRep::needNewOptions() const |
774c051c | 433 | { |
76fc7e57 | 434 | return !detached() && !up(); |
774c051c | 435 | } |
436 | ||
26cc52cb | 437 | void Adaptation::Icap::ServiceRep::changeOptions(Adaptation::Icap::Options *newOptions) |
774c051c | 438 | { |
192378eb | 439 | debugs(93,8, HERE << "changes options from " << theOptions << " to " << |
5f8252d2 | 440 | newOptions << ' ' << status()); |
c99de607 | 441 | |
774c051c | 442 | delete theOptions; |
443 | theOptions = newOptions; | |
8277060a | 444 | theSessionFailures.clear(); |
c99de607 | 445 | isSuspended = 0; |
446 | theLastUpdate = squid_curtime; | |
447 | ||
448 | checkOptions(); | |
449 | announceStatusChange("down after an options fetch failure", true); | |
450 | } | |
ffddf96c | 451 | |
26cc52cb | 452 | void Adaptation::Icap::ServiceRep::checkOptions() |
c99de607 | 453 | { |
9d0cdbb9 | 454 | if (theOptions == NULL) |
455 | return; | |
456 | ||
eafe3f72 | 457 | if (!theOptions->valid()) { |
e0236918 | 458 | debugs(93, DBG_IMPORTANT, "WARNING: Squid got an invalid ICAP OPTIONS response " << |
9e008dda | 459 | "from service " << cfg().uri << "; error: " << theOptions->error); |
eafe3f72 | 460 | return; |
461 | } | |
462 | ||
ffddf96c | 463 | /* |
eadded2e | 464 | * Issue a warning if the ICAP server returned methods in the |
465 | * options response that don't match the method from squid.conf. | |
ffddf96c | 466 | */ |
467 | ||
eadded2e | 468 | if (!theOptions->methods.empty()) { |
469 | bool method_found = false; | |
30abd221 | 470 | String method_list; |
eadded2e | 471 | Vector <ICAP::Method>::iterator iter = theOptions->methods.begin(); |
eadded2e | 472 | |
473 | while (iter != theOptions->methods.end()) { | |
eadded2e | 474 | |
d81a31f1 | 475 | if (*iter == cfg().method) { |
eadded2e | 476 | method_found = true; |
477 | break; | |
478 | } | |
479 | ||
480 | method_list.append(ICAP::methodStr(*iter)); | |
481 | method_list.append(" ", 1); | |
742a021b | 482 | ++iter; |
eadded2e | 483 | } |
484 | ||
eadded2e | 485 | if (!method_found) { |
e0236918 | 486 | debugs(93, DBG_IMPORTANT, "WARNING: Squid is configured to use ICAP method " << |
d81a31f1 | 487 | cfg().methodStr() << |
a7a42b14 | 488 | " for service " << cfg().uri << |
5b4117d8 | 489 | " but OPTIONS response declares the methods are " << method_list); |
eadded2e | 490 | } |
491 | } | |
8eeb99bf | 492 | |
8eeb99bf | 493 | /* |
494 | * Check the ICAP server's date header for clock skew | |
495 | */ | |
5f8252d2 | 496 | const int skew = (int)(theOptions->timestamp() - squid_curtime); |
497 | if (abs(skew) > theOptions->ttl()) { | |
498 | // TODO: If skew is negative, the option will be considered down | |
499 | // because of stale options. We should probably change this. | |
e0236918 | 500 | debugs(93, DBG_IMPORTANT, "ICAP service's clock is skewed by " << skew << |
a7a42b14 | 501 | " seconds: " << cfg().uri); |
5f8252d2 | 502 | } |
c99de607 | 503 | } |
8eeb99bf | 504 | |
26cc52cb | 505 | void Adaptation::Icap::ServiceRep::announceStatusChange(const char *downPhrase, bool important) const |
c99de607 | 506 | { |
507 | if (wasAnnouncedUp == up()) // no significant changes to announce | |
508 | return; | |
9d0cdbb9 | 509 | |
d81a31f1 | 510 | const char *what = cfg().bypass ? "optional" : "essential"; |
c99de607 | 511 | const char *state = wasAnnouncedUp ? downPhrase : "up"; |
9e008dda | 512 | const int level = important ? 1 :2; |
d81a31f1 | 513 | debugs(93,level, what << " ICAP service is " << state << ": " << |
9e008dda | 514 | cfg().uri << ' ' << status()); |
9d0cdbb9 | 515 | |
c99de607 | 516 | wasAnnouncedUp = !wasAnnouncedUp; |
774c051c | 517 | } |
518 | ||
c824c43b | 519 | // we are receiving ICAP OPTIONS response headers here or NULL on failures |
3af10ac0 | 520 | void Adaptation::Icap::ServiceRep::noteAdaptationAnswer(const Answer &answer) |
774c051c | 521 | { |
4299f876 | 522 | Must(initiated(theOptionsFetcher)); |
d81a31f1 | 523 | clearAdaptation(theOptionsFetcher); |
774c051c | 524 | |
3af10ac0 AR |
525 | if (answer.kind == Answer::akError) { |
526 | debugs(93,3, HERE << "failed to fetch options " << status()); | |
527 | handleNewOptions(0); | |
528 | return; | |
529 | } | |
530 | ||
531 | Must(answer.kind == Answer::akForward); // no akBlock for OPTIONS requests | |
532 | HttpMsg *msg = answer.message; | |
c824c43b | 533 | Must(msg); |
534 | ||
192378eb | 535 | debugs(93,5, HERE << "is interpreting new options " << status()); |
5f8252d2 | 536 | |
26cc52cb | 537 | Adaptation::Icap::Options *newOptions = NULL; |
c824c43b | 538 | if (HttpReply *r = dynamic_cast<HttpReply*>(msg)) { |
26cc52cb | 539 | newOptions = new Adaptation::Icap::Options; |
9e008dda | 540 | newOptions->configure(r); |
c824c43b | 541 | } else { |
e0236918 | 542 | debugs(93, DBG_IMPORTANT, "ICAP service got wrong options message " << status()); |
c824c43b | 543 | } |
544 | ||
545 | handleNewOptions(newOptions); | |
546 | } | |
547 | ||
4299f876 AR |
548 | // we (a) must keep trying to get OPTIONS and (b) are RefCounted so we |
549 | // must keep our job alive (XXX: until nobody needs us) | |
550 | void Adaptation::Icap::ServiceRep::callException(const std::exception &e) | |
551 | { | |
552 | clearAdaptation(theOptionsFetcher); | |
553 | debugs(93,2, "ICAP probably failed to fetch options (" << e.what() << | |
4cb2536f | 554 | ")" << status()); |
4299f876 AR |
555 | handleNewOptions(0); |
556 | } | |
557 | ||
26cc52cb | 558 | void Adaptation::Icap::ServiceRep::handleNewOptions(Adaptation::Icap::Options *newOptions) |
c824c43b | 559 | { |
560 | // new options may be NULL | |
5f8252d2 | 561 | changeOptions(newOptions); |
774c051c | 562 | |
192378eb | 563 | debugs(93,3, HERE << "got new options and is now " << status()); |
774c051c | 564 | |
1299ecbf | 565 | scheduleUpdate(optionsFetchTime()); |
2dba5b8e | 566 | |
983983ce | 567 | // XXX: this whole feature bases on the false assumption a service only has one IP |
2dba5b8e CT |
568 | setMaxConnections(); |
569 | const int excess = excessConnections(); | |
570 | // if we owe connections and have idle pconns, close the latter | |
fb505fa1 CT |
571 | if (excess && theIdleConns->count() > 0) { |
572 | const int n = min(excess, theIdleConns->count()); | |
2dba5b8e | 573 | debugs(93,5, HERE << "closing " << n << " pconns to relief debt"); |
fb505fa1 | 574 | theIdleConns->closeN(n); |
2dba5b8e CT |
575 | } |
576 | ||
774c051c | 577 | scheduleNotification(); |
578 | } | |
579 | ||
26cc52cb | 580 | void Adaptation::Icap::ServiceRep::startGettingOptions() |
774c051c | 581 | { |
98a74c05 | 582 | Must(!theOptionsFetcher); |
192378eb | 583 | debugs(93,6, HERE << "will get new options " << status()); |
774c051c | 584 | |
4299f876 AR |
585 | // XXX: "this" here is "self"; works until refcounting API changes |
586 | theOptionsFetcher = initiateAdaptation( | |
4cb2536f | 587 | new Adaptation::Icap::OptXactLauncher(this)); |
26cc52cb | 588 | // TODO: timeout in case Adaptation::Icap::OptXact never calls us back? |
c824c43b | 589 | // Such a timeout should probably be a generic AsyncStart feature. |
774c051c | 590 | } |
591 | ||
26cc52cb | 592 | void Adaptation::Icap::ServiceRep::scheduleUpdate(time_t when) |
774c051c | 593 | { |
1299ecbf | 594 | if (updateScheduled) { |
192378eb | 595 | debugs(93,7, HERE << "reschedules update"); |
1299ecbf | 596 | // XXX: check whether the event is there because AR saw |
04d76948 | 597 | // an unreproducible eventDelete assertion on 2007/06/18 |
26cc52cb AR |
598 | if (eventFind(&ServiceRep_noteTimeToUpdate, this)) |
599 | eventDelete(&ServiceRep_noteTimeToUpdate, this); | |
c99de607 | 600 | else |
e0236918 | 601 | debugs(93, DBG_IMPORTANT, "XXX: ICAP service lost an update event."); |
1299ecbf | 602 | updateScheduled = false; |
774c051c | 603 | } |
604 | ||
1299ecbf | 605 | debugs(93,7, HERE << "raw OPTIONS fetch at " << when << " or in " << |
9e008dda | 606 | (when - squid_curtime) << " sec"); |
1299ecbf | 607 | debugs(93,9, HERE << "last fetched at " << theLastUpdate << " or " << |
9e008dda | 608 | (squid_curtime - theLastUpdate) << " sec ago"); |
5f8252d2 | 609 | |
610 | /* adjust update time to prevent too-frequent updates */ | |
611 | ||
c99de607 | 612 | if (when < squid_curtime) |
613 | when = squid_curtime; | |
614 | ||
26cc52cb | 615 | // XXX: move hard-coded constants from here to Adaptation::Icap::TheConfig |
1299ecbf | 616 | const int minUpdateGap = 30; // seconds |
c99de607 | 617 | if (when < theLastUpdate + minUpdateGap) |
618 | when = theLastUpdate + minUpdateGap; | |
774c051c | 619 | |
c99de607 | 620 | const int delay = when - squid_curtime; |
192378eb | 621 | debugs(93,5, HERE << "will fetch OPTIONS in " << delay << " sec"); |
1299ecbf | 622 | |
26cc52cb AR |
623 | eventAdd("Adaptation::Icap::ServiceRep::noteTimeToUpdate", |
624 | &ServiceRep_noteTimeToUpdate, this, delay, 0, true); | |
c99de607 | 625 | updateScheduled = true; |
774c051c | 626 | } |
627 | ||
1299ecbf | 628 | // returns absolute time when OPTIONS should be fetched |
629 | time_t | |
26cc52cb | 630 | Adaptation::Icap::ServiceRep::optionsFetchTime() const |
1299ecbf | 631 | { |
632 | if (theOptions && theOptions->valid()) { | |
633 | const time_t expire = theOptions->expire(); | |
192378eb | 634 | debugs(93,7, HERE << "options expire on " << expire << " >= " << squid_curtime); |
1299ecbf | 635 | |
636 | // conservative estimate of how long the OPTIONS transaction will take | |
26cc52cb | 637 | // XXX: move hard-coded constants from here to Adaptation::Icap::TheConfig |
1299ecbf | 638 | const int expectedWait = 20; // seconds |
639 | ||
640 | // Unknown or invalid (too small) expiration times should not happen. | |
26cc52cb | 641 | // Adaptation::Icap::Options should use the default TTL, and ICAP servers should not |
1299ecbf | 642 | // send invalid TTLs, but bugs and attacks happen. |
643 | if (expire < expectedWait) | |
644 | return squid_curtime; | |
645 | else | |
646 | return expire - expectedWait; // before the current options expire | |
647 | } | |
648 | ||
649 | // use revival delay as "expiration" time for a service w/o valid options | |
26cc52cb | 650 | return squid_curtime + TheConfig.service_revival_delay; |
1299ecbf | 651 | } |
652 | ||
d81a31f1 | 653 | Adaptation::Initiate * |
4299f876 | 654 | Adaptation::Icap::ServiceRep::makeXactLauncher(HttpMsg *virgin, |
4cb2536f | 655 | HttpRequest *cause) |
d81a31f1 | 656 | { |
4299f876 | 657 | return new Adaptation::Icap::ModXactLauncher(virgin, cause, this); |
d81a31f1 AR |
658 | } |
659 | ||
c99de607 | 660 | // returns a temporary string depicting service status, for debugging |
26cc52cb | 661 | const char *Adaptation::Icap::ServiceRep::status() const |
774c051c | 662 | { |
c99de607 | 663 | static MemBuf buf; |
664 | ||
665 | buf.reset(); | |
666 | buf.append("[", 1); | |
667 | ||
668 | if (up()) | |
669 | buf.append("up", 2); | |
5f8252d2 | 670 | else { |
c99de607 | 671 | buf.append("down", 4); |
5f8252d2 | 672 | if (isSuspended) |
673 | buf.append(",susp", 5); | |
c99de607 | 674 | |
5f8252d2 | 675 | if (!theOptions) |
676 | buf.append(",!opt", 5); | |
e1381638 AJ |
677 | else if (!theOptions->valid()) |
678 | buf.append(",!valid", 7); | |
679 | else if (!theOptions->fresh()) | |
680 | buf.append(",stale", 6); | |
5f8252d2 | 681 | } |
774c051c | 682 | |
76fc7e57 AJ |
683 | if (detached()) |
684 | buf.append(",detached", 9); | |
685 | ||
4299f876 | 686 | if (theOptionsFetcher.set()) |
98a74c05 | 687 | buf.append(",fetch", 6); |
774c051c | 688 | |
c99de607 | 689 | if (notifying) |
690 | buf.append(",notif", 6); | |
774c051c | 691 | |
8277060a CT |
692 | if (const int failures = theSessionFailures.remembered()) |
693 | buf.Printf(",fail%d", failures); | |
774c051c | 694 | |
c99de607 | 695 | buf.append("]", 1); |
696 | buf.terminate(); | |
774c051c | 697 | |
c99de607 | 698 | return buf.content(); |
774c051c | 699 | } |
76fc7e57 AJ |
700 | |
701 | void Adaptation::Icap::ServiceRep::detach() | |
702 | { | |
703 | debugs(93,3, HERE << "detaching ICAP service: " << cfg().uri << | |
d090e020 | 704 | ' ' << status()); |
76fc7e57 AJ |
705 | isDetached = true; |
706 | } | |
707 | ||
708 | bool Adaptation::Icap::ServiceRep::detached() const | |
709 | { | |
710 | return isDetached; | |
711 | } | |
2dba5b8e CT |
712 | |
713 | Adaptation::Icap::ConnWaiterDialer::ConnWaiterDialer(const CbcPointer<ModXact> &xact, | |
714 | Adaptation::Icap::ConnWaiterDialer::Parent::Method aHandler): | |
715 | Parent(xact, aHandler) | |
716 | { | |
717 | theService = &xact->service(); | |
718 | theService->noteNewWaiter(); | |
719 | } | |
720 | ||
721 | Adaptation::Icap::ConnWaiterDialer::ConnWaiterDialer(const Adaptation::Icap::ConnWaiterDialer &aConnWaiter): Parent(aConnWaiter) | |
722 | { | |
723 | theService = aConnWaiter.theService; | |
724 | theService->noteNewWaiter(); | |
725 | } | |
726 | ||
727 | Adaptation::Icap::ConnWaiterDialer::~ConnWaiterDialer() | |
728 | { | |
729 | theService->noteGoneWaiter(); | |
730 | } |