ServiceRep.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
3  *
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.
7  */
8 
9 #ifndef SQUID_ICAPSERVICEREP_H
10 #define SQUID_ICAPSERVICEREP_H
11 
12 #include "adaptation/forward.h"
14 #include "adaptation/Initiator.h"
15 #include "adaptation/Service.h"
16 #include "base/AsyncJobCalls.h"
17 #include "cbdata.h"
18 #include "comm.h"
19 #include "FadingCounter.h"
20 #include "pconn.h"
21 #include <deque>
22 
23 namespace Adaptation
24 {
25 namespace Icap
26 {
27 
28 class Options;
29 class OptXact;
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
34  host many ICAP services. */
35 
36 /*
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.
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
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
56  * auto-destroyed by refcounting when no longer used.
57  */
58 
61 {
63 
64 public:
66 
67 public:
68  explicit ServiceRep(const ServiceConfigPointer &aConfig);
69  virtual ~ServiceRep();
70 
71  virtual void finalize();
72 
73  virtual bool probed() const; // see comments above
74  virtual bool up() const; // see comments above
75  bool availableForNew() const;
76  bool availableForOld() const;
77 
78  virtual Initiate *makeXactLauncher(Http::Message *virginHeader, HttpRequest *virginCause, AccessLogEntry::Pointer &alp);
79 
80  void callWhenAvailable(AsyncCall::Pointer &cb, bool priority = false);
82 
83  // the methods below can only be called on an up() service
84  bool wantsUrl(const SBuf &urlPath) const;
85  bool wantsPreview(const SBuf &urlPath, size_t &wantedSize) const;
86  bool allows204() const;
87  bool allows206() const;
88  Comm::ConnectionPointer getConnection(bool isRetriable, bool &isReused);
89  void putConnection(const Comm::ConnectionPointer &conn, bool isReusable, bool sendReset, const char *comment);
91  void noteConnectionFailed(const char *comment);
92 
93  void noteFailure(); // called by transactions to report service failure
94 
96  void noteGoneWaiter();
97  bool existWaiters() const {return (theAllWaiters > 0);}
98 
99  //AsyncJob virtual methods
100  virtual bool doneAll() const { return Adaptation::Initiator::doneAll() && false;}
101  virtual void callException(const std::exception &e);
102 
103  virtual void detach();
104  virtual bool detached() const;
105 
106 public: // treat these as private, they are for callbacks only
107  void noteTimeToUpdate();
108  void noteTimeToNotify();
109 
110  // receive either an ICAP OPTIONS response header or an abort message
111  virtual void noteAdaptationAnswer(const Answer &answer);
112 
115 
116 private:
117  // stores Prepare() callback info
118 
119  struct Client {
120  Pointer service; // one for each client to preserve service
122  };
123 
124  typedef std::vector<Client> Clients;
125  // TODO: rename to theUpWaiters
126  Clients theClients; // all clients waiting for a call back
127 
129  CbcPointer<Adaptation::Initiate> theOptionsFetcher; // pending ICAP OPTIONS transaction
130  time_t theLastUpdate; // time the options were last updated
131 
134  std::deque<Client> theNotificationWaiters;
136  int theAllWaiters;
140  // TODO: use a better type like the FadingCounter for connOverloadReported
141  mutable bool connOverloadReported;
143 
145  const char *isSuspended; // also stores suspension reason for debugging
146 
147  bool notifying; // may be true in any state except for the initial
148  bool updateScheduled; // time-based options update has been scheduled
149 
150 private:
151  ICAP::Method parseMethod(const char *) const;
152  ICAP::VectPoint parseVectPoint(const char *) const;
153 
154  void suspend(const char *reason);
155 
156  bool hasOptions() const;
157  bool needNewOptions() const;
158  time_t optionsFetchTime() const;
159 
160  void scheduleUpdate(time_t when);
161  void scheduleNotification();
162 
163  void startGettingOptions();
164  void handleNewOptions(Options *newOptions);
165  void changeOptions(Options *newOptions);
166  void checkOptions();
167 
168  void announceStatusChange(const char *downPhrase, bool important) const;
169 
171  void setMaxConnections();
173  int excessConnections() const;
178  int availableConnections() const;
183  void busyCheckpoint();
184 
185  const char *status() const;
186 
187  mutable bool wasAnnouncedUp; // prevent sequential same-state announcements
189 };
190 
191 class ModXact;
194 class ConnWaiterDialer: public NullaryMemFunT<ModXact>
195 {
196 public:
202 };
203 
204 } // namespace Icap
205 } // namespace Adaptation
206 
207 #endif /* SQUID_ICAPSERVICEREP_H */
208 
summarizes adaptation service answer for the noteAdaptationAnswer() API
Definition: Answer.h:22
virtual bool doneAll() const
whether positive goal has been reached
Definition: AsyncJob.cc:96
const char * status() const
internal cleanup; do not call directly
Definition: ServiceRep.cc:687
void putConnection(const Comm::ConnectionPointer &conn, bool isReusable, bool sendReset, const char *comment)
Definition: ServiceRep.cc:145
ICAP::VectPoint parseVectPoint(const char *) const
Definition: SBuf.h:87
void(ModXact::* Method)()
Definition: AsyncJobCalls.h:92
int theMaxConnections
the maximum allowed connections to the service
Definition: ServiceRep.h:139
std::unique_ptr< SSL_SESSION, HardFun< void, SSL_SESSION *,&SSL_SESSION_free > > SessionStatePointer
Definition: Session.h:43
void changeOptions(Options *newOptions)
Definition: ServiceRep.cc:463
bool availableForNew() const
a new transaction may start communicating with the service
Definition: ServiceRep.cc:305
void handleNewOptions(Options *newOptions)
Definition: ServiceRep.cc:584
int availableConnections() const
Definition: ServiceRep.cc:198
ServiceRep::Pointer theService
Definition: ServiceRep.h:198
bool wantsPreview(const SBuf &urlPath, size_t &wantedSize) const
Definition: ServiceRep.cc:329
int conn
the current server connection FD
Definition: Transport.cc:26
int excessConnections() const
The number of connections which excess the Max-Connections limit.
Definition: ServiceRep.cc:222
void callWhenAvailable(AsyncCall::Pointer &cb, bool priority=false)
Definition: ServiceRep.cc:411
IdleConnList * theIdleConns
idle persistent connection pool
Definition: ServiceRep.h:142
bool existWaiters() const
if there are xactions waiting for the service to be available
Definition: ServiceRep.h:97
ConnWaiterDialer(const CbcPointer< Adaptation::Icap::ModXact > &xact, Adaptation::Icap::ConnWaiterDialer::Parent::Method aHandler)
Definition: ServiceRep.cc:739
void suspend(const char *reason)
Definition: ServiceRep.cc:278
void noteConnectionUse(const Comm::ConnectionPointer &conn)
Definition: ServiceRep.cc:171
virtual Initiate * makeXactLauncher(Http::Message *virginHeader, HttpRequest *virginCause, AccessLogEntry::Pointer &alp)
Definition: ServiceRep.cc:680
RefCount< ServiceRep > Pointer
Definition: ServiceRep.h:65
void callWhenReady(AsyncCall::Pointer &cb)
Definition: ServiceRep.cc:429
bool availableForOld() const
a transaction notified about connection slot availability may start communicating with the service ...
Definition: ServiceRep.cc:315
virtual bool probed() const
Definition: ServiceRep.cc:290
void noteConnectionFailed(const char *comment)
Definition: ServiceRep.cc:177
virtual void noteAdaptationAnswer(const Answer &answer)
Definition: ServiceRep.cc:546
NullaryMemFunT< ModXact > Parent
Definition: ServiceRep.h:197
common parts of HttpRequest and HttpReply
Definition: Message.h:24
Counts events, forgetting old ones. Usefull for "3 errors/minute" limits.
Definition: FadingCounter.h:15
std::shared_ptr< SSL_CTX > ContextPointer
Definition: Context.h:28
Comm::ConnectionPointer getConnection(bool isRetriable, bool &isReused)
Definition: ServiceRep.cc:115
virtual bool doneAll() const
whether positive goal has been reached
Definition: ServiceRep.h:100
virtual bool up() const
Definition: ServiceRep.cc:300
void noteNewWaiter()
New xaction waiting for service to be up or available.
Definition: ServiceRep.h:95
Security::SessionStatePointer sslSession
Definition: ServiceRep.h:114
CbcPointer< Adaptation::Initiate > theOptionsFetcher
Definition: ServiceRep.h:129
bool connOverloadReported
whether we reported exceeding theMaxConnections
Definition: ServiceRep.h:141
std::vector< Client > Clients
Definition: ServiceRep.h:124
virtual void callException(const std::exception &e)
called when the job throws during an async call
Definition: ServiceRep.cc:576
Security::ContextPointer sslContext
Definition: ServiceRep.h:113
bool wantsUrl(const SBuf &urlPath) const
Definition: ServiceRep.cc:323
time_t optionsFetchTime() const
Definition: ServiceRep.cc:656
ServiceRep(const ServiceConfigPointer &aConfig)
Definition: ServiceRep.cc:34
ICAP::Method parseMethod(const char *) const
std::map< OptionName, const Option *, OptionNameCmp > Options
name:option map
Definition: Options.h:159
virtual bool detached() const
whether detached() was called
Definition: ServiceRep.cc:734
FadingCounter theSessionFailures
Definition: ServiceRep.h:144
void scheduleUpdate(time_t when)
Definition: ServiceRep.cc:618
std::deque< Client > theNotificationWaiters
Definition: ServiceRep.h:134
void noteGoneWaiter()
An xaction is not waiting any more for service to be available.
Definition: ServiceRep.cc:237
void setMaxConnections()
Set the maximum allowed connections for the service.
Definition: ServiceRep.cc:183
void announceStatusChange(const char *downPhrase, bool important) const
Definition: ServiceRep.cc:531

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors