New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added connect.timeout() middleware #519
Closed
Closed
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ab7b7be
stubbed in .timeout() middleware and test.
e6ad230
Implemented timeout() and req.clearTimeout()
5eb1b55
semi
f798af6
Merge branch 'refs/heads/middleware-timeout'
8b34296
Added default options
7d168f5
Updated thrown error
e7b746f
Added test for static middleware. I don't think .request() works the …
799de71
searching for events that should cancel the timeout
3099468
Added clearTimeout on socket 'drain' to support streaming (eg static …
e45833a
Updated drain to fire only once (so we don't leak event listeners)
80da935
Merge branch 'master' of git://github.com/senchalabs/connect into ups…
c07c0d3
Merge branch 'refs/heads/upstream'
3165d35
Added check to ensure clearTimeout is only created once, and the sing…
1b8e2c9
moved drain callback to the socket so it doesn't get recreated on eac…
21b334b
Updated timeout to use writeHead + setTimeouts instead of socket.setT…
hunterloftis c465b5e
Merge branch 'master' of https://github.com/senchalabs/connect
hunterloftis File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,50 @@ | |||
/*! | |||
* Connect - timeout | |||
* Ported from https://github.com/LearnBoost/connect-timeout | |||
* MIT Licensed | |||
*/ | |||
|
|||
/** | |||
* Timeout: | |||
* | |||
* Invokes `socket.setTimeout` to timeout idle requests | |||
* Adds req.clearTimeout() for long-running requests | |||
* | |||
* - `throwError`: throw an error instead of writing a status code | |||
* - `time`: timeout length in ms (default: 10000) | |||
* | |||
* @param {Object} options | |||
* @return {Function} | |||
* @api public | |||
*/ | |||
|
|||
module.exports = function timeout(options) { | |||
options = options || {}; | |||
options.time = options.time || 8000; | |||
options.code = options.code || 500; | |||
options.throwError = options.throwError || false; | |||
|
|||
return function(req, res, next) { | |||
var writeHead = res.writeHead, | |||
at = req.method + ' ' + req.url, | |||
timer = setTimeout(function() { | |||
if (options.throwError) { | |||
return next(new Error('Timeout ' + req.method + ' ' + req.url)); | |||
} | |||
res.writeHead(options.code); | |||
res.end(); | |||
}, options.time); | |||
|
|||
req.clearTimeout = function() { | |||
clearTimeout(timer); | |||
}; | |||
|
|||
res.writeHead = function() { | |||
res.writeHead = writeHead; | |||
req.clearTimeout(); | |||
return res.writeHead.apply(res, arguments); | |||
} | |||
|
|||
return next(); | |||
}; | |||
}; |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,79 @@ | |||
|
|||
var connect = require('../'); | |||
|
|||
var fixtures = __dirname + '/fixtures'; | |||
|
|||
var app = connect(); | |||
|
|||
app.use(connect.timeout({ | |||
code: 503, | |||
time: 500 | |||
})); | |||
|
|||
app.use(connect['static'](fixtures)); | |||
|
|||
var timeouts; | |||
app.use(function(err, req, res, next) { | |||
timeouts++; | |||
}); | |||
|
|||
app.use(function(req, res, next) { | |||
if (req.url === '/should/timeout') { | |||
// chill and wait for timeout | |||
} | |||
else if (req.url === '/should/not/timeout') { | |||
res.writeHead(200); | |||
res.end(); | |||
} | |||
else if (req.url === '/should/interrupt/timeout') { | |||
req.clearTimeout(); | |||
setTimeout(function() { | |||
res.writeHead(200); | |||
res.end(); | |||
}, 1000); // Wait until after timeout `time` | |||
} | |||
}); | |||
|
|||
// Tests | |||
|
|||
describe('connect.timeout()', function() { | |||
it('should timeout', function(done) { | |||
app.request() | |||
.get('/should/timeout') | |||
.end(function(res) { | |||
res.statusCode.should.equal(503); | |||
done(); | |||
}); | |||
}); | |||
|
|||
it('should not timeout', function(done) { | |||
app.request() | |||
.get('/should/not/timeout') | |||
.end(function(res) { | |||
res.statusCode.should.equal(200); | |||
done(); | |||
}); | |||
}); | |||
|
|||
it('should interrupt timeout', function(done) { | |||
app.request() | |||
.get('/should/interrupt/timeout') | |||
.end(function(res) { | |||
res.statusCode.should.equal(200); | |||
done(); | |||
}); | |||
}); | |||
|
|||
it('should serve static files without timeout', function(done){ | |||
timeouts = 0; | |||
app.request() | |||
.get('/favicon.ico') | |||
.end(function(res) { | |||
setTimeout(function() { | |||
timeouts.should.equal(0); | |||
done(); | |||
}, 1000); | |||
}); | |||
}); | |||
|
|||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this actually doesn't work as expected, I'll see if I can find a work-around
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was curious about how that was expected to work; I just duplicated the old connect-timeout stuff.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this sort of use-case will work better with #522
I need to think on that some more though, both have pros/cons, deferred errors like this one might invoke app.error several times which is good for reporting but bad for responding. we could tell people to check
if (res.headerSent)
but that's kinda lame, almost need one callback for reporting errors and one for responding :s but that's sorta lame tooThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only way a timeout should occur is if
res.end()
is not called within a preset amount of time. This means that there can only be one timeout error. This deferred error can only happen once at most. IMHO, this behavior is desired.Say an error occurs (i.e.
next(err)
is called)... the error handler can decide whether or not to callnext()
or dores.send(500);
or whatever. Maybe the error handler prints the message to stderr, or maybe an email is sent. If the error handler decides to callres.send()
, then technically, no timeout has happened. So, there is only one error reported to the client. Similarly, only one timeout error can happen.