Skip to content
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

Override Response.setTimeout to make response timeout handler work #69

Merged
merged 3 commits into from
Dec 27, 2019

Conversation

gyszalai
Copy link
Contributor

Checklist

  • run npm run test and npm run benchmark
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message and code follows Code of conduct

lib/response.js Outdated
@@ -46,7 +46,18 @@ function Response (req, onEnd, reject) {

util.inherits(Response, http.ServerResponse)

let timeoutHandle
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this reference should be stored in the constructor function Response

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Eomm Id don't really get it. do you mean I should set it as a property and access it as this.timeoutHandle?

lib/response.js Outdated
@@ -46,7 +46,18 @@ function Response (req, onEnd, reject) {

util.inherits(Response, http.ServerResponse)

let timeoutHandle
Response.prototype.setTimeout = function (msecs, callback) {
if (callback) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not permit to call this function without a callback, otherwise, the user can't understand why it is not working

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, shall I throw an error if no callback is specified?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the http docs, the callback is optional: https://nodejs.org/api/http.html#http_response_settimeout_msecs_callback

lib/response.js Outdated
Response.prototype.writeHead = function () {
if (timeoutHandle) {
clearTimeout(timeoutHandle)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be the write function a better place where clear this timeout?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Eomm ok, I'll move it there

lib/response.js Outdated
let timeoutHandle
Response.prototype.setTimeout = function (msecs, callback) {
if (callback) {
timeoutHandle = setTimeout(callback, msecs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need to emit the 'timeout' event when the setTimeout triggers (independently of the presence of the callback)

See https://nodejs.org/api/net.html#net_socket_settimeout_timeout_callback for more details.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mcollina according to the docs, the timeout event should be emitted by the socket. how can I cet a reference to the underlying socket?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is calling this.socket.emit('timeout') enough?

lib/response.js Outdated Show resolved Hide resolved
lib/response.js Outdated Show resolved Hide resolved
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@gyszalai
Copy link
Contributor Author

Somebody please, merge this PR.

@mcollina mcollina merged commit 961911e into fastify:master Dec 27, 2019
@gyszalai gyszalai deleted the response_set_timeout_handler branch December 27, 2019 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants