Skip to content

Re-wrote the middleware compress library.#401

Merged
pkieltyka merged 1 commit into
go-chi:masterfrom
awbraunstein:compress-rewrite-1
Feb 27, 2019
Merged

Re-wrote the middleware compress library.#401
pkieltyka merged 1 commit into
go-chi:masterfrom
awbraunstein:compress-rewrite-1

Conversation

@awbraunstein

Copy link
Copy Markdown
Contributor

This re-write introduces the concept of a Compressor which stores all the settings for how compression/encoding should happen. The old API was changed to use the new Compressor struct which should prevent this change from breaking current consumers.

Additionally, this uses a sync.Pool for encoders that have a Reset(io.Writer) method to reduce memory overhead.

The new API is as follows:

r := chi.NewRouter()
compressor := NewCompressor(5, "text/html", "text/css")
// Add in a no-op encoder.
compressor.SetEncoder("nop", func(w io.Writer, _ int) io.Writer {
	return w
})
r.Use(compressor.Handler())

This fixes #397.

This re-write introduces the concept of a Compressor which stores all the settings for how compression/encoding should happen. The old API was changed to use the new Compressor struct which should prevent this change from breaking current consumers.

Additionally, this uses a sync.Pool for encoders that have a Reset(io.Writer) method to reduce memory overhead.
@pkieltyka

Copy link
Copy Markdown
Member

wow @awbraunstein well done. the code looks very sharp and complete. I will review it in detail this evening. thanks for the quick turnaround

@pkieltyka

Copy link
Copy Markdown
Member

did a review, tested various edge cases manually, and works great. thanks again @awbraunstein

@pkieltyka pkieltyka merged commit 52e06e6 into go-chi:master Feb 27, 2019

@VojtechVitek VojtechVitek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sweet contribution! Thanks ❤️

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.

rewrite Compress middleware

3 participants