-
Notifications
You must be signed in to change notification settings - Fork 158
Use built in zstd for compression when provided #503
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
base: main
Are you sure you want to change the base?
Conversation
For >=v28 of Erlang, Zstd is part of Erlang's stdlib. This greatly simplifies dependency requirements in Req. I took some great pains to match how ezstd is called and matching its return type. It did prove to get a little ugly though to prevent compiler type warnings however. Hopefully in several years this can be built with the expection zstd always exists.
| defp supported_accept_encoding do | ||
| "zstd, #{if brotli_loaded?(), do: "br, ", else: ""}gzip" | ||
| end |
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 did this just to preserve the accept encoding order that tests expect as it would be a bit of a headache otherwise.
| if Code.ensure_loaded?(:zstd) do | ||
| def zstd_decompress(arg) do | ||
| try do | ||
| arg | ||
| |> :zstd.decompress() | ||
| |> :erlang.iolist_to_binary() | ||
| rescue | ||
| err in ErlangError -> | ||
| case err do | ||
| %ErlangError{original: {:zstd_error, reason}} -> | ||
| {:error, reason} | ||
|
|
||
| err -> | ||
| reraise err, __STACKTRACE__ | ||
| end | ||
| end | ||
| end | ||
|
|
||
| defp supported_accept_encoding do | ||
| "zstd, #{if brotli_loaded?(), do: "br, ", else: ""}gzip" | ||
| end |
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 feel pretty gross about this code. I'm open to alternative suggestions here. This ended up being the implementation I was able to arrive at that returned similar values to :ezstd.decompress and kept the type checker happy (Code.ensure_loaded?(:zstd) always evaluates to true, I'm a bit unsure why wrapping it in the macro zstd_support?/1 did not trigger the same warnings however).
| assert Exception.message(e) == | ||
| assert Exception.message(e) in [ | ||
| "Could not decompress Zstandard data: \"Unknown frame descriptor\"", | ||
| # TODO remove once Erlang v28 is required |
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'm happy to blanket the codebase in TODO's like this, but I kinda felt like I was spreading garbage over a nicely manicured lawn.
|
Thank you. The PR looks good to me. I need some more time to figure out if it's worth it to keep support for |
For >=v28 of Erlang, Zstd is part of Erlang's stdlib. This greatly simplifies dependency requirements in Req.
I took some great pains to match how ezstd is called and matching its return type. It did prove to get a little ugly though to prevent compiler type warnings however.
Hopefully in several years this can be built with the expection zstd always exists.