-
Notifications
You must be signed in to change notification settings - Fork 30k
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
streams: introduce StreamWrap and JSStream #926
Conversation
Introduce a way to wrap plain-js `stream.Duplex` streams into C++ StreamBase's child class. With such method at hand it is now possible to pass `stream.Duplex` instance as a `socket` parameter to `tls.connect()`.
var raw = net.connect(common.PORT); | ||
|
||
var pending = false; | ||
raw.on('readable', function() { |
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 one bothers me a bit, I wonder if it could be improved?
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.
@indutny What about it bothers you?
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.
Whole pending
thing. I think there might be a way to do it in a way more grounded in a Streams API
}); | ||
} | ||
util.inherits(StreamWrap, Socket); | ||
exports.StreamWrap = StreamWrap; |
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.
Tiny nit: may as well module.exports =
here.
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.
Yeah, good idea.
this.stream.once('end', function() { | ||
self._handle.emitEOF(); | ||
}); | ||
|
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.
What happens when the underlying stream emits an error? Per this diagram I don't think the stream will emit "end" or "finish". Is there a way to recover from an error in this state?
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 guess this should be handled by the code that created this stream... it could be propagated to the StreamWrap
object, though, and handled in _tls_wrap.js
. Does this sound plausible?
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 think that sound plausible – my expectation would be that a StreamWrap'd stream would expose the inner stream's errors on the outer stream.
I guess this should be handled by the code that created this stream...
This could be a situation where a user gets a stream from package X, instantiates it and passes it to TLSSocket Y; I think the user assumes responsibility for adding error handlers to TLSSocket Y, while TLSSocket Y would be responsible for shutting down / closing / cleaning up once an error on stream X happens.
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.
But this is not the way it works for piping the streams right now, right?
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.
An error downstream will unpipe and clean up listeners on the upstream stream, but will not shut down or destroy the source stream. This has caused a bit of confusion for stream users.
In addition, the act of nesting streams is distinct from piping streams, though piping can be involved in nesting. We don't nest streams often in core (TLS and arguably http are the only places we do something like this.) In userland usually the wrapping stream assumes responsibility for shutting itself down on inner stream error and exposing those errors to userland. Illustrated:
// user assumes responsibility for streams a, b, and c
a = stream()
b = stream()
c = stream()
a.pipe(b).pipe(c).pipe(b).pipe(a)
// user assumes responsibility for streams a and b, but not stream c
a = stream()
c = stream()
b = stream(c)
a.pipe(b).pipe(a).
I think this is a good idea in general. One comment would be that in the not too distant future read_start/read_stop will be replaced by an asynchronous read() version. Hopefully this feature doesn't get in the way. |
@piscisaureus we'll figure it out by then, thanks! |
I'm +1 on this addition, this is cool – I want to make sure it all works with the streams state machine. If possible, I'd like to see this applied to http streams as well before making the API publicly available, just to make sure the abstraction is broadly applicable – that isn't a blocker for this PR though. The only blocking comment I have is the error handling behavior. @piscisaureus Out of curiosity, would the async read be at handle level or JS stream level? |
self.stream.write(buf, done); | ||
}); | ||
|
||
function done() { |
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.
We may want to take error
here.
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.
Actually, ignore that – if we're listening on error
on the inner stream we should be okay.
StreamWrap.prototype.shutdown = function shutdown(req) { | ||
var self = this; | ||
|
||
this.stream.end(function() { |
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 think we can get an error here, too.
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.
No, we can't :(
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 am mistaken!
|
||
bufs.forEach(function(buf) { | ||
self.stream.write(buf, done); | ||
}); |
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.
Might be useful to sniff for self.stream._writev
, and if present, surround the forEach
with cork() / uncork()
?
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.
Good suggestion.
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 think it should be enough to just use .cork()
/.uncork()
and hope for the best.
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.
Fixed.
A few things:
Otherwise LGTM pending the last bullet point. |
LGTM. |
Introduce a way to wrap plain-js `stream.Duplex` streams into C++ StreamBase's child class. With such method at hand it is now possible to pass `stream.Duplex` instance as a `socket` parameter to `tls.connect()`. PR-URL: #926 Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com>
Landed in 1738c77, thank you! |
Introduce a way to wrap plain-js `stream.Duplex` streams into C++ StreamBase's child class. With such method at hand it is now possible to pass `stream.Duplex` instance as a `socket` parameter to `tls.connect()`. PR-URL: nodejs#926 Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com>
This feature is amazing :O |
Notable changes: * stream: Fixed problems for platforms without `writev()` support, particularly Windows. Changes introduced in 1.4.1, via #926, broke some functionality for these platforms, this has now been addressed. #1008 (Fedor Indutny) * arm: We have the very beginnings of ARMv8 / ARM64 / AARCH64 support. An upgrade to OpenSSL 1.0.2 is one requirement for full support. #1028 (Ben Noordhuis) * Add new collaborator: Julian Duque @julianduque
Introduce a way to wrap plain-js
stream.Duplex
streams into C++StreamBase's child class. With such method at hand it is now possible to
pass
stream.Duplex
instance as asocket
parameter totls.connect()
.cc @iojs/collaborators @iojs/streams @chrisdickinson