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

No callback on writeToStream? #124

Closed
YoDaMa opened this issue Feb 1, 2022 · 5 comments
Closed

No callback on writeToStream? #124

YoDaMa opened this issue Feb 1, 2022 · 5 comments
Assignees
Labels

Comments

@YoDaMa
Copy link

YoDaMa commented Feb 1, 2022

In the writeToStream() code there is no use of callbacks or checking the return value on write() calls. For instance here on connect:

stream.write(protocol.CONNECT_HEADER)

As a result the only way to see if the packet has failed to send is to listen for an event on the stream.

In aedes, there is an implicit assumption that whenever writeToStream() is called, the buffer highwatermark will be hit and then once the buffer drains 'drain' will be called. See the aedes write.js file: https://github.com/moscajs/aedes/blob/main/lib/write.js

What is the assumed behavior for writeToStream()? Do we assume that whenever we call it, the highwatermark will be hit and subsequently the drain will be called? Or is that a false assumption being made by the aedes code? What is the best way to check if the call succeeded to write the data to the stream or if it failed?

@mcollina do you have insights into this since you helped write mqtt-packet and the new aedes code?

@YoDaMa YoDaMa added the question label Feb 1, 2022
@mcollina
Copy link
Member

mcollina commented Feb 2, 2022

What is the assumed behavior for writeToStream()? Do we assume that whenever we call it, the highwatermark will be hit and subsequently the drain will be called? Or is that a false assumption being made by the aedes code?

Either drain or error will be emitted if writeToStream return false. There is no need to wait if it returns true.

What is the best way to check if the call succeeded to write the data to the stream or if it failed?

You don't want to wait for a confirmation that data is written to the stream. It has no real meaning of being actually sent on the wire, just that it has been received by the underlining layer. In fact, sending the data could fail even if the callback is invoked successfully.

The way Aedes does it is correct to achieve maximum throughput on a socket.

@vishnureddy17
Copy link
Member

Thanks for the clarification @mcollina.

Follow-up question: What is the rationale behind the design of emitting errors on the passed-in stream outside of the internal stream context? My naive inclination would be that error should only be emitted internally in the stream context and not from the outside.

For context, I've been discussing with @YoDaMa offline.

@mcollina
Copy link
Member

mcollina commented Feb 2, 2022

Follow-up question: What is the rationale behind the design of emitting errors on the passed-in stream outside of the internal stream context? My naive inclination would be that error should only be emitted internally in the stream context and not from the outside.

I don't understand the question. Some of the design of Node.js stream does not have a clear rationale, it just happened 🤦‍♂️😅.

@vishnureddy17
Copy link
Member

vishnureddy17 commented Feb 2, 2022

Sorry my question wasn't clear. In mqtt-packet, if there is an error then stream.emit('error') is called. I would think it is bad practice to call emit('error') on the stream instance because it is not part of the public API of streams and is an internal thing. What is the reason for mqtt-packet to do this?

Example:

stream.emit('error', new Error('Invalid protocol version'))

@mcollina
Copy link
Member

mcollina commented Feb 3, 2022

Oh! I forgot we were doing that in this module. This problem was fixed long ago in Node.js. We should be calling stream.destroy(err) instead.

Would you like to send a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants