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