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 clear ENTRY_FWD_HDR_WAIT or do entry->complete() because
91  // it will trigger our client side processing. Let job cleanup close.
92 }
93 
95 void
97 {
98  debugs(54, 3, HERE);
99  Must(param != NULL);
100  Forwarder* fwdr = static_cast<Forwarder*>(param);
101  // use async call to enable job call protection that time events lack
102  CallJobHere(54, 5, fwdr, Forwarder, requestTimedOut);
103 }
104 
106 void
108 {
109  debugs(54, 3, HERE);
110  handleTimeout();
111 }
112 
113 void
115 {
116  mustStop("error");
117 }
118 
119 void
121 {
122  mustStop("timeout");
123 }
124 
126 void
127 Ipc::Forwarder::handleException(const std::exception& e)
128 {
129  debugs(54, 3, HERE << e.what());
130  mustStop("exception");
131 }
132 
133 void
134 Ipc::Forwarder::callException(const std::exception& e)
135 {
136  try {
137  handleException(e);
138  } catch (const std::exception& ex) {
139  debugs(54, DBG_CRITICAL, HERE << ex.what());
140  }
142 }
143 
146 Ipc::Forwarder::DequeueRequest(unsigned int requestId)
147 {
148  debugs(54, 3, HERE);
149  Must(requestId != 0);
150  AsyncCall::Pointer call;
151  RequestsMap::iterator request = TheRequestsMap.find(requestId);
152  if (request != TheRequestsMap.end()) {
153  call = request->second;
154  Must(call != NULL);
155  TheRequestsMap.erase(request);
156  }
157  return call;
158 }
159 
161 void
163 {
166 }
167 
168 void
169 Ipc::Forwarder::HandleRemoteAck(unsigned int requestId)
170 {
171  debugs(54, 3, HERE);
172  Must(requestId != 0);
173 
174  AsyncCall::Pointer call = DequeueRequest(requestId);
175  if (call != NULL)
176  ScheduleCallHere(call);
177 }
178 
#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:127
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->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:120
virtual void handleError()
Definition: Forwarder.cc:114
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:121
int eventFind(EVH *func, void *arg)
Definition: event.cc:155
CBDATA_NAMESPACED_CLASS_INIT(Ipc, Forwarder)
virtual void swanSong()
Definition: Forwarder.cc:67
static void HandleRemoteAck(unsigned int requestId)
finds and calls the right Forwarder upon Coordinator's response
Definition: Forwarder.cc:169
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:162
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:145
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:146
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:107
virtual void callException(const std::exception &e)
called when the job throws during an async call
Definition: Forwarder.cc:134
static void RequestTimedOut(void *param)
Ipc::Forwarder::requestTimedOut wrapper.
Definition: Forwarder.cc:96
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