-
Notifications
You must be signed in to change notification settings - Fork 349
gzhttp: remove redundant err check in zstdReader #1090
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
Conversation
📝 WalkthroughWalkthroughzstdReader.Read initialization was simplified: when zr.zr is nil it always attempts to get a *zstd.Decoder from zstdReaderPool and Reset(zr.body) (or creates a new decoder), removing the previous guard tied to zr.zerr. Error handling and public API unchanged. Changes
Sequence Diagram(s)sequenceDiagram
participant Caller
participant zstdReader as zstdReader.Read
participant Pool as zstdReaderPool
participant Decoder
Caller->>zstdReader: Read(p)
alt zr.zr is nil
zstdReader->>Pool: Get()
alt pooled decoder available
zstdReader->>Decoder: Reset(body)
else no pooled decoder
zstdReader->>Decoder: NewReader(body)
end
zstdReader->>zstdReader: set zr.zr / zr.zerr
alt zr.zerr != nil
zstdReader-->>Caller: return error
else
zstdReader->>Decoder: Read(p)
zstdReader-->>Caller: n, err
end
else zr.zr exists
zstdReader->>Decoder: Read(p)
zstdReader-->>Caller: n, err
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~7 minutes Suggested reviewers
✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 0
🧹 Nitpick comments (2)
gzhttp/transport.go (2)
211-216: Minor consistency/readability nit: name pooled var and order assignment like gzipReader.To mirror the gzipReader pattern and slightly improve readability, consider assigning the pooled decoder to
zr.zrbefore callingReset, and use a more descriptive local name.Apply this diff:
- reader, ok := zstdReaderPool.Get().(*zstd.Decoder) + dec, ok := zstdReaderPool.Get().(*zstd.Decoder) if ok { - zr.zerr = reader.Reset(zr.body) - zr.zr = reader + zr.zr = dec + zr.zerr = zr.zr.Reset(zr.body) } else { zr.zr, zr.zerr = zstd.NewReader(zr.body, zstd.WithDecoderLowmem(true), zstd.WithDecoderMaxWindow(32<<20), zstd.WithDecoderConcurrency(1)) }
198-204: Fix inaccurate comments referencing gzip in zstd reader.These comments still mention gzip; update to reflect zstd types/functions.
Apply this diff:
-// zstdReader wraps a response body so it can lazily -// call gzip.NewReader on the first call to Read +// zstdReader wraps a response body so it can lazily +// call zstd.NewReader on the first call to Read type zstdReader struct { body io.ReadCloser // underlying HTTP/1 response body framing - zr *zstd.Decoder // lazily-initialized gzip reader + zr *zstd.Decoder // lazily-initialized zstd decoder zerr error // any error from zstd.NewReader; sticky }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
gzhttp/transport.go(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
gzhttp/transport.go (2)
zstd/decoder.go (2)
Decoder(22-47)NewReader(87-123)zstd/decoder_options.go (3)
WithDecoderLowmem(45-47)WithDecoderMaxWindow(127-138)WithDecoderConcurrency(58-70)
🔇 Additional comments (2)
gzhttp/transport.go (2)
211-216: LGTM: redundant inner zerr check removed; logic unchanged.This simplifies the initialization path while preserving behavior. The early guard on Line 207 still prevents re-init when a sticky error is present.
207-216: Note: AI summary implies re-init after prior error; actual code returns early.With the guard on Line 207, a non-nil
zr.zerrshort-circuits before any (re)initialization. The change only removes a redundant inner check; it does not alter the early-return behavior.
93ebcb2 to
9853d92
Compare
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.
Actionable comments posted: 0
🧹 Nitpick comments (1)
gzhttp/transport.go (1)
211-216: Removal of redundant error guard is correct; consider not retaining the decoder on Reset error.Early return on Line 207 already covers zr.zerr, so removing the inner guard simplifies control flow without changing behavior. One small improvement: when a pooled decoder’s Reset fails, you currently set zr.zr = reader and rely on Close to return it to the pool. If the caller doesn’t call Close after an immediate Read error, the decoder won’t be returned to the pool. Prefer returning the decoder to the pool immediately on Reset failure and only assign zr.zr after a successful Reset.
Apply this diff within this block to avoid keeping a decoder reference on Reset error:
- zr.zerr = reader.Reset(zr.body) - zr.zr = reader + if err := reader.Reset(zr.body); err != nil { + // Return decoder to pool on failure. + reader.Reset(nil) + zstdReaderPool.Put(reader) + zr.zerr = err + } else { + zr.zr = reader + }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
gzhttp/transport.go(2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
gzhttp/transport.go (2)
zstd/decoder.go (2)
Decoder(22-47)NewReader(87-123)zstd/decoder_options.go (3)
WithDecoderLowmem(45-47)WithDecoderMaxWindow(127-138)WithDecoderConcurrency(58-70)
🔇 Additional comments (2)
gzhttp/transport.go (2)
199-200: Comment correction is accurate and consistent with implementation.The comment now correctly references zstd.NewReader for the zstd path. Matches actual behavior.
202-204: Field comment fix looks good.“zstd reader” accurately describes the decoder type; aligns with the surrounding code and the gzip variant above.
This PR contains the following updates: | Package | Change | Age | Confidence | |---|---|---|---| | [github.com/klauspost/compress](https://github.com/klauspost/compress) | `v1.18.0` -> `v1.18.1` | [](https://docs.renovatebot.com/merge-confidence/) | [](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>klauspost/compress (github.com/klauspost/compress)</summary> ### [`v1.18.1`](https://github.com/klauspost/compress/releases/tag/v1.18.1) [Compare Source](klauspost/compress@v1.18.0...v1.18.1) #### What's Changed - zstd: Fix incorrect buffer size in dictionary encodes by [@​klauspost](https://github.com/klauspost) in [#​1059](klauspost/compress#1059) - s2: check for cap, not len of buffer in EncodeBetter/Best by [@​vdarulis](https://github.com/vdarulis) in [#​1080](klauspost/compress#1080) - zstd: Add simple zstd EncodeTo/DecodeTo functions by [@​klauspost](https://github.com/klauspost) in [#​1079](klauspost/compress#1079) - zlib: Avoiding extra allocation in zlib.reader.Reset by [@​travelpolicy](https://github.com/travelpolicy) in [#​1086](klauspost/compress#1086) - gzhttp: remove redundant err check in zstdReader by [@​ryanfowler](https://github.com/ryanfowler) in [#​1090](klauspost/compress#1090) - Run modernize. Deprecate Go 1.22 by [@​klauspost](https://github.com/klauspost) in [#​1095](klauspost/compress#1095) - flate: Simplify matchlen by [@​klauspost](https://github.com/klauspost) in [#​1101](klauspost/compress#1101) - flate: Add examples by [@​klauspost](https://github.com/klauspost) in [#​1102](klauspost/compress#1102) - flate: Use exact sizes for huffman tables by [@​klauspost](https://github.com/klauspost) in [#​1103](klauspost/compress#1103) - flate: Faster load+store by [@​klauspost](https://github.com/klauspost) in [#​1104](klauspost/compress#1104) - Add notice to S2 about MinLZ by [@​klauspost](https://github.com/klauspost) in [#​1065](klauspost/compress#1065) #### New Contributors - [@​wooffie](https://github.com/wooffie) made their first contribution in [#​1069](klauspost/compress#1069) - [@​vdarulis](https://github.com/vdarulis) made their first contribution in [#​1080](klauspost/compress#1080) - [@​travelpolicy](https://github.com/travelpolicy) made their first contribution in [#​1086](klauspost/compress#1086) - [@​ryanfowler](https://github.com/ryanfowler) made their first contribution in [#​1090](klauspost/compress#1090) **Full Changelog**: <klauspost/compress@v1.18.0...v1.18.1> </details> --- ### Configuration 📅 **Schedule**: Branch creation - Between 12:00 AM and 03:59 AM ( * 0-3 * * * ) (UTC), Automerge - Between 12:00 AM and 03:59 AM ( * 0-3 * * * ) (UTC). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0MS4xNTIuOSIsInVwZGF0ZWRJblZlciI6IjQxLjE1Mi45IiwidGFyZ2V0QnJhbmNoIjoiZm9yZ2VqbyIsImxhYmVscyI6WyJkZXBlbmRlbmN5LXVwZ3JhZGUiLCJ0ZXN0L25vdC1uZWVkZWQiXX0=--> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9786 Reviewed-by: Earl Warren <[email protected]> Co-authored-by: Renovate Bot <[email protected]> Co-committed-by: Renovate Bot <[email protected]>
This PR removes a redundant error check in the
zstdReader.Read()method, as the error is checked immediately beforehand on line 207. It also fixes a couple comments from the suggestions below viacoderabbitai. Feel free to close if you feel this is too minor 😅Thanks for the awesome module!
Summary by CodeRabbit
Bug Fixes
Performance
Documentation