Skip to content

Conversation

@ryanfowler
Copy link
Contributor

@ryanfowler ryanfowler commented Aug 13, 2025

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 via coderabbitai. Feel free to close if you feel this is too minor 😅

Thanks for the awesome module!

Summary by CodeRabbit

  • Bug Fixes

    • Improved reliability when reading zstd-compressed responses, reducing failures after prior errors.
  • Performance

    • Better reuse of zstd decoders via pooling, lowering CPU/memory overhead and improving throughput.
  • Documentation

    • No changes to public APIs; behavior remains compatible.

@coderabbitai
Copy link

coderabbitai bot commented Aug 13, 2025

📝 Walkthrough

Walkthrough

zstdReader.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

Cohort / File(s) Summary of Changes
Zstd reader initialization logic
gzhttp/transport.go
Simplified initialization when zr.zr == nil to always attempt obtaining a pooled *zstd.Decoder and call Reset(zr.body), falling back to zstd.NewReader if none. Removed prior conditional that referenced zr.zerr. Post-init error handling, read path, pool reclamation, Close behavior and public signatures unchanged.

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
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~7 minutes

Suggested reviewers

  • klauspost
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a 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.zr before calling Reset, 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

📥 Commits

Reviewing files that changed from the base of the PR and between ad4a030 and 93ebcb2.

📒 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.zerr short-circuits before any (re)initialization. The change only removes a redundant inner check; it does not alter the early-return behavior.

@ryanfowler ryanfowler force-pushed the gzhttp-zstd-condition branch from 93ebcb2 to 9853d92 Compare August 13, 2025 06:18
Copy link

@coderabbitai coderabbitai bot left a 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

📥 Commits

Reviewing files that changed from the base of the PR and between 93ebcb2 and 9853d92.

📒 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.

@klauspost klauspost merged commit 86a9489 into klauspost:master Aug 13, 2025
22 checks passed
hantang pushed a commit to qundao/mirror-forgejo that referenced this pull request Oct 21, 2025
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` | [![age](https://developer.mend.io/api/mc/badges/age/go/github.com%2fklauspost%2fcompress/v1.18.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/go/github.com%2fklauspost%2fcompress/v1.18.0/v1.18.1?slim=true)](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 [@&#8203;klauspost](https://github.com/klauspost) in [#&#8203;1059](klauspost/compress#1059)
- s2: check for cap, not len of buffer in EncodeBetter/Best by [@&#8203;vdarulis](https://github.com/vdarulis) in [#&#8203;1080](klauspost/compress#1080)
- zstd: Add simple zstd EncodeTo/DecodeTo functions by [@&#8203;klauspost](https://github.com/klauspost) in [#&#8203;1079](klauspost/compress#1079)
- zlib: Avoiding extra allocation in zlib.reader.Reset by [@&#8203;travelpolicy](https://github.com/travelpolicy) in [#&#8203;1086](klauspost/compress#1086)
- gzhttp: remove redundant err check in zstdReader by [@&#8203;ryanfowler](https://github.com/ryanfowler) in [#&#8203;1090](klauspost/compress#1090)
- Run modernize. Deprecate Go 1.22 by [@&#8203;klauspost](https://github.com/klauspost) in [#&#8203;1095](klauspost/compress#1095)
- flate: Simplify matchlen by [@&#8203;klauspost](https://github.com/klauspost) in [#&#8203;1101](klauspost/compress#1101)
- flate: Add examples by [@&#8203;klauspost](https://github.com/klauspost) in [#&#8203;1102](klauspost/compress#1102)
- flate: Use exact sizes for huffman tables by [@&#8203;klauspost](https://github.com/klauspost) in [#&#8203;1103](klauspost/compress#1103)
- flate: Faster load+store by [@&#8203;klauspost](https://github.com/klauspost) in [#&#8203;1104](klauspost/compress#1104)
- Add notice to S2 about MinLZ by [@&#8203;klauspost](https://github.com/klauspost) in [#&#8203;1065](klauspost/compress#1065)

#### New Contributors

- [@&#8203;wooffie](https://github.com/wooffie) made their first contribution in [#&#8203;1069](klauspost/compress#1069)
- [@&#8203;vdarulis](https://github.com/vdarulis) made their first contribution in [#&#8203;1080](klauspost/compress#1080)
- [@&#8203;travelpolicy](https://github.com/travelpolicy) made their first contribution in [#&#8203;1086](klauspost/compress#1086)
- [@&#8203;ryanfowler](https://github.com/ryanfowler) made their first contribution in [#&#8203;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]>
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

Successfully merging this pull request may close these issues.

2 participants