Forwarder.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2018 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 /* DEBUG: section 54 Interprocess Communication */
10 
11 #include "squid.h"
12 #include "base/AsyncJobCalls.h"
13 #include "base/TextException.h"
14 #include "errorpage.h"
15 #include "HttpReply.h"
16 #include "HttpRequest.h"
17 #include "ipc/Forwarder.h"
18 #include "ipc/Port.h"
19 #include "ipc/TypedMsgHdr.h"
20 
21 CBDATA_NAMESPACED_CLASS_INIT(Ipc, Forwarder);
22 
24 unsigned int Ipc::Forwarder::LastRequestId = 0;
25 
26 Ipc::Forwarder::Forwarder(Request::Pointer aRequest, double aTimeout):
27  AsyncJob("Ipc::Forwarder"),
28  request(aRequest), timeout(aTimeout)
29 {
30 }
31 
33 {
35  Must(request->requestId == 0);
36  });
37 }
38 
39 void
41 {
42  debugs(54, 3, HERE);
43 
44  typedef NullaryMemFunT<Forwarder> Dialer;
46  if (++LastRequestId == 0) // don't use zero value as request->requestId
47  ++LastRequestId;
48  request->requestId = LastRequestId;
49  TheRequestsMap[request->requestId] = callback;
50  TypedMsgHdr message;
51 
52  try {
53  request->pack(message);
54  } catch (...) {
55  // assume the pack() call failed because the message did not fit
56  // TODO: add a more specific exception?
57  handleError();
58  return;
59  }
60 
62  eventAdd("Ipc::Forwarder::requestTimedOut", &Forwarder::RequestTimedOut,
63  this, timeout, 0, false);
64 }
65 
66 void
68 {
69  debugs(54, 5, HERE);
70  removeTimeoutEvent();
71  if (request->requestId > 0) {
72  DequeueRequest(request->requestId);
73  request->requestId = 0;
74  }
75 }
76 
77 bool
79 {
80  debugs(54, 5, HERE);
81  return request->requestId == 0;
82 }
83 
85 void
87 {
88  debugs(54, 3, HERE);
89  request->requestId = 0;
90  // Do not do entry->complete() because it will trigger our client side
91  // processing when we no longer own the client-Squid connection.
92  // Let job cleanup close the client-Squid connection that Coordinator
93  // now owns.
94 }
95 
97 void
99 {
100  debugs(54, 3, HERE);
101  Must(param != NULL);
102  Forwarder* fwdr = static_cast<Forwarder*>(param);
103  // use async call to enable job call protection that time events lack
104  CallJobHere(54, 5, fwdr, Forwarder, requestTimedOut);
105 }
106 
108 void
110 {
111  debugs(54, 3, HERE);
112  handleTimeout();
113 }
114 
115 void
117 {
118  mustStop("error");
119 }
120 
121 void
123 {
124  mustStop("timeout");
125 }
126 
128 void
129 Ipc::Forwarder::handleException(const std::exception& e)
130 {
131  debugs(54, 3, HERE << e.what());
132  mustStop("exception");
133 }
134 
135 void
136 Ipc::Forwarder::callException(const std::exception& e)
137 {
138  try {
139  handleException(e);
140  } catch (const std::exception& ex) {
141  debugs(54, DBG_CRITICAL, HERE << ex.what());
142  }
144 }
145 
148 Ipc::Forwarder::DequeueRequest(unsigned int requestId)
149 {
150  debugs(54, 3, HERE);
151  Must(requestId != 0);
152  AsyncCall::Pointer call;
153  RequestsMap::iterator request = TheRequestsMap.find(requestId);
154  if (request != TheRequestsMap.end()) {
155  call = request->second;
156  Must(call != NULL);
157  TheRequestsMap.erase(request);
158  }
159  return call;
160 }
161 
163 void
165 {
168 }
169 
170 void
171 Ipc::Forwarder::HandleRemoteAck(unsigned int requestId)
172 {
173  debugs(54, 3, HERE);
174  Must(requestId != 0);
175 
176  AsyncCall::Pointer call = DequeueRequest(requestId);
177  if (call != NULL)
178  ScheduleCallHere(call);
179 }
180 
#define JobCallback(dbgSection, dbgLevel, Dialer, job, method)
Convenience macro to create a Dialer-based job callback.
Definition: AsyncJobCalls.h:68
static unsigned int LastRequestId
last requestId used
Definition: Forwarder.h:69
virtual void handleException(const std::exception &e)
terminate with an error
Definition: Forwarder.cc:129
#define CBDATA_NAMESPACED_CLASS_INIT(namespace, type)
Definition: cbdata.h:326
void const char HLPCB * callback
Definition: stub_helper.cc:16
#define CallJobHere(debugSection, debugLevel, job, Class, method)
Definition: AsyncJobCalls.h:57
struct _request * request(char *urlin)
Definition: tcp-banger2.c:291
virtual void callException(const std::exception &e)
called when the job throws during an async call
Definition: AsyncJob.cc:127
void handleRemoteAck()
called when Coordinator starts processing the request
Definition: Forwarder.cc:86
#define Must(condition)
Like assert() but throws an exception instead of aborting the process.
Definition: TextException.h:69
Forwarder(Request::Pointer aRequest, double aTimeout)
Definition: Forwarder.cc:26
std::map< unsigned int, AsyncCall::Pointer > RequestsMap
maps request-&gt;id to Forwarder::handleRemoteAck callback
Definition: Forwarder.h:66
#define DBG_CRITICAL
Definition: Debug.h:45
#define SWALLOW_EXCEPTIONS(code)
Definition: TextException.h:73
virtual void handleTimeout()
Definition: Forwarder.cc:122
virtual void handleError()
Definition: Forwarder.cc:116
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:124
int eventFind(EVH *func, void *arg)
Definition: event.cc:155
virtual void swanSong()
Definition: Forwarder.cc:67
static void HandleRemoteAck(unsigned int requestId)
finds and calls the right Forwarder upon Coordinator&#39;s response
Definition: Forwarder.cc:171
virtual void start()
called by AsyncStart; do not call directly
Definition: Forwarder.cc:40
void SendMessage(const String &toAddress, const TypedMsgHdr &message)
Definition: UdsOp.cc:188
void removeTimeoutEvent()
called when we are no longer waiting for Coordinator to respond
Definition: Forwarder.cc:164
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:153
void eventDelete(EVH *func, void *arg)
Definition: event.cc:131
void eventAdd(const char *name, EVH *func, void *arg, double when, int weight, bool cbdata)
Definition: event.cc:109
#define ScheduleCallHere(call)
Definition: AsyncCall.h:166
struct msghdr with a known type, fixed-size I/O and control buffers
Definition: TypedMsgHdr.h:31
static RequestsMap TheRequestsMap
pending Coordinator requests
Definition: Forwarder.h:67
static AsyncCall::Pointer DequeueRequest(unsigned int requestId)
returns and forgets the right Forwarder callback for the request
Definition: Forwarder.cc:148
virtual bool doneAll() const
whether positive goal has been reached
Definition: Forwarder.cc:78
void requestTimedOut()
called when Coordinator fails to start processing the request [in time]
Definition: Forwarder.cc:109
virtual void callException(const std::exception &e)
called when the job throws during an async call
Definition: Forwarder.cc:136
static void RequestTimedOut(void *param)
Ipc::Forwarder::requestTimedOut wrapper.
Definition: Forwarder.cc:98
virtual ~Forwarder()
Definition: Forwarder.cc:32
static String CoordinatorAddr()
get the IPC message address for coordinator process
Definition: Port.cc:64
#define NULL
Definition: types.h:166

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors