Arlolra has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/169198

Change subject: fixup! Clear timeouts per request, not worker
......................................................................

fixup! Clear timeouts per request, not worker

 * Request are handled asynchronously.

 * Follow up to I63cb727c86c37b50914d6ee303eea8ad3d999f51.

Change-Id: I102014ac4e7191bdeead5c3d8e235231c3cd87f8
---
M api/ParsoidService.js
M api/routes.js
M api/server.js
M package.json
4 files changed, 33 insertions(+), 20 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/parsoid 
refs/changes/98/169198/1

diff --git a/api/ParsoidService.js b/api/ParsoidService.js
index b632f0a..aabad8b 100644
--- a/api/ParsoidService.js
+++ b/api/ParsoidService.js
@@ -9,7 +9,9 @@
 var express = require('express'),
        hbs = require('handlebars'),
        cluster = require('cluster'),
-       path = require('path');
+       path = require('path'),
+       uuid = require('node-uuid').v4,
+       Buffer = require('buffer').Buffer;
 
 // relative includes
 var mp = '../lib/',
@@ -57,6 +59,14 @@
        // limit upload file size
        app.use(express.limit('15mb'));
 
+       // request ids
+       var buf = new Buffer(16);
+       app.use(function(req, res, next) {
+               uuid(null, buf);
+               res.local('reqId', buf.toString('hex'));
+               next();
+       });
+
        // Catch errors
        app.on('error', function( err ) {
                if ( err.errno === "EADDRINUSE" ) {
diff --git a/api/routes.js b/api/routes.js
index 54c21a3..c7cd818 100644
--- a/api/routes.js
+++ b/api/routes.js
@@ -36,20 +36,22 @@
 // Helpers
 
 var CPU_TIMEOUT = 5 * 60 * 1000;  // 5 minutes
-var cpuTimeout = function( p ) {
+var cpuTimeout = function( p, res ) {
        return new Promise(function( resolve, reject ) {
                if ( cluster.isMaster ) {
                        return p.then( resolve, reject );
                }
                process.send({
                        type: "timeout",
-                       timeout: CPU_TIMEOUT
+                       timeout: CPU_TIMEOUT,
+                       reqId: res.local("reqId")
                });
                var self = this;
                function done(cb) {
                        process.send({
                                type: "timeout",
-                               done: true
+                               done: true,
+                               reqId: res.local("reqId")
                        });
                        var args = Array.prototype.slice.call(arguments, 1);
                        cb.apply(self, args);
@@ -220,7 +222,7 @@
                apiUtils.setHeader(res, env, 'X-Parsoid-Performance', 
env.getPerformanceHeader());
                apiUtils.endResponse(res, env, out.join(''));
        });
-       return cpuTimeout(p).catch(function( err ) {
+       return cpuTimeout(p, res).catch(function( err ) {
                env.log("fatal/request", err);
        });
 };
@@ -333,7 +335,7 @@
                p = p.then( redirectToOldid );
        }
 
-       return cpuTimeout(p).catch(function( err ) {
+       return cpuTimeout(p, res).catch(function( err ) {
                env.log("fatal/request", err);
        });
 };
@@ -487,7 +489,7 @@
        ).then(
                roundTripDiff.bind( null, env, req, res, false )
        );
-       cpuTimeout(p).catch(function(err) {
+       cpuTimeout(p, res).catch(function(err) {
                env.log("fatal/request", err);
        });
 };
@@ -510,7 +512,7 @@
                var html = doc.innerHTML.replace(/[\r\n]/g, '');
                return roundTripDiff( env, req, res, false, DU.parseHTML(html) 
);
        });
-       cpuTimeout(p).catch(function(err) {
+       cpuTimeout(p, res).catch(function(err) {
                env.log("fatal/request", err);
        });
 };
@@ -533,7 +535,7 @@
                doc.body.appendChild(comment);
                return roundTripDiff( env, req, res, true, doc );
        });
-       cpuTimeout(p).catch(function(err) {
+       cpuTimeout(p, res).catch(function(err) {
                env.log("fatal/request", err);
        });
 };
diff --git a/api/server.js b/api/server.js
index 733eebd..c0a106d 100755
--- a/api/server.js
+++ b/api/server.js
@@ -64,10 +64,7 @@
        }
 
        var timeoutHandler, timeouts = new Map();
-       var spawn = function( pid ) {
-               if ( pid ) {
-                       timeouts.delete( pid );
-               }
+       var spawn = function() {
                if ( Object.keys(cluster.workers).length < argv.n ) {
                        var worker = cluster.fork();
                        worker.on('message', timeoutHandler.bind(null, worker));
@@ -78,14 +75,17 @@
        timeoutHandler = function( worker, msg ) {
                if ( msg.type !== "timeout" ) { return; }
                if ( msg.done ) {
-                       clearTimeout( timeouts.get( worker.process.pid ) );
-                       timeouts.delete( worker.process.pid );
+                       clearTimeout( timeouts.get( msg.reqId ) );
+                       timeouts.delete( msg.reqId );
                } else if ( msg.timeout ) {
                        var pid = worker.process.pid;
-                       timeouts.set(pid, setTimeout(function() {
-                               console.log("Cpu timeout; killing worker %s.", 
pid);
-                               worker.kill();
-                               spawn( pid );
+                       timeouts.set(msg.reqId, setTimeout(function() {
+                               timeouts.delete( msg.reqId );
+                               if ( worker.id in cluster.workers ) {
+                                       console.log("Cpu timeout; killing 
worker %s.", pid);
+                                       worker.kill();
+                                       spawn();
+                               }
                        }, msg.timeout));
                }
        };
@@ -101,7 +101,7 @@
                if ( !worker.suicide ) {
                        var pid = worker.process.pid;
                        console.log('worker %s died (%s), restarting.', pid, 
code);
-                       spawn( pid );
+                       spawn();
                }
        });
 
diff --git a/package.json b/package.json
index d4e1b9c..dfac29c 100644
--- a/package.json
+++ b/package.json
@@ -14,6 +14,7 @@
                "handlebars": "~1.3.0",
                "html5": "~1.0.5",
                "html5-entities": "~1.0.0",
+               "node-uuid": "~1.4.1",
                "pegjs": "git+https://github.com/arlolra/pegjs#startOffset";,
                "prfun": "~1.0.1",
                "request": "~2.40.0",

-- 
To view, visit https://gerrit.wikimedia.org/r/169198
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I102014ac4e7191bdeead5c3d8e235231c3cd87f8
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/parsoid
Gerrit-Branch: master
Gerrit-Owner: Arlolra <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to