]> git.ipfire.org Git - thirdparty/squid.git/blob - src/adaptation/icap/Launcher.cc
Merged from parent (trunk 11270, circa 3.2.0.5+)
[thirdparty/squid.git] / src / adaptation / icap / Launcher.cc
1 /*
2 * DEBUG: section 93 ICAP (RFC 3507) Client
3 */
4
5 #include "squid.h"
6 #include "acl/FilledChecklist.h"
7 #include "adaptation/icap/Launcher.h"
8 #include "adaptation/icap/Xaction.h"
9 #include "adaptation/icap/ServiceRep.h"
10 #include "adaptation/icap/Config.h"
11 #include "base/TextException.h"
12 #include "HttpMsg.h"
13 #include "HttpRequest.h"
14 #include "HttpReply.h"
15
16
17 Adaptation::Icap::Launcher::Launcher(const char *aTypeName,
18 Adaptation::ServicePointer &aService):
19 AsyncJob(aTypeName),
20 Adaptation::Initiate(aTypeName),
21 theService(aService), theXaction(0), theLaunches(0)
22 {
23 }
24
25 Adaptation::Icap::Launcher::~Launcher()
26 {
27 assert(!theXaction);
28 }
29
30 void Adaptation::Icap::Launcher::start()
31 {
32 Adaptation::Initiate::start();
33
34 Must(theInitiator.set());
35 launchXaction("first");
36 }
37
38 void Adaptation::Icap::Launcher::launchXaction(const char *xkind)
39 {
40 Must(!theXaction);
41 ++theLaunches;
42 debugs(93,4, HERE << "launching " << xkind << " xaction #" << theLaunches);
43 Adaptation::Icap::Xaction *x = createXaction();
44 x->attempts = theLaunches;
45 if (theLaunches > 1)
46 x->disableRetries();
47 if (theLaunches >= TheConfig.repeat_limit)
48 x->disableRepeats("over icap_retry_limit");
49 theXaction = initiateAdaptation(x);
50 Must(initiated(theXaction));
51 }
52
53 void Adaptation::Icap::Launcher::noteAdaptationAnswer(const Answer &answer)
54 {
55 debugs(93,5, HERE << "launches: " << theLaunches << " answer: " << answer);
56
57 // XXX: akError is unused by ICAPXaction in favor of noteXactAbort()
58 Must(answer.kind != Answer::akError);
59
60 sendAnswer(answer);
61 clearAdaptation(theXaction);
62 Must(done());
63 }
64
65 void Adaptation::Icap::Launcher::noteInitiatorAborted()
66 {
67
68 announceInitiatorAbort(theXaction); // propogate to the transaction
69 clearInitiator();
70 Must(done()); // should be nothing else to do
71
72 }
73
74 void Adaptation::Icap::Launcher::noteXactAbort(XactAbortInfo info)
75 {
76 debugs(93,5, HERE << "theXaction:" << theXaction << " launches: " << theLaunches);
77
78 // TODO: add more checks from FwdState::checkRetry()?
79 if (canRetry(info)) {
80 clearAdaptation(theXaction);
81 launchXaction("retry");
82 } else if (canRepeat(info)) {
83 clearAdaptation(theXaction);
84 launchXaction("repeat");
85 } else {
86 debugs(93,3, HERE << "cannot retry or repeat a failed transaction");
87 clearAdaptation(theXaction);
88 tellQueryAborted(false); // caller decides based on bypass, consumption
89 Must(done());
90 }
91 }
92
93 bool Adaptation::Icap::Launcher::doneAll() const
94 {
95 return (!theInitiator || !theXaction) && Adaptation::Initiate::doneAll();
96 }
97
98 void Adaptation::Icap::Launcher::swanSong()
99 {
100 if (theInitiator.set())
101 tellQueryAborted(true); // always final here because abnormal
102
103 if (theXaction.set())
104 clearAdaptation(theXaction);
105
106 Adaptation::Initiate::swanSong();
107 }
108
109 bool Adaptation::Icap::Launcher::canRetry(Adaptation::Icap::XactAbortInfo &info) const
110 {
111 // We do not check and can exceed zero repeat limit when retrying.
112 // This is by design as the limit does not apply to pconn retrying.
113 return !shutting_down && info.isRetriable;
114 }
115
116 bool Adaptation::Icap::Launcher::canRepeat(Adaptation::Icap::XactAbortInfo &info) const
117 {
118 debugs(93,9, HERE << shutting_down);
119 if (theLaunches >= TheConfig.repeat_limit || shutting_down)
120 return false;
121
122 debugs(93,9, HERE << info.isRepeatable); // TODO: update and use status()
123 if (!info.isRepeatable)
124 return false;
125
126 debugs(93,9, HERE << info.icapReply);
127 if (!info.icapReply) // did not get to read an ICAP reply; a timeout?
128 return true;
129
130 debugs(93,9, HERE << info.icapReply->sline.status);
131 if (!info.icapReply->sline.status) // failed to parse the reply; I/O err
132 return true;
133
134 ACLFilledChecklist *cl =
135 new ACLFilledChecklist(TheConfig.repeat, info.icapRequest, dash_str);
136 cl->reply = HTTPMSGLOCK(info.icapReply);
137
138 const bool result = cl->fastCheck();
139 delete cl;
140 return result;
141 }
142
143 /* ICAPXactAbortInfo */
144
145 Adaptation::Icap::XactAbortInfo::XactAbortInfo(HttpRequest *anIcapRequest,
146 HttpReply *anIcapReply, bool beRetriable, bool beRepeatable):
147 icapRequest(anIcapRequest ? HTTPMSGLOCK(anIcapRequest) : NULL),
148 icapReply(anIcapReply ? HTTPMSGLOCK(anIcapReply) : NULL),
149 isRetriable(beRetriable), isRepeatable(beRepeatable)
150 {
151 }
152
153 Adaptation::Icap::XactAbortInfo::XactAbortInfo(const Adaptation::Icap::XactAbortInfo &i):
154 icapRequest(i.icapRequest ? HTTPMSGLOCK(i.icapRequest) : NULL),
155 icapReply(i.icapReply ? HTTPMSGLOCK(i.icapReply) : NULL),
156 isRetriable(i.isRetriable), isRepeatable(i.isRepeatable)
157 {
158 }
159
160 Adaptation::Icap::XactAbortInfo::~XactAbortInfo()
161 {
162 HTTPMSGUNLOCK(icapRequest);
163 HTTPMSGUNLOCK(icapReply);
164 }