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

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors