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

feat(compressor): added gzip/deflate compression support on request #12

Merged
merged 5 commits into from
Jan 4, 2025

Conversation

cnlangzi
Copy link
Member

@cnlangzi cnlangzi commented Jan 3, 2025

Added

Copy link

sourcery-ai bot commented Jan 3, 2025

Reviewer's Guide by Sourcery

This pull request introduces gzip and deflate compression support for requests. The App struct now has a compressors field, which contains a list of Compressor interfaces. The createWriter function checks the Accept-Encoding header of the request and uses the appropriate compressor if available. The existing request handlers now use the new createWriter function to wrap the response writer.

Sequence diagram for HTTP request compression flow

sequenceDiagram
    participant Client
    participant App
    participant Compressor
    participant ResponseWriter

    Client->>App: HTTP Request with Accept-Encoding
    App->>App: createWriter()
    App->>Compressor: Check AcceptEncoding()
    alt Compression supported
        Compressor->>ResponseWriter: Create compressed writer
        ResponseWriter-->>App: Return compressed writer
    else No compression
        App->>ResponseWriter: Create standard writer
        ResponseWriter-->>App: Return standard writer
    end
    App->>Client: Compressed/Standard Response
Loading

Class diagram for compression-related types

classDiagram
    class Compressor {
        <<interface>>
        +AcceptEncoding() string
        +New(rw ResponseWriter) ResponseWriter
    }

    class ResponseWriter {
        <<interface>>
        +Write(p []byte) (int, error)
        +Header() Header
        +WriteHeader(statusCode int)
        +Close()
    }

    class GzipCompressor {
        +AcceptEncoding() string
        +New(rw ResponseWriter) ResponseWriter
    }

    class DeflateCompressor {
        +AcceptEncoding() string
        +New(rw ResponseWriter) ResponseWriter
    }

    class gzipResponseWriter {
        -w *gzip.Writer
        +Write(p []byte) (int, error)
        +Close()
    }

    class deflateResponseWriter {
        -w *flate.Writer
        +Write(p []byte) (int, error)
        +Close()
    }

    Compressor <|.. GzipCompressor
    Compressor <|.. DeflateCompressor
    ResponseWriter <|.. gzipResponseWriter
    ResponseWriter <|.. deflateResponseWriter
Loading

File-Level Changes

Change Details Files
Added support for gzip and deflate compression.
  • Added a compressors field to the App struct.
  • Added a createWriter function to the App struct.
  • Added GzipCompressor and DeflateCompressor structs.
  • Added WithCompressor option.
  • Modified existing handlers to use the createWriter function.
  • Added ResponseWriter interface and implementations for gzip, deflate, and default response writers.
  • Added Compressor interface and implementations for gzip and deflate compressors.
app.go
compressor.go
compressor_gzip.go
compressor_deflate.go
response_writer.go
response_writer_gzip.go
response_writer_deflate.go
option.go
Updated context and README.
  • Updated context to access interceptor from app.
  • Updated README with Go Report Card badge link.
context.go
README.md

Assessment against linked issues

Issue Objective Addressed Explanation
#10 Support compression feature in response writer
#10 Handle Accept-Encoding header with multiple compression algorithms (gzip, deflate, br, zst) Only gzip and deflate are implemented, br and zst are not supported

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @cnlangzi - I've reviewed your changes and found some issues that need to be addressed.

Blocking issues:

  • The error from flate.NewWriter() should be handled rather than ignored (link)

Overall Comments:

  • Consider adding proper error handling for compression writer creation instead of discarding errors silently, especially in DeflateCompressor.New()
Here's what I looked at during the review
  • 🔴 General issues: 1 blocking issue, 1 other issue
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

compressor_deflate.go Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
Copy link

codecov bot commented Jan 3, 2025

Codecov Report

Attention: Patch coverage is 94.11765% with 4 lines in your changes missing coverage. Please review.

Project coverage is 83.12%. Comparing base (e7af653) to head (001a5ae).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
context.go 0.00% 3 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #12      +/-   ##
==========================================
+ Coverage   82.28%   83.12%   +0.83%     
==========================================
  Files          21       26       +5     
  Lines         988     1037      +49     
==========================================
+ Hits          813      862      +49     
  Misses        138      138              
  Partials       37       37              
Flag Coverage Δ
Unit-Tests 83.12% <94.11%> (+0.83%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@cnlangzi cnlangzi merged commit a419c00 into main Jan 4, 2025
6 checks passed
@cnlangzi cnlangzi deleted the feat/compressor branch January 4, 2025 00:37
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.

[feat]support Accept-Encoding
1 participant