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

Returning this in net and its inheriting prototype methods #1657

Closed
th507 opened this issue May 8, 2015 · 12 comments
Closed

Returning this in net and its inheriting prototype methods #1657

th507 opened this issue May 8, 2015 · 12 comments
Labels
http Issues or PRs related to the http subsystem. https Issues or PRs related to the https subsystem. net Issues and PRs related to the net subsystem. semver-minor PRs that contain new features and should be released in the next minor version. tls Issues and PRs related to the tls subsystem.

Comments

@th507
Copy link

th507 commented May 8, 2015

One typical way to create a server is through

http.createServer(callback).listen(port);

Then we export the server for other purposes (testing, for example)

module.exports = http.createServer(callback).listen(port);

At this point, everything works as expected.

Later on, when we are trying to add a timeout to the http.Server, we might be tempted to write something like this:

module.exports = http.createServer(callback).listen(port).setTimeout(timeout);

At this time, errors might occur because unlike server.listen, server.setTimeout does not return the instance of http.server.

Event through http module is labelled as stability level 3, but the documentation does not specify the return value for server.listen and for server.setTimeout`. So maybe change the return value is not completely out of the question.

I understand there are multiple setTimeout functions in various modules (like net.setTimeout, message.setTimeout, server.setTimeout, and so on).

Maybe we could consider return http.Server for server.setTimeout(), or even return corresponding instances for all *.setTimeout functions (except the setTimeout in plain JavaScript).

If such changes are not possible, at least we could specify the expected return values for those APIs (namely server.listen, server.setTimeout and some others), so that we might avoid some confusions about those APIs.

@bnoordhuis
Copy link
Member

Seems like an okay change to me. For consistency, all setTimeout functions in lib/ (except the global one) should be updated to return this.

One thing to consider is that it's a permanent change. We can't retroactively change the setTimeout functions to return a token like the global setTimeout does, because that would break code that has come to rely on the previous behavior.

@bnoordhuis bnoordhuis added enhancement timers Issues and PRs related to the timers subsystem / setImmediate, setInterval, setTimeout. labels May 8, 2015
@silverwind
Copy link
Contributor

We should investigate various server, net and socket methods. Some are chainable, while many are not.

Like @bnoordhuis said, caution should be taken for cases where it makes sense to return something else.

@yosuke-furukawa
Copy link
Member

+1 to @silverwind
We would be better to reconsider what methods should be chainable.

@silverwind
Copy link
Contributor

Looking at just the net.Socket methods, these return undefined right now:

  • socket.setKeepAlive()
  • socket.setNoDelay()
  • socket.setTimeout()
  • socket.ref()
  • socket.unref()
  • socket.destroy()
  • socket.end()

@evanlucas
Copy link
Contributor

I'm +1 for returning this on those. I guess my question would be do those need to be in separate PRs?

@silverwind
Copy link
Contributor

Hmm, given the number of methods to change, I'd prefer them in one PR, maybe with separate commits for each method.

We need to be certain that we never intend to return anything else if we go ahead. For example, what if we want to report a failure in a setNoDelay call (maybe to OS doesn't support it)? Do we throw at this point or do we want to return false?

@silverwind
Copy link
Contributor

#1699 looking good so far on setTimeout. I'm thinking maybe we should just extend that PR to include the other methods as well and deliver it all in a semver-minor package?

@silverwind silverwind changed the title Maybe return http.Server for server.setTimeout() ? Returning this in net and it's inheriting prototype methods May 14, 2015
@silverwind silverwind added tls Issues and PRs related to the tls subsystem. http Issues or PRs related to the http subsystem. https Issues or PRs related to the https subsystem. net Issues and PRs related to the net subsystem. semver-minor PRs that contain new features and should be released in the next minor version. and removed timers Issues and PRs related to the timers subsystem / setImmediate, setInterval, setTimeout. labels May 14, 2015
@silverwind
Copy link
Contributor

One done, seven more socket methods to go. We'll need to check how each method reports errors, but I think they're all potential candidates.

@silverwind
Copy link
Contributor

@evanlucas maybe we can split up the work here. Would you like to add a this return value to setNoDelay and setKeepAlive given your recent involvement in them? I could check out the others.

@evanlucas
Copy link
Contributor

Yes. Was waiting until #1518 got ok'd and landed

@silverwind silverwind changed the title Returning this in net and it's inheriting prototype methods Returning this in net and its inheriting prototype methods May 19, 2015
@silverwind
Copy link
Contributor

Did socket.setKeepAlive and socket.setNoDelay in #1779. I didn't see much use for doing it in socket.destroy and socket.end as these should be the last call in the chain anyways.

@silverwind
Copy link
Contributor

All done, thank everyone

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
http Issues or PRs related to the http subsystem. https Issues or PRs related to the https subsystem. net Issues and PRs related to the net subsystem. semver-minor PRs that contain new features and should be released in the next minor version. tls Issues and PRs related to the tls subsystem.
Projects
None yet
Development

No branches or pull requests

6 participants