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
proposal: bufio: add Writer.UnwrittenData #59106
Comments
CC @dsnet |
The idea itself generally sounds fine, but it's not clear to me what this is trying to solve:
If we do add this, I suggest we name it |
My global is to address the issue of allowing users can flush data again when a flush error occurs, without being affected by previous errors. Given that, I fully agree with adding a new mod. |
This proposal has been added to the active column of the proposals project |
This does not sound like a great idea. Taking one of the motivating use cases, if a network connection is interrupted, there is no guarantee that earlier Write calls that succeeded were actually received on the other side. Being able to find the data that isn't yet flushed does not recover all the data that has been "lost". The same is true for writes to a file in a file system. If the kernel buffers some writes and then the disk has an I/O error, perhaps a future write will fail, but the earlier writes have failed too. If you run a sequence like Write+Flush+Close+Sync and they all succeed, or if you do Write+Flush+read an ack from the other side, then you know everything worked. If any of those fail, it is quite rare to be able to reliably tell what has and has not been received/written/etc. Adding UnwrittenData seems like it encourages people to think the available guarantees are stronger than they really are. |
Based on the discussion above, this proposal seems like a likely decline. |
No change in consensus, so declined. |
Overview:
Currently, bufio.Writer does not provide a way to retrieve data that has not been successfully written. If a Flush() call fails, data in the buffer cannot be written to the underlying io.Writer.
Even if flush is called continuously, it will always fail.
We propose adding an UnwrittenData() method to allow users to retrieve data that has not been successfully written in case of a failed Flush(), enabling additional processing or retrying.
Motivation:
In some cases, Flush() may fail, for example, due to a network interruption or the underlying io.Writer closing. This could result in some data in the buffer not being written to the underlying io.Writer and being lost, which could cause inconsistencies in the application state or data loss. Therefore, users need a way to retrieve data in the buffer that has not been successfully written.
Proposal:
We propose adding an UnwrittenData() method that returns data in the buffer that has not been successfully written.
If the data in the buffer has been successfully written to the underlying io.Writer before Flush() is called, UnwrittenData() returns an empty byte slice.
Compatibility:
This proposal has no impact on compatibility with existing code, as the new method does not modify existing interfaces or behavior.
Implementation:
The proposed method only needs to return data in the buffer that has not been successfully written, and does not need to modify any existing code or behavior.
Example:
Here is an example of how the UnwrittenData() method could be used:
The text was updated successfully, but these errors were encountered: