Forwarder.cc
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 /* 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  debugs(54, 5, HERE);
31 }
32 
34 {
35  debugs(54, 5, HERE);
36  Must(request->requestId == 0);
37  cleanup();
38 }
39 
41 void
43 {
44 }
45 
46 void
48 {
49  debugs(54, 3, HERE);
50 
51  typedef NullaryMemFunT<Forwarder> Dialer;
53  if (++LastRequestId == 0) // don't use zero value as request->requestId
54  ++LastRequestId;
55  request->requestId = LastRequestId;
56  TheRequestsMap[request->requestId] = callback;
57  TypedMsgHdr message;
58 
59  try {
60  request->pack(message);
61  } catch (...) {
62  // assume the pack() call failed because the message did not fit
63  // TODO: add a more specific exception?
64  handleError();
65  return;
66  }
67 
69  eventAdd("Ipc::Forwarder::requestTimedOut", &Forwarder::RequestTimedOut,
70  this, timeout, 0, false);
71 }
72 
73 void
75 {
76  debugs(54, 5, HERE);
77  removeTimeoutEvent();
78  if (request->requestId > 0) {
79  DequeueRequest(request->requestId);
80  request->requestId = 0;
81  }
82  cleanup();
83 }
84 
85 bool
87 {
88  debugs(54, 5, HERE);
89  return request->requestId == 0;
90 }
91 
93 void
95 {
96  debugs(54, 3, HERE);
97  request->requestId = 0;
98  // Do not clear ENTRY_FWD_HDR_WAIT or do entry->complete() because
99  // it will trigger our client side processing. Let job cleanup close.
100 }
101 
103 void
105 {
106  debugs(54, 3, HERE);
107  Must(param != NULL);
108  Forwarder* fwdr = static_cast<Forwarder*>(param);
109  // use async call to enable job call protection that time events lack
110  CallJobHere(54, 5, fwdr, Forwarder, requestTimedOut);
111 }
112 
114 void
116 {
117  debugs(54, 3, HERE);
118  handleTimeout();
119 }
120 
121 void
123 {
124  mustStop("error");
125 }
126 
127 void
129 {
130  mustStop("timeout");
131 }
132 
134 void
135 Ipc::Forwarder::handleException(const std::exception& e)
136 {
137  debugs(54, 3, HERE << e.what());
138  mustStop("exception");
139 }
140 
141 void
142 Ipc::Forwarder::callException(const std::exception& e)
143 {
144  try {
145  handleException(e);
146  } catch (const std::exception& ex) {
147  debugs(54, DBG_CRITICAL, HERE << ex.what());
148  }
150 }
151 
154 Ipc::Forwarder::DequeueRequest(unsigned int requestId)
155 {
156  debugs(54, 3, HERE);
157  Must(requestId != 0);
158  AsyncCall::Pointer call;
159  RequestsMap::iterator request = TheRequestsMap.find(requestId);
160  if (request != TheRequestsMap.end()) {
161  call = request->second;
162  Must(call != NULL);
163  TheRequestsMap.erase(request);
164  }
165  return call;
166 }
167 
169 void
171 {
174 }
175 
176 void
177 Ipc::Forwarder::HandleRemoteAck(unsigned int requestId)
178 {
179  debugs(54, 3, HERE);
180  Must(requestId != 0);
181 
182  AsyncCall::Pointer call = DequeueRequest(requestId);
183  if (call != NULL)
184  ScheduleCallHere(call);
185 }
186 
#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:70
virtual void handleException(const std::exception &e)
terminate with an error
Definition: Forwarder.cc:135
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:94
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:67
#define DBG_CRITICAL
Definition: Debug.h:44
virtual void handleTimeout()
Definition: Forwarder.cc:128
virtual void handleError()
Definition: Forwarder.cc:122
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
int eventFind(EVH *func, void *arg)
Definition: event.cc:155
CBDATA_NAMESPACED_CLASS_INIT(Ipc, Forwarder)
virtual void swanSong()
Definition: Forwarder.cc:74
static void HandleRemoteAck(unsigned int requestId)
finds and calls the right Forwarder upon Coordinator's response
Definition: Forwarder.cc:177
virtual void start()
called by AsyncStart; do not call directly
Definition: Forwarder.cc:47
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:170
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:147
void eventDelete(EVH *func, void *arg)
Definition: event.cc:131
#define Must(cond)
Definition: TextException.h:89
void eventAdd(const char *name, EVH *func, void *arg, double when, int weight, bool cbdata)
Definition: event.cc:109
virtual void cleanup()
perform cleanup actions
Definition: Forwarder.cc:42
#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:68
static AsyncCall::Pointer DequeueRequest(unsigned int requestId)
returns and forgets the right Forwarder callback for the request
Definition: Forwarder.cc:154
virtual bool doneAll() const
whether positive goal has been reached
Definition: Forwarder.cc:86
void requestTimedOut()
called when Coordinator fails to start processing the request [in time]
Definition: Forwarder.cc:115
virtual void callException(const std::exception &e)
called when the job throws during an async call
Definition: Forwarder.cc:142
static void RequestTimedOut(void *param)
Ipc::Forwarder::requestTimedOut wrapper.
Definition: Forwarder.cc:104
virtual ~Forwarder()
Definition: Forwarder.cc:33
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