]>
Commit | Line | Data |
---|---|---|
774c051c | 1 | |
2 | /* | |
774c051c | 3 | * |
4 | * SQUID Web Proxy Cache http://www.squid-cache.org/ | |
5 | * ---------------------------------------------------------- | |
6 | * | |
7 | * Squid is the result of efforts by numerous individuals from | |
8 | * the Internet community; see the CONTRIBUTORS file for full | |
9 | * details. Many organizations have provided support for Squid's | |
10 | * development; see the SPONSORS file for full details. Squid is | |
11 | * Copyrighted (C) 2001 by the Regents of the University of | |
12 | * California; see the COPYRIGHT file for full details. Squid | |
13 | * incorporates software developed and/or copyrighted by other | |
33ff9dbf | 14 | * sources; see the CREDITS file for full details. |
774c051c | 15 | * |
16 | * This program is free software; you can redistribute it and/or modify | |
17 | * it under the terms of the GNU General Public License as published by | |
18 | * the Free Software Foundation; either version 2 of the License, or | |
19 | * (at your option) any later version. | |
9e008dda | 20 | * |
774c051c | 21 | * This program is distributed in the hope that it will be useful, |
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
24 | * GNU General Public License for more details. | |
9e008dda | 25 | * |
774c051c | 26 | * You should have received a copy of the GNU General Public License |
27 | * along with this program; if not, write to the Free Software | |
28 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |
29 | * | |
30 | */ | |
31 | ||
32 | #ifndef SQUID_ICAPSERVICEREP_H | |
33 | #define SQUID_ICAPSERVICEREP_H | |
34 | ||
d81a31f1 | 35 | #include "adaptation/forward.h" |
26cc52cb | 36 | #include "adaptation/icap/Elements.h" |
602d9612 A |
37 | #include "adaptation/Initiator.h" |
38 | #include "adaptation/Service.h" | |
2dba5b8e | 39 | #include "base/AsyncJobCalls.h" |
602d9612 | 40 | #include "cbdata.h" |
2dba5b8e | 41 | #include "comm.h" |
602d9612 | 42 | #include "FadingCounter.h" |
2dba5b8e CT |
43 | #include "pconn.h" |
44 | #include <deque> | |
26cc52cb | 45 | |
af6a12ee AJ |
46 | namespace Adaptation |
47 | { | |
e1381638 AJ |
48 | namespace Icap |
49 | { | |
26cc52cb AR |
50 | |
51 | class Options; | |
52 | class OptXact; | |
774c051c | 53 | |
54 | /* The ICAP service representative maintains information about a single ICAP | |
55 | service that Squid communicates with. The representative initiates OPTIONS | |
56 | requests to the service to keep cached options fresh. One ICAP server may | |
c99de607 | 57 | host many ICAP services. */ |
58 | ||
59 | /* | |
1299ecbf | 60 | * A service with a fresh cached OPTIONS response and without many failures |
61 | * is an "up" service. All other services are "down". A service is "probed" | |
62 | * if we tried to get an OPTIONS response from it and succeeded or failed. | |
63 | * A probed down service is called "broken". | |
64 | * | |
65 | * The number of failures required to bring an up service down is determined | |
66 | * by icap_service_failure_limit in squid.conf. | |
c99de607 | 67 | * |
68 | * As a bootstrapping mechanism, ICAP transactions wait for an unprobed | |
69 | * service to get a fresh OPTIONS response (see the callWhenReady method). | |
70 | * The waiting callback is called when the OPTIONS transaction completes, | |
71 | * even if the service is now broken. | |
72 | * | |
73 | * We do not initiate ICAP transactions with a broken service, but will | |
74 | * eventually retry to fetch its options in hope to bring the service up. | |
75 | * | |
76 | * A service that should no longer be used after Squid reconfiguration is | |
9e008dda AJ |
77 | * treated as if it does not have a fresh cached OPTIONS response. We do |
78 | * not try to fetch fresh options for such a service. It should be | |
c99de607 | 79 | * auto-destroyed by refcounting when no longer used. |
80 | */ | |
81 | ||
26cc52cb | 82 | class ServiceRep : public RefCountable, public Adaptation::Service, |
e1381638 | 83 | public Adaptation::Initiator |
774c051c | 84 | { |
85 | ||
86 | public: | |
26cc52cb | 87 | typedef RefCount<ServiceRep> Pointer; |
774c051c | 88 | |
89 | public: | |
6666da11 | 90 | explicit ServiceRep(const ServiceConfigPointer &aConfig); |
26cc52cb | 91 | virtual ~ServiceRep(); |
774c051c | 92 | |
62c7f90e AR |
93 | virtual void finalize(); |
94 | ||
76fc7e57 AJ |
95 | virtual bool probed() const; // see comments above |
96 | virtual bool up() const; // see comments above | |
2dba5b8e CT |
97 | bool availableForNew() const; ///< a new transaction may start communicating with the service |
98 | bool availableForOld() const; ///< a transaction notified about connection slot availability may start communicating with the service | |
774c051c | 99 | |
4299f876 | 100 | virtual Initiate *makeXactLauncher(HttpMsg *virginHeader, HttpRequest *virginCause); |
d81a31f1 | 101 | |
2dba5b8e | 102 | void callWhenAvailable(AsyncCall::Pointer &cb, bool priority = false); |
bd7f2ede | 103 | void callWhenReady(AsyncCall::Pointer &cb); |
774c051c | 104 | |
774c051c | 105 | // the methods below can only be called on an up() service |
30abd221 | 106 | bool wantsUrl(const String &urlPath) const; |
107 | bool wantsPreview(const String &urlPath, size_t &wantedSize) const; | |
774c051c | 108 | bool allows204() const; |
83c51da9 | 109 | bool allows206() const; |
983983ce | 110 | Comm::ConnectionPointer getConnection(bool isRetriable, bool &isReused); |
a32c060f | 111 | void putConnection(const Comm::ConnectionPointer &conn, bool isReusable, bool sendReset, const char *comment); |
983983ce | 112 | void noteConnectionUse(const Comm::ConnectionPointer &conn); |
fb505fa1 | 113 | void noteConnectionFailed(const char *comment); |
774c051c | 114 | |
c99de607 | 115 | void noteFailure(); // called by transactions to report service failure |
9e008dda | 116 | |
2dba5b8e CT |
117 | void noteNewWaiter() {theAllWaiters++;} ///< New xaction waiting for service to be up or available |
118 | void noteGoneWaiter(); ///< An xaction is not waiting any more for service to be available | |
119 | bool existWaiters() const {return (theAllWaiters > 0);} ///< if there are xactions waiting for the service to be available | |
120 | ||
bd7f2ede | 121 | //AsyncJob virtual methods |
d81a31f1 | 122 | virtual bool doneAll() const { return Adaptation::Initiator::doneAll() && false;} |
4299f876 | 123 | virtual void callException(const std::exception &e); |
c99de607 | 124 | |
76fc7e57 AJ |
125 | virtual void detach(); |
126 | virtual bool detached() const; | |
127 | ||
774c051c | 128 | public: // treat these as private, they are for callbacks only |
129 | void noteTimeToUpdate(); | |
130 | void noteTimeToNotify(); | |
c824c43b | 131 | |
132 | // receive either an ICAP OPTIONS response header or an abort message | |
3af10ac0 | 133 | virtual void noteAdaptationAnswer(const Answer &answer); |
774c051c | 134 | |
135 | private: | |
136 | // stores Prepare() callback info | |
137 | ||
9e008dda | 138 | struct Client { |
774c051c | 139 | Pointer service; // one for each client to preserve service |
bd7f2ede | 140 | AsyncCall::Pointer callback; |
774c051c | 141 | }; |
142 | ||
143 | typedef Vector<Client> Clients; | |
2dba5b8e | 144 | // TODO: rename to theUpWaiters |
774c051c | 145 | Clients theClients; // all clients waiting for a call back |
146 | ||
26cc52cb | 147 | Options *theOptions; |
4299f876 | 148 | CbcPointer<Adaptation::Initiate> theOptionsFetcher; // pending ICAP OPTIONS transaction |
c99de607 | 149 | time_t theLastUpdate; // time the options were last updated |
150 | ||
2dba5b8e CT |
151 | /// FIFO queue of xactions waiting for a connection slot and not yet notified |
152 | /// about it; xaction is removed when notification is scheduled | |
153 | std::deque<Client> theNotificationWaiters; | |
154 | int theBusyConns; ///< number of connections given to active transactions | |
155 | /// number of xactions waiting for a connection slot (notified and not) | |
156 | /// the number is decreased after the xaction receives notification | |
157 | int theAllWaiters; | |
158 | int theMaxConnections; ///< the maximum allowed connections to the service | |
159 | // TODO: use a better type like the FadingCounter for connOverloadReported | |
160 | mutable bool connOverloadReported; ///< whether we reported exceeding theMaxConnections | |
fb505fa1 | 161 | IdleConnList *theIdleConns; ///< idle persistent connection pool |
2dba5b8e | 162 | |
8277060a | 163 | FadingCounter theSessionFailures; |
c99de607 | 164 | const char *isSuspended; // also stores suspension reason for debugging |
774c051c | 165 | |
774c051c | 166 | bool notifying; // may be true in any state except for the initial |
c99de607 | 167 | bool updateScheduled; // time-based options update has been scheduled |
774c051c | 168 | |
169 | private: | |
170 | ICAP::Method parseMethod(const char *) const; | |
171 | ICAP::VectPoint parseVectPoint(const char *) const; | |
172 | ||
c99de607 | 173 | void suspend(const char *reason); |
174 | ||
175 | bool hasOptions() const; | |
774c051c | 176 | bool needNewOptions() const; |
1299ecbf | 177 | time_t optionsFetchTime() const; |
774c051c | 178 | |
1299ecbf | 179 | void scheduleUpdate(time_t when); |
774c051c | 180 | void scheduleNotification(); |
c99de607 | 181 | |
774c051c | 182 | void startGettingOptions(); |
26cc52cb AR |
183 | void handleNewOptions(Options *newOptions); |
184 | void changeOptions(Options *newOptions); | |
c99de607 | 185 | void checkOptions(); |
186 | ||
187 | void announceStatusChange(const char *downPhrase, bool important) const; | |
774c051c | 188 | |
2dba5b8e CT |
189 | /// Set the maximum allowed connections for the service |
190 | void setMaxConnections(); | |
191 | /// The number of connections which excess the Max-Connections limit | |
192 | int excessConnections() const; | |
193 | /** | |
194 | * The available connections slots to the ICAP server | |
195 | \return the available slots, or -1 if there is no limit on allowed connections | |
196 | */ | |
197 | int availableConnections() const; | |
198 | /** | |
199 | * If there are xactions waiting for the service to be available, notify | |
200 | * as many xactions as the available connections slots. | |
201 | */ | |
202 | void busyCheckpoint(); | |
203 | ||
774c051c | 204 | const char *status() const; |
205 | ||
c99de607 | 206 | mutable bool wasAnnouncedUp; // prevent sequential same-state announcements |
76fc7e57 | 207 | bool isDetached; |
26cc52cb | 208 | CBDATA_CLASS2(ServiceRep); |
774c051c | 209 | }; |
210 | ||
2dba5b8e CT |
211 | class ModXact; |
212 | /// Custom dialer to call Service::noteNewWaiter and noteGoneWaiter | |
213 | /// to maintain Service idea of waiting and being-notified transactions. | |
214 | class ConnWaiterDialer: public NullaryMemFunT<ModXact> | |
215 | { | |
216 | public: | |
217 | typedef NullaryMemFunT<ModXact> Parent; | |
218 | ServiceRep::Pointer theService; | |
219 | ConnWaiterDialer(const CbcPointer<ModXact> &xact, Parent::Method aHandler); | |
220 | ConnWaiterDialer(const Adaptation::Icap::ConnWaiterDialer &aConnWaiter); | |
221 | ~ConnWaiterDialer(); | |
222 | }; | |
26cc52cb AR |
223 | |
224 | } // namespace Icap | |
225 | } // namespace Adaptation | |
226 | ||
774c051c | 227 | #endif /* SQUID_ICAPSERVICEREP_H */ |