public
Description: Phusion Passenger (mod_rails)
Homepage: http://www.modrails.com/
Clone URL: git://github.com/FooBarWidget/passenger.git
Search Repo:
Click here to lend your support to: passenger and make a donation at www.pledgie.com !
Begin working on seperating ApplicationPool server into a seperate 
executable.
Hongli Lai (Phusion) (author)
Tue Apr 15 16:26:02 -0700 2008
commit  9b9f71bb7d36974efe383780665f92656cbbbe9c
tree    dc83d466ee02ee1428341e8356bf206f4898c54d
parent  81a485007231a067c1d4e0e4088520a9a3863710
...
8
9
10
 
11
12
13
...
8
9
10
11
12
13
14
0
@@ -8,6 +8,7 @@
0
 ext/apache2/*.lo
0
 ext/apache2/*.slo
0
 ext/apache2/.libs
0
+ext/apache2/ApplicationPoolServerExecutable
0
 ext/passenger/Makefile
0
 ext/boost/src/*.a
0
 doc/rdoc
...
118
119
120
121
 
122
123
 
 
 
 
124
125
126
...
134
135
136
 
 
 
 
 
 
 
 
 
 
 
 
 
137
138
139
...
212
213
214
215
 
 
 
 
 
216
217
218
...
118
119
120
 
121
122
 
123
124
125
126
127
128
129
...
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
...
228
229
230
 
231
232
233
234
235
236
237
238
0
@@ -118,9 +118,12 @@
0
   apxs_objects = APACHE2::OBJECTS.keys.join(',')
0
 
0
   desc "Build mod_passenger Apache 2 module"
0
- task :apache2 => ['mod_passenger.so', ', :native_support]
0
+ task :apache2 => ['mod_passenger.so', 'ApplicationPoolServerExecutable', :native_support]
0
   
0
- file 'mod_passenger.so' => ['../boost/src/libboost_thread.a', 'mod_passenger.o'] + APACHE2::OBJECTS.keys do
0
+ file 'mod_passenger.so' => [
0
+ '../boost/src/libboost_thread.a',
0
+ 'mod_passenger.o'
0
+ ] + APACHE2::OBJECTS.keys do
0
     # apxs totally sucks. We couldn't get it working correctly
0
     # on MacOS X (it had various problems with building universal
0
     # binaries), so we decided to ditch it and build/install the
0
@@ -134,6 +137,19 @@
0
       linkflags
0
   end
0
   
0
+ file 'ApplicationPoolServerExecutable' => [
0
+ '../boost/src/libboost_thread.a',
0
+ 'ApplicationPoolServerExecutable.cpp',
0
+ 'ApplicationPool.h',
0
+ 'StandardApplicationPool.h',
0
+ 'Utils.o',
0
+ 'Logging.o'
0
+ ] do
0
+ create_executable "ApplicationPoolServerExecutable",
0
+ 'ApplicationPoolServerExecutable.cpp Utils.o Logging.o',
0
+ "#{LDFLAGS} ../boost/src/libboost_thread.a -lpthread"
0
+ end
0
+
0
   desc "Install mod_passenger Apache 2 module"
0
   task 'apache2:install' => :apache2 do
0
     install_dir = `#{APXS2} -q LIBEXECDIR`.strip
0
@@ -212,7 +228,11 @@
0
   task :test => [:'test:apache2', :'test:ruby', :'test:integration']
0
   
0
   desc "Run unit tests for the Apache 2 module"
0
- task 'test:apache2' => ['Apache2ModuleTests', :native_support] do
0
+ task 'test:apache2' => [
0
+ 'Apache2ModuleTests',
0
+ '../ext/apache2/ApplicationPoolServerExecutable',
0
+ :native_support
0
+ ] do
0
     sh "./Apache2ModuleTests"
0
   end
0
   
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
0
@@ -1 +1,346 @@
0
+/*
0
+ * Phusion Passenger - http://www.modrails.com/
0
+ * Copyright (C) 2008 Phusion
0
+ *
0
+ * This program is free software; you can redistribute it and/or modify
0
+ * it under the terms of the GNU General Public License as published by
0
+ * the Free Software Foundation; version 2 of the License.
0
+ *
0
+ * This program is distributed in the hope that it will be useful,
0
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
0
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0
+ * GNU General Public License for more details.
0
+ *
0
+ * You should have received a copy of the GNU General Public License along
0
+ * with this program; if not, write to the Free Software Foundation, Inc.,
0
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
0
+ */
0
+#ifndef _PASSENGER_APPLICATION_POOL_SERVER_EXECUTABLE_H_
0
+#define _PASSENGER_APPLICATION_POOL_SERVER_EXECUTABLE_H_
0
+
0
+#include <boost/bind.hpp>
0
+#include <boost/thread/thread.hpp>
0
+
0
+#include <string>
0
+#include <vector>
0
+#include <set>
0
+#include <map>
0
+
0
+#include "MessageChannel.h"
0
+#include "StandardApplicationPool.h"
0
+#include "Application.h"
0
+#include "Logging.h"
0
+#include "Exceptions.h"
0
+
0
+
0
+using namespace boost;
0
+using namespace std;
0
+using namespace Passenger;
0
+
0
+class Server;
0
+class Client;
0
+typedef shared_ptr<Client> ClientPtr;
0
+
0
+#define SERVER_SOCKET_FD 3
0
+
0
+
0
+/*****************************************
0
+ * Server
0
+ *****************************************/
0
+
0
+class Server {
0
+private:
0
+ friend class Client;
0
+
0
+ int serverSocket;
0
+ StandardApplicationPool pool;
0
+ set<ClientPtr> clients;
0
+ mutex lock;
0
+
0
+public:
0
+ Server(int serverSocket,
0
+ const string &spawnServerCommand,
0
+ const string &logFile,
0
+ const string &environment,
0
+ const string &rubyCommand,
0
+ const string &user)
0
+ : pool(spawnServerCommand, logFile, environment, rubyCommand, user) {
0
+ this->serverSocket = serverSocket;
0
+ }
0
+
0
+ ~Server() {
0
+ int ret;
0
+ do {
0
+ ret = close(serverSocket);
0
+ } while (ret == -1 && errno == EINTR);
0
+
0
+ // Wait for all clients to disconnect.
0
+ mutex::scoped_lock l(lock);
0
+ clients.clear();
0
+ }
0
+
0
+ int start(); // Will be defined later, because Client depends on Server's interface.
0
+};
0
+
0
+
0
+/*****************************************
0
+ * Client
0
+ *****************************************/
0
+
0
+/**
0
+ * Represents a single ApplicationPool client, connected to this server.
0
+ *
0
+ * @invariant
0
+ * The life time of a Client object is guaranteed to be less than
0
+ * that of its associated Server object.
0
+ */
0
+class Client {
0
+private:
0
+ /** The Server that this Client object belongs to. */
0
+ Server &server;
0
+
0
+ /** The connection to the client. */
0
+ int fd;
0
+ MessageChannel channel;
0
+
0
+ /** The thread which handles the client connection. */
0
+ thread *thr;
0
+
0
+ /**
0
+ * Maps session ID to sessions created by ApplicationPool::get(). Session IDs
0
+ * are sent back to the ApplicationPool client. This allows the ApplicationPool
0
+ * client to tell us which of the multiple sessions it wants to close, later on.
0
+ */
0
+ map<int, Application::SessionPtr> sessions;
0
+
0
+ /** Last used session ID. */
0
+ int lastSessionID;
0
+
0
+ void processGet(const vector<string> &args) {
0
+ Application::SessionPtr session;
0
+ bool failed = false;
0
+
0
+ try {
0
+ session = server.pool.get(args[1], args[2] == "true", args[3]);
0
+ sessions[lastSessionID] = session;
0
+ lastSessionID++;
0
+ } catch (const SpawnException &e) {
0
+ if (e.hasErrorPage()) {
0
+ P_TRACE(3, "Client " << this << ": SpawnException "
0
+ "occured (with error page)");
0
+ channel.write("SpawnException", e.what(), "true", NULL);
0
+ channel.writeScalar(e.getErrorPage());
0
+ } else {
0
+ P_TRACE(3, "Client " << this << ": SpawnException "
0
+ "occured (no error page)");
0
+ channel.write("SpawnException", e.what(), "false", NULL);
0
+ }
0
+ failed = true;
0
+ } catch (const IOException &e) {
0
+ channel.write("IOException", e.what(), NULL);
0
+ failed = true;
0
+ }
0
+ if (!failed) {
0
+ try {
0
+ channel.write("ok", toString(session->getPid()).c_str(),
0
+ toString(lastSessionID - 1).c_str(), NULL);
0
+ channel.writeFileDescriptor(session->getReader());
0
+ channel.writeFileDescriptor(session->getWriter());
0
+ session->closeReader();
0
+ session->closeWriter();
0
+ } catch (const exception &) {
0
+ P_TRACE(3, "Client " << this << ": something went wrong "
0
+ "while sending 'ok' back to the client.");
0
+ sessions.erase(lastSessionID - 1);
0
+ throw;
0
+ }
0
+ }
0
+ }
0
+
0
+ void processClose(const vector<string> &args) {
0
+ sessions.erase(atoi(args[1]));
0
+ }
0
+
0
+ void processClear(const vector<string> &args) {
0
+ server.pool.clear();
0
+ }
0
+
0
+ void processSetMaxIdleTime(const vector<string> &args) {
0
+ server.pool.setMaxIdleTime(atoi(args[1]));
0
+ }
0
+
0
+ void processSetMax(const vector<string> &args) {
0
+ server.pool.setMax(atoi(args[1]));
0
+ }
0
+
0
+ void processGetActive(const vector<string> &args) {
0
+ channel.write(toString(server.pool.getActive()).c_str(), NULL);
0
+ }
0
+
0
+ void processGetCount(const vector<string> &args) {
0
+ channel.write(toString(server.pool.getCount()).c_str(), NULL);
0
+ }
0
+
0
+ void processGetSpawnServerPid(const vector<string> &args) {
0
+ channel.write(toString(server.pool.getSpawnServerPid()).c_str(), NULL);
0
+ }
0
+
0
+ void processUnknownMessage(const vector<string> &args) {
0
+ string name;
0
+ if (args.empty()) {
0
+ name = "(null)";
0
+ } else {
0
+ name = args[0];
0
+ }
0
+ P_WARN("An ApplicationPool client sent an invalid command: "
0
+ << name << " (" << args.size() << " elements)");
0
+ }
0
+
0
+ /**
0
+ * The entry point of the thread that handles the client connection.
0
+ */
0
+ void threadMain(const set<ClientPtr>::iterator &it) {
0
+ try {
0
+ vector<string> args;
0
+ while (true) {
0
+ if (!channel.read(args)) {
0
+ // Client closed connection.
0
+ break;
0
+ }
0
+
0
+ P_TRACE(3, "Client " << this << ": received message: " <<
0
+ toString(args));
0
+ if (args[0] == "get" && args.size() == 4) {
0
+ processGet(args);
0
+ } else if (args[0] == "close" && args.size() == 2) {
0
+ processClose(args);
0
+ } else if (args[0] == "clear" && args.size() == 1) {
0
+ processClear(args);
0
+ } else if (args[0] == "setMaxIdleTime" && args.size() == 2) {
0
+ processSetMaxIdleTime(args);
0
+ } else if (args[0] == "setMax" && args.size() == 2) {
0
+ processSetMax(args);
0
+ } else if (args[0] == "getActive" && args.size() == 1) {
0
+ processGetActive(args);
0
+ } else if (args[0] == "getCount" && args.size() == 1) {
0
+ processGetCount(args);
0
+ } else if (args[0] == "getSpawnServerPid" && args.size() == 1) {
0
+ processGetSpawnServerPid(args);
0
+ } else {
0
+ processUnknownMessage(args);
0
+ break;
0
+ }
0
+ }
0
+ } catch (const exception &e) {
0
+ P_WARN("Uncaught exception in ApplicationPoolServer client thread: " <<
0
+ e.what());
0
+ }
0
+
0
+ mutex::scoped_lock l(server.lock);
0
+ server.clients.erase(it);
0
+ }
0
+
0
+public:
0
+ /**
0
+ * Create a new Client object.
0
+ *
0
+ * @param connection The connection to the ApplicationPool client.
0
+ *
0
+ * @note
0
+ * <tt>connection</tt> will be closed upon destruction
0
+ */
0
+ Client(Server &the_server, int connection)
0
+ : server(the_server),
0
+ fd(connection),
0
+ channel(connection) {
0
+ thr = NULL;
0
+ lastSessionID = 0;
0
+ }
0
+
0
+ /**
0
+ * Start the thread for handling the connection with this client.
0
+ *
0
+ * @param The iterator of this Client object inside the server's
0
+ * <tt>clients</tt> set. This is used to remove itself from
0
+ * the <tt>clients</tt> set once the client has closed the
0
+ * connection.
0
+ */
0
+ void start(const set<ClientPtr>::iterator &it) {
0
+ thr = new thread(bind(&Client::threadMain, this, it));
0
+ }
0
+
0
+ ~Client() {
0
+ if (thr != NULL) {
0
+ thr->join();
0
+ delete thr;
0
+ }
0
+ close(fd);
0
+ }
0
+};
0
+
0
+
0
+int
0
+Server::start() {
0
+ while (true) {
0
+ int fds[2], ret;
0
+ char x;
0
+
0
+ // The received data only serves to wake up the server socket,
0
+ // and is not important.
0
+ do {
0
+ ret = read(serverSocket, &x, 1);
0
+ } while (ret == -1 && errno == EINTR);
0
+ if (ret == 0) {
0
+ // All web server processes disconnected from this server.
0
+ // So we can safely quit.
0
+ break;
0
+ }
0
+
0
+ // We have an incoming connect request from an
0
+ // ApplicationPool client.
0
+ do {
0
+ ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
0
+ } while (ret == -1 && errno == EINTR);
0
+ if (ret == -1) {
0
+ int e = errno;
0
+ P_ERROR("Cannot create an anonymous Unix socket: " <<
0
+ strerror(e) << " (" << e << ") --- aborting!");
0
+ abort();
0
+
0
+ // Shut up compiler warning.
0
+ printf("%d", e);
0
+ }
0
+
0
+ try {
0
+ MessageChannel(serverSocket).writeFileDescriptor(fds[1]);
0
+ do {
0
+ ret = close(fds[1]);
0
+ } while (ret == -1 && errno == EINTR);
0
+ } catch (SystemException &e) {
0
+ P_ERROR("Cannot send a file descriptor: " << e.sys() <<
0
+ " --- aborting!");
0
+ abort();
0
+ } catch (const exception &e) {
0
+ P_ERROR("Cannot send a file descriptor: " << e.what() <<
0
+ " --- aborting!");
0
+ abort();
0
+ }
0
+
0
+ ClientPtr client(new Client(*this, fds[0]));
0
+ pair<set<ClientPtr>::iterator, bool> p;
0
+ {
0
+ mutex::scoped_lock l(lock);
0
+ p = clients.insert(client);
0
+ }
0
+ client->start(p.first);
0
+ }
0
+}
0
+
0
+int
0
+main(int argc, char *argv[]) {
0
+ Server server(SERVER_SOCKET_FD, argv[1], argv[2], argv[2], argv[3], argv[5]);
0
+ return server.start();
0
+}
0
+
0
+#endif /* _PASSENGER_APPLICATION_POOL_SERVER_EXECUTABLE_H_ */

Comments

    No one has commented yet.