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