Skip to content
This repository
Browse code

Don't hardcode path for unix socket. Make it a temporary random file …

…and pass it around on environment
  • Loading branch information...
commit 94b756a46ed1dcc6a7646b186fc14fdd7d2131e8 1 parent a99b93d
Pedro Teixeira authored
3  README.markdown
Source Rendered
@@ -78,4 +78,5 @@ No downtime!
78 78 1. review the master / worker protocol for passing in the server socket
79 79 1. review the code that synchronously tries to fetch the file descriptor from the original master into the new respawned master (lib/fugue.js lines 72 - 91) - they're a bit too hacky - try and remove the recvMsg cycle...
80 80 1. Monitor request timeouts (as Unicorn does) - only plausible for HTTP Servers, though (??)
81   -1. Don't hardcode path for unix socket. Make it a temporary random file and pass it around on environment?
  81 +1. (DONE) Don't hardcode path for unix socket. Make it a temporary random file and pass it around on environment?
  82 +1. Somehow delete master sockets that are not being used. This can be tricky, since master socket paths are passed around to workers and new masters.
1  example_app/test_app.js → examples/test_app.js
@@ -22,7 +22,6 @@ fugue.start(server, 4000, null, 2, {
22 22 // uid: 'pedroteixeira',
23 23 // gid: 'staff',
24 24 working_path: '/tmp',
25   - tmp_path: process.cwd() + '/tmp',
26 25 verbose: true
27 26 });
28 27
28 lib/fugue.js
@@ -13,7 +13,7 @@ exports.start = function(server, port, host, worker_count, options) {
13 13 // merge options
14 14 var default_options = {
15 15 working_path : process.cwd(),
16   - tmp_path : 'tmp',
  16 + tmp_path : '/tmp',
17 17 uid : process.getuid(),
18 18 gid : process.getgid(),
19 19 daemonize: false,
@@ -59,9 +59,23 @@ exports.start = function(server, port, host, worker_count, options) {
59 59 spawn = require('child_process').spawn,
60 60 sys = require('sys'),
61 61 fs = require('fs'),
  62 + path = require('path'),
62 63 netBinding = process.binding('net');
63   -
64   - var socket_server_socket_path = options.tmp_path + '/fugue_master.sock'
  64 +
  65 + // calculate master_socket_path.
  66 + // It can come from env if we have been spawned or we have to create a new one
  67 + var master_socket_path = null;
  68 + if (process.env._FUGUE_MASTER_SOCKET_PATH) {
  69 + if (path.existsSync(process.env._FUGUE_MASTER_SOCKET_PATH)) {
  70 + master_socket_path = process.env._FUGUE_MASTER_SOCKET_PATH;
  71 + }
  72 + }
  73 + if (!master_socket_path) {
  74 + // make new
  75 + master_socket_path = process.env._FUGUE_MASTER_SOCKET_PATH = path.join(options.tmp_path, 'fugue_'+process.pid+'_master.sock');
  76 + }
  77 +
  78 + log('Using master socket path: '+master_socket_path);
65 79
66 80 // Redirect stdout and stderr
67 81 if (options.log_file) {
@@ -83,8 +97,8 @@ exports.start = function(server, port, host, worker_count, options) {
83 97 // request socket to original server
84 98 // this startup part has to work synchronously, so we dig deep into the bindings...
85 99 var client_socket = netBinding.socket('unix');
86   - log('connecting to '+socket_server_socket_path);
87   - netBinding.connect(client_socket, socket_server_socket_path);
  100 + log('connecting to '+master_socket_path);
  101 + netBinding.connect(client_socket, master_socket_path);
88 102 var request_buffer = new Buffer('GIMME_SOCKET', 'ascii');
89 103 netBinding.write(client_socket, request_buffer, 0, request_buffer.length);
90 104 var responseBuffer = new Buffer(256);
@@ -144,7 +158,7 @@ exports.start = function(server, port, host, worker_count, options) {
144 158 conn.write('HERE_YOU_GO', 'ascii', server_socket);
145 159 }
146 160 });
147   - }).listen(socket_server_socket_path);
  161 + }).listen(master_socket_path);
148 162
149 163 var workers = [];
150 164
@@ -256,7 +270,7 @@ exports.start = function(server, port, host, worker_count, options) {
256 270 log("Worker "+worker_id+" exiting.");
257 271 });
258 272
259   - var connection = net.createConnection(socket_server_socket_path).on('connect', function() {
  273 + var connection = net.createConnection(master_socket_path).on('connect', function() {
260 274 connection.write('GIMME_SOCKET');
261 275 connection.on('fd', function(fd) {
262 276 server.listenFD(fd);

0 comments on commit 94b756a

Please sign in to comment.
Something went wrong with that request. Please try again.