Skip to content

Add Brotli support with pure go implementation #1587

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

Closed
wants to merge 1 commit into from

Conversation

delaneyj
Copy link

@delaneyj delaneyj commented Jun 9, 2020

It appears both #1223 was closed due to cgo usage. Found a pure go port and used Gzip middleware as a launch point. Tests pass and first contribution so let me know if I'm missing anything!

@codecov
Copy link

codecov bot commented Jun 9, 2020

Codecov Report

Merging #1587 (a327810) into master (43e32ba) will decrease coverage by 0.07%.
The diff coverage is 80.95%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1587      +/-   ##
==========================================
- Coverage   84.96%   84.88%   -0.08%     
==========================================
  Files          28       29       +1     
  Lines        2168     2210      +42     
==========================================
+ Hits         1842     1876      +34     
- Misses        211      216       +5     
- Partials      115      118       +3     
Impacted Files Coverage Δ
middleware/compress_gzip.go 77.27% <ø> (ø)
middleware/compress_brotli.go 80.95% <80.95%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 43e32ba...a327810. Read the comment docs.

@stale
Copy link

stale bot commented Aug 8, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Aug 8, 2020
@stale stale bot closed this Aug 15, 2020
@lammel
Copy link
Contributor

lammel commented Nov 20, 2020

The bot was too fast for me, sorry.
I actually wanted to review this PR.

@lammel lammel reopened this Nov 20, 2020
@stale stale bot removed the wontfix label Nov 20, 2020
res.Header().Set(echo.HeaderContentEncoding, brotliScheme) // Issue #806
rw := res.Writer

w := brotli.NewWriterOptions(rw, brotli.WriterOptions{Quality: config.Level})
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use a sync.Pool here to reduce the amount of allocations performed by this middleware. Yo can check the current implementation of compress.go or use #1672 as a reference

assert.Equal("test\n", string(chunkBuf))

// Write and flush the second part of the data
c.Response().Write([]byte("test\n"))
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use other body here to really be sure that you are getting the second part of the data and not the first part again

assert.Equal("test\n", string(chunkBuf))

// Write the final part of the data and return
c.Response().Write([]byte("test"))
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here

Skipper Skipper

// Brotli compression level.
// Optional. Default value -1.
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment is not correct. The default value is brotli.DefaultCompression

if config.Skipper == nil {
config.Skipper = DefaultBrotliConfig.Skipper
}
if config.Level == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Be aware that for the Brotli library that you added a Level equal to 0 is a valid level. I thinkit is better to remove this check and keep the Level received by parameter.

https://github.com/andybalholm/brotli/blob/729edfbcfeaad43a6412c185205501ec80773250/writer.go#L8-L12

const (
	BestSpeed          = 0
	BestCompression    = 11
	DefaultCompression = 6
)

@pafuent
Copy link
Contributor

pafuent commented Nov 22, 2020

Please, don't forget to update your PR with the latest changes on master

@lammel
Copy link
Contributor

lammel commented Dec 15, 2020

This PR will be considered for v5.

Concerns:

  • Adding an external dependency (maintenance state, code quality, security)
  • Higher memory use or binary size

It might also be an option to make this a contrib middleware and keep it seperate of core echo.

@pafuent
Copy link
Contributor

pafuent commented Jan 26, 2021

@delaneyj Could you please update this PR with the latest changes on master and also address the comments if they make sense?

@lammel
Copy link
Contributor

lammel commented May 8, 2021

Ping @delaneyj
Are you (or other contributors) still interested in getting this merged?
We may consider this for v5 then.

@aldas
Copy link
Contributor

aldas commented Jan 10, 2022

As this middleware uses external library "github.com/andybalholm/brotli" it should not be added to this repository. It should be moved to echo-contrib or created as a separate repository and added to third party middlewares list in Echo readme.md https://github.com/labstack/echo#third-party-middlewares . External dependencies in core are problematic as our hands are tied with semantic versioning

separate repo would + PR to add it to list would be best as then owner could iterate and have breaking changes as they choose.

@lammel
Copy link
Contributor

lammel commented Jan 24, 2022

Closing this PR due to inactivity and required additional dependencies.
May be reopened in the echo-contrib project.

@lammel lammel closed this Jan 24, 2022
@ex-tag
Copy link

ex-tag commented Oct 2, 2022

Is Brotili compression an Echo v5 middleware?
I checked https://github.com/labstack/echo-contrib and it does not seem to be available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants