Skip to content

Commit

Permalink
Merge branch 'v0.12'
Browse files Browse the repository at this point in the history
Conflicts:
	lib/controller/base_controller.js
  • Loading branch information
mde committed Jul 24, 2014
2 parents 7af1ccf + 486bc85 commit 65189a4
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 28 deletions.
7 changes: 1 addition & 6 deletions lib/app/logging.js
@@ -1,7 +1,4 @@
var logging
, inFlight = require('../in_flight');

logging = {
var logging = {
initRequestLogger: function (reqUrl, reqObj, respObj, method, accessTime) {
// Probably should proxy events for wrapped Response obj
respObj.resp.addListener('finish', function () {
Expand All @@ -26,8 +23,6 @@ logging = {
catch(e) {
geddy.log.error('Logging failed for request, id ' + id);
}

inFlight.removeEntry(id);
});
}

Expand Down
19 changes: 12 additions & 7 deletions lib/app/request_helpers.js
Expand Up @@ -74,14 +74,19 @@ var requestHelpers = {

// Domains-based error-handling should make this less necessary
, initInFlight: function (reqObj, respObj, method, accessTime) {
var inFlightId = inFlight.addEntry({
request: reqObj
, method: method
, response: respObj
, accessTime: accessTime
var entry = inFlight.addEntry({
request: reqObj
, method: method
, response: respObj
, accessTime: accessTime
})
, id = entry.id;
reqObj._geddyId = id;
respObj._geddyId = id;

respObj.resp.addListener('finish', function () {
inFlight.removeEntry(id);
});
reqObj._geddyId = inFlightId;
respObj._geddyId = inFlightId;
}

, getParams: function (router, reqUrl, method) {
Expand Down
4 changes: 4 additions & 0 deletions lib/base_config.js
Expand Up @@ -14,6 +14,10 @@ config = {
, ssl: null
// Include stack traces in error pages
, detailedErrors: true
// Milliseconds before a request times out
// Value must be truthy + greater than zero
// to have effect -- default to no timeout
, requestTimeout: null
// Maps flash message keys to css classes
, flash: {
defaultClass: 'alert'
Expand Down
9 changes: 7 additions & 2 deletions lib/controller/base_controller.js
Expand Up @@ -17,6 +17,7 @@
*/
var crypto = require('crypto')
, utils = require('utilities')
, EventEmitter = require('events').EventEmitter
, Responder = require('./responder').Responder
, Negotiator = require('./responder/negotiator').Negotiator
, errors = require('../response/errors')
Expand Down Expand Up @@ -125,9 +126,13 @@ controller.BaseController = function () {
this._cacheableActions = [];

this._cacheableActionsTTL = {};

EventEmitter.call(this);
};

controller.BaseController.prototype = new (function () {
controller.BaseController.prototype = Object.create(EventEmitter.prototype);

utils.mixin(controller.BaseController.prototype, new (function () {
var _addFilter
, _execFilters
, _negotiateContent
Expand Down Expand Up @@ -770,6 +775,6 @@ controller.BaseController.prototype = new (function () {

};

})();
})());

exports.BaseController = controller.BaseController;
13 changes: 8 additions & 5 deletions lib/controller/init.js
Expand Up @@ -27,12 +27,15 @@ init = {
}

, inFlight: function (cb) {
// Register controller with the in-flight data
// Shouldn't be necessary assuming domains-based
// error-handling actually works
inFlight.updateEntry(this.request._geddyId, {
controller: this
var self = this
, id = this.request._geddyId
, entry = inFlight.getEntry(id);
entry.controller = this;
entry.on('timeout', function () {
self.emit('timeout');
self.respond('Request timed out', {statusCode: 504});
});
inFlight.setEntry(id, entry);
cb();
}

Expand Down
42 changes: 35 additions & 7 deletions lib/in_flight.js
@@ -1,19 +1,43 @@
var utils = require('utilities');
var utils = require('utilities')
, EventEmitter = require('events').EventEmitter
, InFlight
, InFlightEntry;

var InFlight = function () {
var _timeout = null;

InFlightEntry = function (props) {
this.id = utils.string.uuid();
utils.mixin(this, props);
EventEmitter.call(this);
};
InFlightEntry.prototype = Object.create(EventEmitter.prototype);

InFlight = function () {
this.entries = new utils.SortedCollection();
};

InFlight.prototype = new (function () {

this.addEntry = function (data) {
var id = utils.string.uuid();
this.entries.addItem(id, data);
return id;
var entry = new InFlightEntry(data);
this.entries.addItem(entry.id, entry);

if (_timeout) {
setTimeout(function () {
entry.emit('timeout', _timeout);
// If there's no controller to respond, at least
// output something useful
if (!entry.controller) {
entry.response.send('Request timed out', 504);
}
}, _timeout);
}

return entry;
};

this.updateEntry = function (id, obj) {
utils.mixin(this.entries.getItem(id), obj);
this.setEntry = function (id, obj) {
this.entries.setItem(id, obj);
};

this.removeEntry = function (id) {
Expand All @@ -32,6 +56,10 @@ InFlight.prototype = new (function () {
this.entries.each(handler);
};

this.setTimeout = function (t) {
_timeout = t;
};

})();

module.exports = new InFlight();
Expand Down
11 changes: 11 additions & 0 deletions lib/init/in_flight.js
@@ -0,0 +1,11 @@
var inFlight = require('../in_flight');

module.exports = {
init: function (app, callback) {
var t = app.config.requestTimeout;
if (t && t > 0) {
inFlight.setTimeout(t);
}
callback();
}
};
1 change: 1 addition & 0 deletions lib/init/index.js
Expand Up @@ -12,6 +12,7 @@ exports.init = function () {
, opts = args.shift() || {}
, items = [
'checkCompat'
, 'inFlight'
, 'build'
, 'router'
, 'model'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -8,7 +8,7 @@
"MVC",
"realtime"
],
"version": "0.12.12",
"version": "0.12.13",
"author": "Matthew Eernisse <mde@fleegix.org> (http://fleegix.org)",
"dependencies": {
"barista": "0.2.x",
Expand Down

0 comments on commit 65189a4

Please sign in to comment.