2 * Copyright (C) 1996-2025 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.
9 /* DEBUG: section 84 Helper process maintenance */
12 #include "base/AsyncCbdataCalls.h"
13 #include "base/Packable.h"
16 #include "comm/Connection.h"
17 #include "comm/Read.h"
18 #include "comm/Write.h"
19 #include "debug/Messages.h"
22 #include "format/Quoting.h"
24 #include "helper/Reply.h"
25 #include "helper/Request.h"
27 #include "SquidConfig.h"
29 #include "SquidMath.h"
33 // helper_stateful_server::data uses explicit alloc()/freeOne() */
36 #define HELPER_MAX_ARGS 64
38 /// The maximum allowed request retries.
41 /// Helpers input buffer size.
42 /// Keep in sync with MAX_PAC_GROUP_SIZE until converted to SBuf
43 const size_t ReadBufSize(128*1024);
45 static IOCB helperHandleRead
;
46 static IOCB helperStatefulHandleRead
;
47 static void Enqueue(Helper::Client
*, Helper::Xaction
*);
48 static Helper::Session
*GetFirstAvailable(const Helper::Client::Pointer
&);
49 static helper_stateful_server
*StatefulGetFirstAvailable(const statefulhelper::Pointer
&);
50 static void helperDispatch(Helper::Session
*, Helper::Xaction
*);
51 static void helperStatefulDispatch(helper_stateful_server
* srv
, Helper::Xaction
* r
);
52 static void helperKickQueue(const Helper::Client::Pointer
&);
53 static void helperStatefulKickQueue(const statefulhelper::Pointer
&);
54 static void helperStatefulServerDone(helper_stateful_server
* srv
);
55 static void StatefulEnqueue(statefulhelper
* hlp
, Helper::Xaction
* r
);
57 CBDATA_NAMESPACED_CLASS_INIT(Helper
, Session
);
58 CBDATA_CLASS_INIT(helper_stateful_server
);
60 InstanceIdDefinitions(Helper::SessionBase
, "Hlpr");
63 Helper::SessionBase::initStats()
73 Helper::SessionBase::closePipesSafely()
76 shutdown(writePipe
->fd
, SD_BOTH
);
80 if (readPipe
->fd
== writePipe
->fd
)
88 if (WaitForSingleObject(hIpc
, 5000) != WAIT_OBJECT_0
) {
90 debugs(84, DBG_IMPORTANT
, "WARNING: " << helper().id_name
<<
91 " #" << index
<< " (PID " << (long int)pid
<< ") didn't exit in 5 seconds");
99 Helper::SessionBase::closeWritePipeSafely()
102 shutdown(writePipe
->fd
, (readPipe
->fd
== writePipe
->fd
? SD_BOTH
: SD_SEND
));
105 flags
.closing
= true;
106 if (readPipe
->fd
== writePipe
->fd
)
112 if (WaitForSingleObject(hIpc
, 5000) != WAIT_OBJECT_0
) {
114 debugs(84, DBG_IMPORTANT
, "WARNING: " << helper().id_name
<<
115 " #" << index
<< " (PID " << (long int)pid
<< ") didn't exit in 5 seconds");
123 Helper::SessionBase::dropQueued()
125 while (!requests
.empty()) {
126 // XXX: re-schedule these on another helper?
127 const auto r
= requests
.front();
128 requests
.pop_front();
129 r
->reply
.result
= Helper::Unknown
;
130 helper().callBack(*r
);
135 Helper::SessionBase::~SessionBase()
138 memFreeBuf(rbuf_sz
, rbuf
);
143 Helper::Session::~Session()
154 if (Comm::IsConnOpen(writePipe
))
155 closeWritePipeSafely();
157 dlinkDelete(&link
, &parent
->servers
);
159 assert(parent
->childs
.n_running
> 0);
160 -- parent
->childs
.n_running
;
162 assert(requests
.empty());
166 Helper::Session::dropQueued()
168 SessionBase::dropQueued();
169 requestsIndex
.clear();
172 helper_stateful_server::~helper_stateful_server()
174 /* TODO: walk the local queue of requests and carry them all out */
175 if (Comm::IsConnOpen(writePipe
))
176 closeWritePipeSafely();
178 parent
->cancelReservation(reservationId
);
180 dlinkDelete(&link
, &parent
->servers
);
182 assert(parent
->childs
.n_running
> 0);
183 -- parent
->childs
.n_running
;
185 assert(requests
.empty());
189 Helper::Client::openSessions()
195 const char *args
[HELPER_MAX_ARGS
+1]; // save space for a NULL terminator
196 char fd_note_buf
[FD_DESC_SZ
];
204 // Helps reducing diff. TODO: remove
205 const auto hlp
= this;
207 if (hlp
->cmdline
== nullptr)
210 progname
= hlp
->cmdline
->key
;
212 if ((s
= strrchr(progname
, '/')))
213 shortname
= xstrdup(s
+ 1);
215 shortname
= xstrdup(progname
);
217 /* figure out how many new child are actually needed. */
218 int need_new
= hlp
->childs
.needNew();
220 debugs(84, Important(19), "helperOpenServers: Starting " << need_new
<< "/" << hlp
->childs
.n_max
<< " '" << shortname
<< "' processes");
223 debugs(84, Important(20), "helperOpenServers: No '" << shortname
<< "' processes needed.");
226 procname
= (char *)xmalloc(strlen(shortname
) + 3);
228 snprintf(procname
, strlen(shortname
) + 3, "(%s)", shortname
);
230 args
[nargs
] = procname
;
233 for (w
= hlp
->cmdline
->next
; w
&& nargs
< HELPER_MAX_ARGS
; w
= w
->next
) {
234 args
[nargs
] = w
->key
;
238 args
[nargs
] = nullptr;
241 assert(nargs
<= HELPER_MAX_ARGS
+ 1);
243 int successfullyStarted
= 0;
245 for (k
= 0; k
< need_new
; ++k
) {
248 pid
= ipcCreate(hlp
->ipc_type
,
258 debugs(84, DBG_IMPORTANT
, "WARNING: Cannot run '" << progname
<< "' process.");
262 ++successfullyStarted
;
263 ++ hlp
->childs
.n_running
;
264 ++ hlp
->childs
.n_active
;
265 const auto srv
= new Helper::Session
;
269 srv
->addr
= hlp
->addr
;
270 srv
->readPipe
= new Comm::Connection
;
271 srv
->readPipe
->fd
= rfd
;
272 srv
->writePipe
= new Comm::Connection
;
273 srv
->writePipe
->fd
= wfd
;
274 srv
->rbuf
= (char *)memAllocBuf(ReadBufSize
, &srv
->rbuf_sz
);
275 srv
->wqueue
= new MemBuf
;
277 srv
->nextRequestId
= 0;
278 srv
->replyXaction
= nullptr;
279 srv
->ignoreToEom
= false;
281 dlinkAddTail(srv
, &srv
->link
, &hlp
->servers
);
284 snprintf(fd_note_buf
, FD_DESC_SZ
, "%s #%d", shortname
, k
+ 1);
285 fd_note(rfd
, fd_note_buf
);
287 snprintf(fd_note_buf
, FD_DESC_SZ
, "reading %s #%d", shortname
, k
+ 1);
288 fd_note(rfd
, fd_note_buf
);
289 snprintf(fd_note_buf
, FD_DESC_SZ
, "writing %s #%d", shortname
, k
+ 1);
290 fd_note(wfd
, fd_note_buf
);
293 commSetNonBlocking(rfd
);
296 commSetNonBlocking(wfd
);
298 AsyncCall::Pointer closeCall
= asyncCall(5, 4, "Helper::Session::HelperServerClosed", cbdataDialer(SessionBase::HelperServerClosed
,
299 static_cast<Helper::SessionBase
*>(srv
)));
301 comm_add_close_handler(rfd
, closeCall
);
303 if (hlp
->timeout
&& hlp
->childs
.concurrency
) {
304 AsyncCall::Pointer timeoutCall
= commCbCall(84, 4, "Helper::Session::requestTimeout",
305 CommTimeoutCbPtrFun(Helper::Session::requestTimeout
, srv
));
306 commSetConnTimeout(srv
->readPipe
, hlp
->timeout
, timeoutCall
);
309 AsyncCall::Pointer call
= commCbCall(5,4, "helperHandleRead",
310 CommIoCbPtrFun(helperHandleRead
, srv
));
311 comm_read(srv
->readPipe
, srv
->rbuf
, srv
->rbuf_sz
- 1, call
);
314 // Call handleFewerServers() before hlp->last_restart is updated because
315 // that method uses last_restart to measure the delay since previous start.
316 // TODO: Refactor last_restart code to measure failure frequency rather than
317 // detecting a helper #X failure that is being close to the helper #Y start.
318 if (successfullyStarted
< need_new
)
319 hlp
->handleFewerServers(false);
321 hlp
->last_restart
= squid_curtime
;
322 safe_free(shortname
);
324 helperKickQueue(hlp
);
328 statefulhelper::openSessions()
331 const char *args
[HELPER_MAX_ARGS
+1]; // save space for a NULL terminator
332 char fd_note_buf
[FD_DESC_SZ
];
334 // Helps reducing diff. TODO: remove
335 const auto hlp
= this;
337 if (hlp
->cmdline
== nullptr)
340 if (hlp
->childs
.concurrency
)
341 debugs(84, DBG_CRITICAL
, "ERROR: concurrency= is not yet supported for stateful helpers ('" << hlp
->cmdline
<< "')");
343 char *progname
= hlp
->cmdline
->key
;
346 if ((s
= strrchr(progname
, '/')))
347 shortname
= xstrdup(s
+ 1);
349 shortname
= xstrdup(progname
);
351 /* figure out haw mant new helpers are needed. */
352 int need_new
= hlp
->childs
.needNew();
354 debugs(84, DBG_IMPORTANT
, "helperOpenServers: Starting " << need_new
<< "/" << hlp
->childs
.n_max
<< " '" << shortname
<< "' processes");
357 debugs(84, DBG_IMPORTANT
, "helperStatefulOpenServers: No '" << shortname
<< "' processes needed.");
360 char *procname
= (char *)xmalloc(strlen(shortname
) + 3);
362 snprintf(procname
, strlen(shortname
) + 3, "(%s)", shortname
);
364 args
[nargs
] = procname
;
367 for (wordlist
*w
= hlp
->cmdline
->next
; w
&& nargs
< HELPER_MAX_ARGS
; w
= w
->next
) {
368 args
[nargs
] = w
->key
;
372 args
[nargs
] = nullptr;
375 assert(nargs
<= HELPER_MAX_ARGS
+ 1);
377 int successfullyStarted
= 0;
379 for (int k
= 0; k
< need_new
; ++k
) {
384 pid_t pid
= ipcCreate(hlp
->ipc_type
,
394 debugs(84, DBG_IMPORTANT
, "WARNING: Cannot run '" << progname
<< "' process.");
398 ++successfullyStarted
;
399 ++ hlp
->childs
.n_running
;
400 ++ hlp
->childs
.n_active
;
401 helper_stateful_server
*srv
= new helper_stateful_server
;
405 srv
->addr
= hlp
->addr
;
406 srv
->readPipe
= new Comm::Connection
;
407 srv
->readPipe
->fd
= rfd
;
408 srv
->writePipe
= new Comm::Connection
;
409 srv
->writePipe
->fd
= wfd
;
410 srv
->rbuf
= (char *)memAllocBuf(ReadBufSize
, &srv
->rbuf_sz
);
413 srv
->reservationStart
= 0;
415 dlinkAddTail(srv
, &srv
->link
, &hlp
->servers
);
418 snprintf(fd_note_buf
, FD_DESC_SZ
, "%s #%d", shortname
, k
+ 1);
419 fd_note(rfd
, fd_note_buf
);
421 snprintf(fd_note_buf
, FD_DESC_SZ
, "reading %s #%d", shortname
, k
+ 1);
422 fd_note(rfd
, fd_note_buf
);
423 snprintf(fd_note_buf
, FD_DESC_SZ
, "writing %s #%d", shortname
, k
+ 1);
424 fd_note(wfd
, fd_note_buf
);
427 commSetNonBlocking(rfd
);
430 commSetNonBlocking(wfd
);
432 AsyncCall::Pointer closeCall
= asyncCall(5, 4, "helper_stateful_server::HelperServerClosed", cbdataDialer(Helper::SessionBase::HelperServerClosed
,
433 static_cast<Helper::SessionBase
*>(srv
)));
435 comm_add_close_handler(rfd
, closeCall
);
437 AsyncCall::Pointer call
= commCbCall(5,4, "helperStatefulHandleRead",
438 CommIoCbPtrFun(helperStatefulHandleRead
, srv
));
439 comm_read(srv
->readPipe
, srv
->rbuf
, srv
->rbuf_sz
- 1, call
);
442 // Call handleFewerServers() before hlp->last_restart is updated because
443 // that method uses last_restart to measure the delay since previous start.
444 // TODO: Refactor last_restart code to measure failure frequency rather than
445 // detecting a helper #X failure that is being close to the helper #Y start.
446 if (successfullyStarted
< need_new
)
447 hlp
->handleFewerServers(false);
449 hlp
->last_restart
= squid_curtime
;
450 safe_free(shortname
);
452 helperStatefulKickQueue(hlp
);
456 Helper::Client::submitRequest(Helper::Xaction
* const r
)
458 if (const auto srv
= GetFirstAvailable(this))
459 helperDispatch(srv
, r
);
466 /// handles helperSubmit() and helperStatefulSubmit() failures
468 SubmissionFailure(const Helper::Client::Pointer
&hlp
, HLPCB
*callback
, void *data
)
470 auto result
= Helper::Error
;
472 debugs(84, 3, "no helper");
473 result
= Helper::Unknown
;
475 // else pretend the helper has responded with ERR
477 callback(data
, Helper::Reply(result
));
481 helperSubmit(const Helper::Client::Pointer
&hlp
, const char * const buf
, HLPCB
* const callback
, void * const data
)
483 if (!hlp
|| !hlp
->trySubmit(buf
, callback
, data
))
484 SubmissionFailure(hlp
, callback
, data
);
487 /// whether queuing an additional request would overload the helper
489 Helper::Client::queueFull() const {
490 return stats
.queue_size
>= static_cast<int>(childs
.queue_size
);
494 Helper::Client::overloaded() const {
495 return stats
.queue_size
> static_cast<int>(childs
.queue_size
);
498 /// synchronizes queue-dependent measurements with the current queue state
500 Helper::Client::syncQueueStats()
504 debugs(84, 5, id_name
<< " still overloaded; dropped " << droppedRequests
);
506 overloadStart
= squid_curtime
;
507 debugs(84, 3, id_name
<< " became overloaded");
511 debugs(84, 5, id_name
<< " is no longer overloaded");
512 if (droppedRequests
) {
513 debugs(84, DBG_IMPORTANT
, "helper " << id_name
<<
514 " is no longer overloaded after dropping " << droppedRequests
<<
515 " requests in " << (squid_curtime
- overloadStart
) << " seconds");
523 /// prepares the helper for request submission
524 /// returns true if and only if the submission should proceed
525 /// may kill Squid if the helper remains overloaded for too long
527 Helper::Client::prepSubmit()
529 // re-sync for the configuration may have changed since the last submission
532 // Nothing special to do if the new request does not overload (i.e., the
533 // queue is not even full yet) or only _starts_ overloading this helper
534 // (i.e., the queue is currently at its limit).
538 if (squid_curtime
- overloadStart
<= 180)
539 return true; // also OK: overload has not persisted long enough to panic
541 if (childs
.onPersistentOverload
== ChildConfig::actDie
)
542 fatalf("Too many queued %s requests; see on-persistent-overload.", id_name
);
544 if (!droppedRequests
) {
545 debugs(84, DBG_IMPORTANT
, "WARNING: dropping requests to overloaded " <<
546 id_name
<< " helper configured with on-persistent-overload=err");
549 debugs(84, 3, "failed to send " << droppedRequests
<< " helper requests to " << id_name
);
554 Helper::Client::trySubmit(const char * const buf
, HLPCB
* const callback
, void * const data
)
557 return false; // request was dropped
559 submit(buf
, callback
, data
); // will send or queue
560 return true; // request submitted or queued
563 /// dispatches or enqueues a helper requests; does not enforce queue limits
565 Helper::Client::submit(const char * const buf
, HLPCB
* const callback
, void * const data
)
567 const auto r
= new Xaction(callback
, data
, buf
);
569 debugs(84, DBG_DATA
, Raw("buf", buf
, strlen(buf
)));
573 Helper::Client::callBack(Xaction
&r
)
575 const auto callback
= r
.request
.callback
;
578 r
.request
.callback
= nullptr;
579 void *cbdata
= nullptr;
580 if (cbdataReferenceValidDone(r
.request
.data
, &cbdata
))
581 callback(cbdata
, r
.reply
);
584 /// Submit request or callback the caller with a Helper::Error error.
585 /// If the reservation is not set then reserves a new helper.
587 helperStatefulSubmit(const statefulhelper::Pointer
&hlp
, const char *buf
, HLPCB
* callback
, void *data
, const Helper::ReservationId
& reservation
)
589 if (!hlp
|| !hlp
->trySubmit(buf
, callback
, data
, reservation
))
590 SubmissionFailure(hlp
, callback
, data
);
593 /// If possible, submit request. Otherwise, either kill Squid or return false.
595 statefulhelper::trySubmit(const char *buf
, HLPCB
* callback
, void *data
, const Helper::ReservationId
& reservation
)
598 return false; // request was dropped
600 submit(buf
, callback
, data
, reservation
); // will send or queue
601 return true; // request submitted or queued
605 statefulhelper::reserveServer(helper_stateful_server
* srv
)
607 // clear any old reservation
608 if (srv
->reserved()) {
609 reservations
.erase(srv
->reservationId
);
610 srv
->clearReservation();
614 reservations
.insert(Reservations::value_type(srv
->reservationId
, srv
));
618 statefulhelper::cancelReservation(const Helper::ReservationId reservation
)
620 const auto it
= reservations
.find(reservation
);
621 if (it
== reservations
.end())
624 helper_stateful_server
*srv
= it
->second
;
625 reservations
.erase(it
);
626 srv
->clearReservation();
628 // schedule a queue kick
629 AsyncCall::Pointer call
= asyncCall(5,4, "helperStatefulServerDone", cbdataDialer(helperStatefulServerDone
, srv
));
630 ScheduleCallHere(call
);
633 helper_stateful_server
*
634 statefulhelper::findServer(const Helper::ReservationId
& reservation
)
636 const auto it
= reservations
.find(reservation
);
637 if (it
== reservations
.end())
643 helper_stateful_server::reserve()
645 assert(!reservationId
);
646 reservationStart
= squid_curtime
;
647 reservationId
= Helper::ReservationId::Next();
648 debugs(84, 3, "srv-" << index
<< " reservation id = " << reservationId
);
652 helper_stateful_server::clearReservation()
654 debugs(84, 3, "srv-" << index
<< " reservation id = " << reservationId
);
660 reservationId
.clear();
661 reservationStart
= 0;
665 statefulhelper::submit(const char *buf
, HLPCB
* callback
, void *data
, const Helper::ReservationId
& reservation
)
667 Helper::Xaction
*r
= new Helper::Xaction(callback
, data
, buf
);
669 if (buf
&& reservation
) {
670 debugs(84, 5, reservation
);
671 helper_stateful_server
*lastServer
= findServer(reservation
);
673 debugs(84, DBG_CRITICAL
, "ERROR: Helper " << id_name
<< " reservation expired (" << reservation
<< ")");
674 r
->reply
.result
= Helper::TimedOut
;
679 debugs(84, 5, "StatefulSubmit dispatching");
680 helperStatefulDispatch(lastServer
, r
);
682 helper_stateful_server
*srv
;
683 if ((srv
= StatefulGetFirstAvailable(this))) {
685 helperStatefulDispatch(srv
, r
); // may delete r
687 StatefulEnqueue(this, r
);
690 // r may be dangling here
695 Helper::Client::packStatsInto(Packable
* const p
, const char * const label
) const
698 p
->appendf("%s:\n", label
);
700 p
->appendf(" program: %s\n", cmdline
->key
);
701 p
->appendf(" number active: %d of %d (%d shutting down)\n", childs
.n_active
, childs
.n_max
, (childs
.n_running
- childs
.n_active
));
702 p
->appendf(" requests sent: %d\n", stats
.requests
);
703 p
->appendf(" replies received: %d\n", stats
.replies
);
704 p
->appendf(" requests timedout: %d\n", stats
.timedout
);
705 p
->appendf(" queue length: %d\n", stats
.queue_size
);
706 p
->appendf(" avg service time: %d msec\n", stats
.avg_svc_time
);
708 p
->appendf("%7s\t%7s\t%7s\t%11s\t%11s\t%11s\t%6s\t%7s\t%7s\t%7s\n",
720 for (dlink_node
*link
= servers
.head
; link
; link
= link
->next
) {
721 const auto srv
= static_cast<SessionBase
*>(link
->data
);
723 const auto xaction
= srv
->requests
.empty() ? nullptr : srv
->requests
.front();
724 double tt
= 0.001 * (xaction
? tvSubMsec(xaction
->request
.dispatch_time
, current_time
) : tvSubMsec(srv
->dispatch_time
, srv
->answer_time
));
725 p
->appendf("%7u\t%7d\t%7d\t%11" PRIu64
"\t%11" PRIu64
"\t%11" PRIu64
"\t%c%c%c%c%c%c\t%7.3f\t%7d\t%s\n",
732 srv
->stats
.pending
? 'B' : ' ',
733 srv
->flags
.writing
? 'W' : ' ',
734 srv
->flags
.closing
? 'C' : ' ',
735 srv
->reserved() ? 'R' : ' ',
736 srv
->flags
.shutdown
? 'S' : ' ',
737 xaction
&& xaction
->request
.placeholder
? 'P' : ' ',
740 xaction
? Format::QuoteMimeBlob(xaction
->request
.buf
) : "(none)");
743 p
->append("\nFlags key:\n"
748 " S\tSHUTDOWN PENDING\n"
749 " P\tPLACEHOLDER\n", 101);
753 Helper::Client::willOverload() const {
754 return queueFull() && !(childs
.needNew() || GetFirstAvailable(this));
757 Helper::Client::Pointer
758 Helper::Client::Make(const char * const name
)
760 return new Client(name
);
763 statefulhelper::Pointer
764 statefulhelper::Make(const char *name
)
766 return new statefulhelper(name
);
770 helperShutdown(const Helper::Client::Pointer
&hlp
)
772 dlink_node
*link
= hlp
->servers
.head
;
775 const auto srv
= static_cast<Helper::Session
*>(link
->data
);
778 if (srv
->flags
.shutdown
) {
779 debugs(84, 3, "helperShutdown: " << hlp
->id_name
<< " #" << srv
->index
<< " has already SHUT DOWN.");
783 assert(hlp
->childs
.n_active
> 0);
784 -- hlp
->childs
.n_active
;
785 srv
->flags
.shutdown
= true; /* request it to shut itself down */
787 if (srv
->flags
.closing
) {
788 debugs(84, 3, "helperShutdown: " << hlp
->id_name
<< " #" << srv
->index
<< " is CLOSING.");
792 if (srv
->stats
.pending
) {
793 debugs(84, 3, "helperShutdown: " << hlp
->id_name
<< " #" << srv
->index
<< " is BUSY.");
797 debugs(84, 3, "helperShutdown: " << hlp
->id_name
<< " #" << srv
->index
<< " shutting down.");
798 /* the rest of the details is dealt with in the helperServerFree
801 srv
->closePipesSafely();
804 Assure(!hlp
->childs
.n_active
);
809 helperStatefulShutdown(const statefulhelper::Pointer
&hlp
)
811 dlink_node
*link
= hlp
->servers
.head
;
812 helper_stateful_server
*srv
;
815 srv
= (helper_stateful_server
*)link
->data
;
818 if (srv
->flags
.shutdown
) {
819 debugs(84, 3, "helperStatefulShutdown: " << hlp
->id_name
<< " #" << srv
->index
<< " has already SHUT DOWN.");
823 assert(hlp
->childs
.n_active
> 0);
824 -- hlp
->childs
.n_active
;
825 srv
->flags
.shutdown
= true; /* request it to shut itself down */
827 if (srv
->stats
.pending
) {
828 debugs(84, 3, "helperStatefulShutdown: " << hlp
->id_name
<< " #" << srv
->index
<< " is BUSY.");
832 if (srv
->flags
.closing
) {
833 debugs(84, 3, "helperStatefulShutdown: " << hlp
->id_name
<< " #" << srv
->index
<< " is CLOSING.");
837 if (srv
->reserved()) {
839 debugs(84, 3, "helperStatefulShutdown: " << hlp
->id_name
<< " #" << srv
->index
<< " is RESERVED. Closing anyway.");
841 debugs(84, 3, "helperStatefulShutdown: " << hlp
->id_name
<< " #" << srv
->index
<< " is RESERVED. Not Shutting Down Yet.");
846 debugs(84, 3, "helperStatefulShutdown: " << hlp
->id_name
<< " #" << srv
->index
<< " shutting down.");
848 /* the rest of the details is dealt with in the helperStatefulServerFree
851 srv
->closePipesSafely();
855 Helper::Client::~Client()
857 /* note, don't free id_name, it probably points to static memory */
859 // A non-empty queue would leak Helper::Xaction objects, stalling any
860 // pending (and even future collapsed) transactions. To avoid stalling
861 // transactions, we must dropQueued(). We ought to do that when we
862 // discover that no progress is possible rather than here because
863 // reference counting may keep this object alive for a long time.
864 assert(queue
.empty());
868 Helper::Client::handleKilledServer(SessionBase
* const srv
)
870 if (!srv
->flags
.shutdown
) {
871 assert(childs
.n_active
> 0);
873 debugs(84, DBG_CRITICAL
, "WARNING: " << id_name
<< " #" << srv
->index
<< " exited");
875 handleFewerServers(srv
->stats
.replies
>= 1);
877 if (childs
.needNew() > 0) {
878 srv
->flags
.shutdown
= true;
883 if (!childs
.n_active
)
888 Helper::Client::dropQueued()
893 Assure(!childs
.n_active
);
894 Assure(!GetFirstAvailable(this));
896 // no helper servers means nobody can advance our queued transactions
898 debugs(80, DBG_CRITICAL
, "ERROR: Dropping " << queue
.size() << ' ' <<
899 id_name
<< " helper requests due to lack of helper processes");
900 // similar to SessionBase::dropQueued()
901 while (const auto r
= nextRequest()) {
902 r
->reply
.result
= Helper::Unknown
;
909 Helper::Client::handleFewerServers(const bool madeProgress
)
911 const auto needNew
= childs
.needNew();
914 return; // some server(s) have died, but we still have enough
916 debugs(80, DBG_IMPORTANT
, "Too few " << id_name
<< " processes are running (need " << needNew
<< "/" << childs
.n_max
<< ")" <<
917 Debug::Extra
<< "active processes: " << childs
.n_active
<<
918 Debug::Extra
<< "processes configured to start at (re)configuration: " << childs
.n_startup
);
920 if (childs
.n_active
< childs
.n_startup
&& last_restart
> squid_curtime
- 30) {
922 debugs(80, DBG_CRITICAL
, "ERROR: The " << id_name
<< " helpers are crashing too rapidly, need help!");
924 fatalf("The %s helpers are crashing too rapidly, need help!", id_name
);
929 Helper::SessionBase::HelperServerClosed(SessionBase
* const srv
)
931 srv
->helper().handleKilledServer(srv
);
937 Helper::Session::popRequest(const int request_number
)
939 Xaction
*r
= nullptr;
940 if (parent
->childs
.concurrency
) {
941 // If concurrency supported retrieve request from ID
942 const auto it
= requestsIndex
.find(request_number
);
943 if (it
!= requestsIndex
.end()) {
945 requests
.erase(it
->second
);
946 requestsIndex
.erase(it
);
948 } else if(!requests
.empty()) {
949 // Else get the first request from queue, if any
950 r
= requests
.front();
951 requests
.pop_front();
957 /// Calls back with a pointer to the buffer with the helper output
959 helperReturnBuffer(Helper::Session
* srv
, const Helper::Client::Pointer
&hlp
, char * const msg
, const size_t msgSize
, const char * const msgEnd
)
961 if (Helper::Xaction
*r
= srv
->replyXaction
) {
962 const bool hasSpace
= r
->reply
.accumulate(msg
, msgSize
);
964 debugs(84, DBG_IMPORTANT
, "ERROR: Disconnecting from a " <<
965 "helper that overflowed " << srv
->rbuf_sz
<< "-byte " <<
966 "Squid input buffer: " << hlp
->id_name
<< " #" << srv
->index
);
967 srv
->closePipesSafely();
972 return; // We are waiting for more data.
975 if (cbdataReferenceValid(r
->request
.data
)) {
977 if (r
->reply
.result
== Helper::BrokenHelper
&& r
->request
.retries
< MAX_RETRIES
) {
978 debugs(84, DBG_IMPORTANT
, "ERROR: helper: " << r
->reply
<< ", attempt #" << (r
->request
.retries
+ 1) << " of 2");
985 -- srv
->stats
.pending
;
986 ++ srv
->stats
.replies
;
988 ++ hlp
->stats
.replies
;
990 srv
->answer_time
= current_time
;
992 srv
->dispatch_time
= r
->request
.dispatch_time
;
994 hlp
->stats
.avg_svc_time
=
995 Math::intAverage(hlp
->stats
.avg_svc_time
,
996 tvSubMsec(r
->request
.dispatch_time
, current_time
),
997 hlp
->stats
.replies
, REDIRECT_AV_FACTOR
);
999 // release or re-submit parsedRequestXaction object
1000 srv
->replyXaction
= nullptr;
1002 ++r
->request
.retries
;
1003 hlp
->submitRequest(r
);
1008 if (hlp
->timeout
&& hlp
->childs
.concurrency
)
1009 srv
->checkForTimedOutRequests(hlp
->retryTimedOut
);
1011 if (!srv
->flags
.shutdown
) {
1012 helperKickQueue(hlp
);
1013 } else if (!srv
->flags
.closing
&& !srv
->stats
.pending
) {
1014 srv
->closeWritePipeSafely();
1019 helperHandleRead(const Comm::ConnectionPointer
&conn
, char *, size_t len
, Comm::Flag flag
, int, void *data
)
1021 const auto srv
= static_cast<Helper::Session
*>(data
);
1022 const auto hlp
= srv
->parent
;
1023 assert(cbdataReferenceValid(data
));
1025 /* Bail out early on Comm::ERR_CLOSING - close handlers will tidy up for us */
1027 if (flag
== Comm::ERR_CLOSING
) {
1031 assert(conn
->fd
== srv
->readPipe
->fd
);
1033 debugs(84, 5, "helperHandleRead: " << len
<< " bytes from " << hlp
->id_name
<< " #" << srv
->index
);
1035 if (flag
!= Comm::OK
|| len
== 0) {
1036 srv
->closePipesSafely();
1040 srv
->roffset
+= len
;
1041 srv
->rbuf
[srv
->roffset
] = '\0';
1042 debugs(84, DBG_DATA
, Raw("accumulated", srv
->rbuf
, srv
->roffset
));
1044 if (!srv
->stats
.pending
&& !srv
->stats
.timedout
) {
1045 /* someone spoke without being spoken to */
1046 debugs(84, DBG_IMPORTANT
, "ERROR: Killing helper process after an unexpected read from " <<
1047 hlp
->id_name
<< " #" << srv
->index
<< ", " << (int)len
<<
1048 " bytes '" << srv
->rbuf
<< "'");
1051 srv
->rbuf
[0] = '\0';
1052 srv
->closePipesSafely();
1056 bool needsMore
= false;
1057 char *msg
= srv
->rbuf
;
1058 while (*msg
&& !needsMore
) {
1060 char *eom
= strchr(msg
, hlp
->eom
);
1063 debugs(84, 3, "helperHandleRead: end of reply found");
1064 if (eom
> msg
&& eom
[-1] == '\r' && hlp
->eom
== '\n') {
1066 // rewind to the \r octet which is the real terminal now
1067 // and remember that we have to skip forward 2 places now.
1074 if (!srv
->ignoreToEom
&& !srv
->replyXaction
) {
1076 if (hlp
->childs
.concurrency
) {
1078 i
= strtol(msg
, &e
, 10);
1079 // Do we need to check for e == msg? Means wrong response from helper.
1080 // Will be dropped as "unexpected reply on channel 0"
1081 needsMore
= !(xisspace(*e
) || (eom
&& e
== eom
));
1084 while (*msg
&& xisspace(*msg
))
1086 } // else not enough data to compute request number
1088 if (!(srv
->replyXaction
= srv
->popRequest(i
))) {
1089 if (srv
->stats
.timedout
) {
1090 debugs(84, 3, "Timedout reply received for request-ID: " << i
<< " , ignore");
1092 debugs(84, DBG_IMPORTANT
, "ERROR: helperHandleRead: unexpected reply on channel " <<
1093 i
<< " from " << hlp
->id_name
<< " #" << srv
->index
<<
1094 " '" << srv
->rbuf
<< "'");
1096 srv
->ignoreToEom
= true;
1098 } // else we need to just append reply data to the current Xaction
1101 size_t msgSize
= eom
? eom
- msg
: (srv
->roffset
- (msg
- srv
->rbuf
));
1102 assert(msgSize
<= srv
->rbuf_sz
);
1103 helperReturnBuffer(srv
, hlp
, msg
, msgSize
, eom
);
1104 msg
+= msgSize
+ skip
;
1105 assert(static_cast<size_t>(msg
- srv
->rbuf
) <= srv
->rbuf_sz
);
1107 // The next message should not ignored.
1108 if (eom
&& srv
->ignoreToEom
)
1109 srv
->ignoreToEom
= false;
1111 assert(skip
== 0 && eom
== nullptr);
1115 size_t msgSize
= (srv
->roffset
- (msg
- srv
->rbuf
));
1116 assert(msgSize
<= srv
->rbuf_sz
);
1117 memmove(srv
->rbuf
, msg
, msgSize
);
1118 srv
->roffset
= msgSize
;
1119 srv
->rbuf
[srv
->roffset
] = '\0';
1121 // All of the responses parsed and msg points at the end of read data
1122 assert(static_cast<size_t>(msg
- srv
->rbuf
) == srv
->roffset
);
1126 if (Comm::IsConnOpen(srv
->readPipe
) && !fd_table
[srv
->readPipe
->fd
].closing()) {
1127 int spaceSize
= srv
->rbuf_sz
- srv
->roffset
- 1;
1128 assert(spaceSize
>= 0);
1130 AsyncCall::Pointer call
= commCbCall(5,4, "helperHandleRead",
1131 CommIoCbPtrFun(helperHandleRead
, srv
));
1132 comm_read(srv
->readPipe
, srv
->rbuf
+ srv
->roffset
, spaceSize
, call
);
1137 helperStatefulHandleRead(const Comm::ConnectionPointer
&conn
, char *, size_t len
, Comm::Flag flag
, int, void *data
)
1140 helper_stateful_server
*srv
= (helper_stateful_server
*)data
;
1141 const auto hlp
= srv
->parent
;
1142 assert(cbdataReferenceValid(data
));
1144 /* Bail out early on Comm::ERR_CLOSING - close handlers will tidy up for us */
1146 if (flag
== Comm::ERR_CLOSING
) {
1150 assert(conn
->fd
== srv
->readPipe
->fd
);
1152 debugs(84, 5, "helperStatefulHandleRead: " << len
<< " bytes from " <<
1153 hlp
->id_name
<< " #" << srv
->index
);
1155 if (flag
!= Comm::OK
|| len
== 0) {
1156 srv
->closePipesSafely();
1160 srv
->roffset
+= len
;
1161 srv
->rbuf
[srv
->roffset
] = '\0';
1162 debugs(84, DBG_DATA
, Raw("accumulated", srv
->rbuf
, srv
->roffset
));
1164 if (srv
->requests
.empty()) {
1165 /* someone spoke without being spoken to */
1166 debugs(84, DBG_IMPORTANT
, "ERROR: Killing helper process after an unexpected read from " <<
1167 hlp
->id_name
<< " #" << srv
->index
<< ", " << (int)len
<<
1168 " bytes '" << srv
->rbuf
<< "'");
1171 srv
->closePipesSafely();
1175 if ((t
= strchr(srv
->rbuf
, hlp
->eom
))) {
1176 debugs(84, 3, "helperStatefulHandleRead: end of reply found");
1178 if (t
> srv
->rbuf
&& t
[-1] == '\r' && hlp
->eom
== '\n') {
1180 // rewind to the \r octet which is the real terminal now
1187 const auto r
= srv
->requests
.front();
1189 if (!r
->reply
.accumulate(srv
->rbuf
, t
? (t
- srv
->rbuf
) : srv
->roffset
)) {
1190 debugs(84, DBG_IMPORTANT
, "ERROR: Disconnecting from a " <<
1191 "helper that overflowed " << srv
->rbuf_sz
<< "-byte " <<
1192 "Squid input buffer: " << hlp
->id_name
<< " #" << srv
->index
);
1193 srv
->closePipesSafely();
1197 * BUG: the below assumes that only one response per read() was received and discards any octets remaining.
1198 * Doing this prohibits concurrency support with multiple replies per read().
1199 * TODO: check that read() setup on these buffers pays attention to roffest!=0
1200 * TODO: check that replies bigger than the buffer are discarded and do not to affect future replies
1205 /* end of reply found */
1206 srv
->requests
.pop_front(); // we already have it in 'r'
1209 if (cbdataReferenceValid(r
->request
.data
)) {
1210 r
->reply
.finalize();
1211 r
->reply
.reservationId
= srv
->reservationId
;
1214 debugs(84, DBG_IMPORTANT
, "StatefulHandleRead: no callback data registered");
1220 -- srv
->stats
.pending
;
1221 ++ srv
->stats
.replies
;
1223 ++ hlp
->stats
.replies
;
1224 srv
->answer_time
= current_time
;
1225 hlp
->stats
.avg_svc_time
=
1226 Math::intAverage(hlp
->stats
.avg_svc_time
,
1227 tvSubMsec(srv
->dispatch_time
, current_time
),
1228 hlp
->stats
.replies
, REDIRECT_AV_FACTOR
);
1231 helperStatefulServerDone(srv
);
1233 hlp
->cancelReservation(srv
->reservationId
);
1236 if (Comm::IsConnOpen(srv
->readPipe
) && !fd_table
[srv
->readPipe
->fd
].closing()) {
1237 int spaceSize
= srv
->rbuf_sz
- 1;
1239 AsyncCall::Pointer call
= commCbCall(5,4, "helperStatefulHandleRead",
1240 CommIoCbPtrFun(helperStatefulHandleRead
, srv
));
1241 comm_read(srv
->readPipe
, srv
->rbuf
, spaceSize
, call
);
1245 /// Handles a request when all running helpers, if any, are busy.
1247 Enqueue(Helper::Client
* const hlp
, Helper::Xaction
* const r
)
1250 ++ hlp
->stats
.queue_size
;
1252 /* do this first so idle=N has a chance to grow the child pool before it hits critical. */
1253 if (hlp
->childs
.needNew() > 0) {
1254 hlp
->openSessions();
1258 if (hlp
->stats
.queue_size
< (int)hlp
->childs
.queue_size
)
1261 if (squid_curtime
- hlp
->last_queue_warn
< 600)
1264 if (shutting_down
|| reconfiguring
)
1267 hlp
->last_queue_warn
= squid_curtime
;
1269 debugs(84, DBG_CRITICAL
, "WARNING: All " << hlp
->childs
.n_active
<< "/" << hlp
->childs
.n_max
<< " " << hlp
->id_name
<< " processes are busy.");
1270 debugs(84, DBG_CRITICAL
, "WARNING: " << hlp
->stats
.queue_size
<< " pending requests queued");
1271 debugs(84, DBG_CRITICAL
, "WARNING: Consider increasing the number of " << hlp
->id_name
<< " processes in your config file.");
1275 StatefulEnqueue(statefulhelper
* hlp
, Helper::Xaction
* r
)
1278 ++ hlp
->stats
.queue_size
;
1280 /* do this first so idle=N has a chance to grow the child pool before it hits critical. */
1281 if (hlp
->childs
.needNew() > 0) {
1282 hlp
->openSessions();
1286 if (hlp
->stats
.queue_size
< (int)hlp
->childs
.queue_size
)
1289 if (squid_curtime
- hlp
->last_queue_warn
< 600)
1292 if (shutting_down
|| reconfiguring
)
1295 hlp
->last_queue_warn
= squid_curtime
;
1297 debugs(84, DBG_CRITICAL
, "WARNING: All " << hlp
->childs
.n_active
<< "/" << hlp
->childs
.n_max
<< " " << hlp
->id_name
<< " processes are busy.");
1298 debugs(84, DBG_CRITICAL
, "WARNING: " << hlp
->stats
.queue_size
<< " pending requests queued");
1299 debugs(84, DBG_CRITICAL
, "WARNING: Consider increasing the number of " << hlp
->id_name
<< " processes in your config file.");
1303 Helper::Client::nextRequest()
1308 auto *r
= queue
.front();
1314 static Helper::Session
*
1315 GetFirstAvailable(const Helper::Client::Pointer
&hlp
)
1318 Helper::Session
*selected
= nullptr;
1319 debugs(84, 5, "GetFirstAvailable: Running servers " << hlp
->childs
.n_running
);
1321 if (hlp
->childs
.n_running
== 0)
1324 /* Find "least" loaded helper (approx) */
1325 for (n
= hlp
->servers
.head
; n
!= nullptr; n
= n
->next
) {
1326 const auto srv
= static_cast<Helper::Session
*>(n
->data
);
1328 if (selected
&& selected
->stats
.pending
<= srv
->stats
.pending
)
1331 if (srv
->flags
.shutdown
)
1334 if (!srv
->stats
.pending
)
1346 debugs(84, 5, "GetFirstAvailable: None available.");
1350 if (selected
->stats
.pending
>= (hlp
->childs
.concurrency
? hlp
->childs
.concurrency
: 1)) {
1351 debugs(84, 3, "GetFirstAvailable: Least-loaded helper is fully loaded!");
1355 debugs(84, 5, "GetFirstAvailable: returning srv-" << selected
->index
);
1359 static helper_stateful_server
*
1360 StatefulGetFirstAvailable(const statefulhelper::Pointer
&hlp
)
1363 helper_stateful_server
*srv
= nullptr;
1364 helper_stateful_server
*oldestReservedServer
= nullptr;
1365 debugs(84, 5, "StatefulGetFirstAvailable: Running servers " << hlp
->childs
.n_running
);
1367 if (hlp
->childs
.n_running
== 0)
1370 for (n
= hlp
->servers
.head
; n
!= nullptr; n
= n
->next
) {
1371 srv
= (helper_stateful_server
*)n
->data
;
1373 if (srv
->stats
.pending
)
1376 if (srv
->reserved()) {
1377 if ((squid_curtime
- srv
->reservationStart
) > hlp
->childs
.reservationTimeout
) {
1378 if (!oldestReservedServer
)
1379 oldestReservedServer
= srv
;
1380 else if (oldestReservedServer
->reservationStart
< srv
->reservationStart
)
1381 oldestReservedServer
= srv
;
1382 debugs(84, 5, "the earlier reserved server is the srv-" << oldestReservedServer
->index
);
1387 if (srv
->flags
.shutdown
)
1390 debugs(84, 5, "StatefulGetFirstAvailable: returning srv-" << srv
->index
);
1394 if (oldestReservedServer
) {
1395 debugs(84, 5, "expired reservation " << oldestReservedServer
->reservationId
<< " for srv-" << oldestReservedServer
->index
);
1396 return oldestReservedServer
;
1399 debugs(84, 5, "StatefulGetFirstAvailable: None available.");
1404 helperDispatchWriteDone(const Comm::ConnectionPointer
&, char *, size_t, Comm::Flag flag
, int, void *data
)
1406 const auto srv
= static_cast<Helper::Session
*>(data
);
1408 srv
->writebuf
->clean();
1409 delete srv
->writebuf
;
1410 srv
->writebuf
= nullptr;
1411 srv
->flags
.writing
= false;
1413 if (flag
!= Comm::OK
) {
1414 /* Helper server has crashed */
1415 debugs(84, DBG_CRITICAL
, "helperDispatch: Helper " << srv
->parent
->id_name
<< " #" << srv
->index
<< " has crashed");
1419 if (!srv
->wqueue
->isNull()) {
1420 srv
->writebuf
= srv
->wqueue
;
1421 srv
->wqueue
= new MemBuf
;
1422 srv
->flags
.writing
= true;
1423 AsyncCall::Pointer call
= commCbCall(5,5, "helperDispatchWriteDone",
1424 CommIoCbPtrFun(helperDispatchWriteDone
, srv
));
1425 Comm::Write(srv
->writePipe
, srv
->writebuf
->content(), srv
->writebuf
->contentSize(), call
, nullptr);
1430 helperDispatch(Helper::Session
* const srv
, Helper::Xaction
* const r
)
1432 const auto hlp
= srv
->parent
;
1433 const uint64_t reqId
= ++srv
->nextRequestId
;
1435 if (!cbdataReferenceValid(r
->request
.data
)) {
1436 debugs(84, DBG_IMPORTANT
, "ERROR: helperDispatch: invalid callback data");
1441 r
->request
.Id
= reqId
;
1442 const auto it
= srv
->requests
.insert(srv
->requests
.end(), r
);
1443 r
->request
.dispatch_time
= current_time
;
1445 if (srv
->wqueue
->isNull())
1446 srv
->wqueue
->init();
1448 if (hlp
->childs
.concurrency
) {
1449 srv
->requestsIndex
.insert(Helper::Session::RequestIndex::value_type(reqId
, it
));
1450 assert(srv
->requestsIndex
.size() == srv
->requests
.size());
1451 srv
->wqueue
->appendf("%" PRIu64
" %s", reqId
, r
->request
.buf
);
1453 srv
->wqueue
->append(r
->request
.buf
, strlen(r
->request
.buf
));
1455 if (!srv
->flags
.writing
) {
1456 assert(nullptr == srv
->writebuf
);
1457 srv
->writebuf
= srv
->wqueue
;
1458 srv
->wqueue
= new MemBuf
;
1459 srv
->flags
.writing
= true;
1460 AsyncCall::Pointer call
= commCbCall(5,5, "helperDispatchWriteDone",
1461 CommIoCbPtrFun(helperDispatchWriteDone
, srv
));
1462 Comm::Write(srv
->writePipe
, srv
->writebuf
->content(), srv
->writebuf
->contentSize(), call
, nullptr);
1465 debugs(84, 5, "helperDispatch: Request sent to " << hlp
->id_name
<< " #" << srv
->index
<< ", " << strlen(r
->request
.buf
) << " bytes");
1468 ++ srv
->stats
.pending
;
1469 ++ hlp
->stats
.requests
;
1473 helperStatefulDispatchWriteDone(const Comm::ConnectionPointer
&, char *, size_t, Comm::Flag
, int, void *)
1477 helperStatefulDispatch(helper_stateful_server
* srv
, Helper::Xaction
* r
)
1479 const auto hlp
= srv
->parent
;
1481 if (!cbdataReferenceValid(r
->request
.data
)) {
1482 debugs(84, DBG_IMPORTANT
, "ERROR: helperStatefulDispatch: invalid callback data");
1484 hlp
->cancelReservation(srv
->reservationId
);
1488 debugs(84, 9, "helperStatefulDispatch busying helper " << hlp
->id_name
<< " #" << srv
->index
);
1490 assert(srv
->reservationId
);
1491 r
->reply
.reservationId
= srv
->reservationId
;
1493 if (r
->request
.placeholder
== 1) {
1494 /* a callback is needed before this request can _use_ a helper. */
1495 /* we don't care about releasing this helper. The request NEVER
1496 * gets to the helper. So we throw away the return code */
1497 r
->reply
.result
= Helper::Unknown
;
1499 /* throw away the placeholder */
1501 /* and push the queue. Note that the callback may have submitted a new
1502 * request to the helper which is why we test for the request */
1504 if (!srv
->requests
.size())
1505 helperStatefulServerDone(srv
);
1510 srv
->requests
.push_back(r
);
1511 srv
->dispatch_time
= current_time
;
1512 AsyncCall::Pointer call
= commCbCall(5,5, "helperStatefulDispatchWriteDone",
1513 CommIoCbPtrFun(helperStatefulDispatchWriteDone
, srv
));
1514 Comm::Write(srv
->writePipe
, r
->request
.buf
, strlen(r
->request
.buf
), call
, nullptr);
1515 debugs(84, 5, "helperStatefulDispatch: Request sent to " <<
1516 hlp
->id_name
<< " #" << srv
->index
<< ", " <<
1517 (int) strlen(r
->request
.buf
) << " bytes");
1520 ++ srv
->stats
.pending
;
1521 ++ hlp
->stats
.requests
;
1525 helperKickQueue(const Helper::Client::Pointer
&hlp
)
1527 Helper::Xaction
*r
= nullptr;
1528 Helper::Session
*srv
= nullptr;
1530 while ((srv
= GetFirstAvailable(hlp
)) && (r
= hlp
->nextRequest()))
1531 helperDispatch(srv
, r
);
1533 if (!hlp
->childs
.n_active
)
1538 helperStatefulKickQueue(const statefulhelper::Pointer
&hlp
)
1541 helper_stateful_server
*srv
;
1542 while ((srv
= StatefulGetFirstAvailable(hlp
)) && (r
= hlp
->nextRequest())) {
1543 debugs(84, 5, "found srv-" << srv
->index
);
1544 hlp
->reserveServer(srv
);
1545 helperStatefulDispatch(srv
, r
);
1548 if (!hlp
->childs
.n_active
)
1553 helperStatefulServerDone(helper_stateful_server
* srv
)
1555 if (!srv
->flags
.shutdown
) {
1556 helperStatefulKickQueue(srv
->parent
);
1557 } else if (!srv
->flags
.closing
&& !srv
->reserved() && !srv
->stats
.pending
) {
1558 srv
->closeWritePipeSafely();
1564 Helper::Session::checkForTimedOutRequests(bool const retry
)
1566 assert(parent
->childs
.concurrency
);
1567 while(!requests
.empty() && requests
.front()->request
.timedOut(parent
->timeout
)) {
1568 const auto r
= requests
.front();
1569 RequestIndex::iterator it
;
1570 it
= requestsIndex
.find(r
->request
.Id
);
1571 assert(it
!= requestsIndex
.end());
1572 requestsIndex
.erase(it
);
1573 requests
.pop_front();
1574 debugs(84, 2, "Request " << r
->request
.Id
<< " timed-out, remove it from queue");
1575 bool retried
= false;
1576 if (retry
&& r
->request
.retries
< MAX_RETRIES
&& cbdataReferenceValid(r
->request
.data
)) {
1577 debugs(84, 2, "Retry request " << r
->request
.Id
);
1578 ++r
->request
.retries
;
1579 parent
->submitRequest(r
);
1581 } else if (cbdataReferenceValid(r
->request
.data
)) {
1582 if (!parent
->onTimedOutResponse
.isEmpty()) {
1583 if (r
->reply
.accumulate(parent
->onTimedOutResponse
.rawContent(), parent
->onTimedOutResponse
.length()))
1584 r
->reply
.finalize();
1586 r
->reply
.result
= Helper::TimedOut
;
1587 parent
->callBack(*r
);
1589 r
->reply
.result
= Helper::TimedOut
;
1590 parent
->callBack(*r
);
1595 ++parent
->stats
.timedout
;
1602 Helper::Session::requestTimeout(const CommTimeoutCbParams
&io
)
1604 debugs(26, 3, io
.conn
);
1605 const auto srv
= static_cast<Session
*>(io
.data
);
1607 srv
->checkForTimedOutRequests(srv
->parent
->retryTimedOut
);
1609 debugs(84, 3, io
.conn
<< " establish a new timeout");
1610 AsyncCall::Pointer timeoutCall
= commCbCall(84, 4, "Helper::Session::requestTimeout",
1611 CommTimeoutCbPtrFun(Session::requestTimeout
, srv
));
1613 const time_t timeSpent
= srv
->requests
.empty() ? 0 : (squid_curtime
- srv
->requests
.front()->request
.dispatch_time
.tv_sec
);
1614 const time_t minimumNewTimeout
= 1; // second
1615 const auto timeLeft
= max(minimumNewTimeout
, srv
->parent
->timeout
- timeSpent
);
1617 commSetConnTimeout(io
.conn
, timeLeft
, timeoutCall
);