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

Missing common APIs like Reader:Close() Writer:Flush() #45

Open
evelikov opened this issue Aug 5, 2021 · 3 comments
Open

Missing common APIs like Reader:Close() Writer:Flush() #45

evelikov opened this issue Aug 5, 2021 · 3 comments

Comments

@evelikov
Copy link

evelikov commented Aug 5, 2021

Greetings everyone,

I'm a little fresh with golang, so hope I am looking at the right place.

Going through the API - it seems like Reader:Close and Writer:Flush are missing.
These are fairly common - as you can see in the the golang standard library zlib and zgip.

Is it possible to have these in future release?
Thanks in advance

@ulikunitz
Copy link
Owner

I didn't add those methods by design and it is unlikely that I will add those methods in the future.

The Close method would require clients to add boiler plate code and it wouldn't do anything that the Garbage Collector can do itself. Note that the API doesn't open files directly and handles only io.Reader objects, which don't have a Close function. So a Close function is not required for the API to work properly.

The Flush method might make sense by pushing all buffered data to the underlying writer. I didn't add it because it will not produce a complete xz file, which requires a footer. It also would have a negative effect on the compression ratio because at least an LZMA2 segment would need to be closed. Theoretically the Flush method could close the xz file and start a new one, but that would be quire surprising for the user. I'd rather would like the cient to express that explicitly by using Close and create a new Writer. There is no use case that really requires a Flush method for the xz Writer.

Antoine de Saint-Exupery described the design principle, I follow, best: "Perfection is not achieved, if you cannot add anything anymore, but if you cannot take anything away."

@evelikov
Copy link
Author

evelikov commented Aug 8, 2021

Is the GC really enough, or the right answer even to handle the lack of Close()?

I'm wondering, if it were that simple the core golang developers would have omitted Close() in a lot of places. Wouldn't they? Are you sure we are no cases where the explicit Close() is the right thing to do? Say, reading thousands of XZ archives or something?

On the Flush() front - doesn't [zstd])https://github.com/DataDog/zstd) and other formats also require a footer? If so, having similar behaviour would be great.

The mentioned design principle is admirable goal, yet omitting the APIs seems to be going against the golang ecosystem - standard library and third party libs alike.

Thanks for for prompt reply and input.

@ulikunitz
Copy link
Owner

ulikunitz commented Aug 15, 2021

We should discuss these two methods separately.

Close(): Right now there is no need for it, because there is no resource that the garbage collector cannot release itself.

Flush(): The only use case that makes sense is a streaming application, where the user want to make sure that all buffered data is written to the underlying writer (e.g. a socket). While I don't think xz would be a good fit for the use case compared to zstd, I will look into it in the next version. (I plan to release such a version this year.)

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

No branches or pull requests

2 participants