-
Notifications
You must be signed in to change notification settings - Fork 4
feat: pluggable ServeMuxOptions, HTTP marshalers, and zstd compression #88
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package core | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/go-coldbrew/core/config" | ||
| "github.com/klauspost/compress/gzhttp" | ||
| ) | ||
|
|
||
| // newHTTPCompressionWrapper builds the gzhttp wrapper used by initHTTP. It | ||
| // negotiates gzip and (unless disabled) zstd from Accept-Encoding. Pulled out | ||
| // so it can be tested without standing up the full gateway. | ||
| func newHTTPCompressionWrapper(cfg config.Config) (func(http.Handler) http.HandlerFunc, error) { | ||
| return gzhttp.NewWrapper( | ||
| gzhttp.MinSize(cfg.HTTPCompressionMinSize), | ||
| gzhttp.EnableZstd(!cfg.DisableZstdCompression), | ||
| gzhttp.PreferZstd(!cfg.DisableZstdCompression && cfg.PreferZstd), | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| package core | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/go-coldbrew/core/config" | ||
| ) | ||
|
|
||
| func TestNewHTTPCompressionWrapper_NegotiatesEncoding(t *testing.T) { | ||
| body := strings.Repeat("payload-", 256) // ~2KiB, well above the 256-byte default | ||
|
|
||
| cases := []struct { | ||
| name string | ||
| cfg config.Config | ||
| acceptEncoding string | ||
| wantEncoding string | ||
| }{ | ||
| { | ||
| name: "zstd-preferred-default", | ||
| cfg: config.Config{HTTPCompressionMinSize: 256, PreferZstd: true}, | ||
| acceptEncoding: "gzip, zstd", | ||
| wantEncoding: "zstd", | ||
| }, | ||
| { | ||
| name: "client-only-gzip", | ||
| cfg: config.Config{HTTPCompressionMinSize: 256, PreferZstd: true}, | ||
| acceptEncoding: "gzip", | ||
| wantEncoding: "gzip", | ||
| }, | ||
| { | ||
| name: "zstd-disabled-falls-back-to-gzip", | ||
| cfg: config.Config{HTTPCompressionMinSize: 256, DisableZstdCompression: true, PreferZstd: true}, | ||
| acceptEncoding: "gzip, zstd", | ||
| wantEncoding: "gzip", | ||
| }, | ||
| { | ||
| name: "no-accept-encoding-no-compression", | ||
| cfg: config.Config{HTTPCompressionMinSize: 256, PreferZstd: true}, | ||
| acceptEncoding: "", | ||
| wantEncoding: "", | ||
| }, | ||
| { | ||
| name: "prefer-zstd-false-picks-gzip-when-equal-q", | ||
| cfg: config.Config{HTTPCompressionMinSize: 256, PreferZstd: false}, | ||
| acceptEncoding: "gzip, zstd", | ||
| wantEncoding: "gzip", | ||
| }, | ||
| } | ||
|
|
||
| handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
| w.Header().Set("Content-Type", "text/plain") | ||
| _, _ = w.Write([]byte(body)) | ||
| }) | ||
|
|
||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| wrapper, err := newHTTPCompressionWrapper(tc.cfg) | ||
| if err != nil { | ||
| t.Fatalf("newHTTPCompressionWrapper: %v", err) | ||
| } | ||
| wrapped := wrapper(handler) | ||
|
|
||
| req := httptest.NewRequest(http.MethodGet, "/", nil) | ||
| if tc.acceptEncoding != "" { | ||
| req.Header.Set("Accept-Encoding", tc.acceptEncoding) | ||
| } | ||
| rec := httptest.NewRecorder() | ||
| wrapped.ServeHTTP(rec, req) | ||
|
|
||
| got := rec.Header().Get("Content-Encoding") | ||
| if got != tc.wantEncoding { | ||
| t.Fatalf("Content-Encoding = %q, want %q", got, tc.wantEncoding) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestNewHTTPCompressionWrapper_BelowMinSize(t *testing.T) { | ||
| cfg := config.Config{HTTPCompressionMinSize: 256, PreferZstd: true} | ||
| wrapper, err := newHTTPCompressionWrapper(cfg) | ||
| if err != nil { | ||
| t.Fatalf("newHTTPCompressionWrapper: %v", err) | ||
| } | ||
|
|
||
| wrapped := wrapper(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
| w.Header().Set("Content-Type", "text/plain") | ||
| _, _ = w.Write([]byte("tiny")) | ||
| })) | ||
|
|
||
| req := httptest.NewRequest(http.MethodGet, "/", nil) | ||
| req.Header.Set("Accept-Encoding", "gzip, zstd") | ||
| rec := httptest.NewRecorder() | ||
| wrapped.ServeHTTP(rec, req) | ||
|
|
||
| if got := rec.Header().Get("Content-Encoding"); got != "" { | ||
| t.Fatalf("Content-Encoding = %q, want empty (body below MinSize)", got) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package core | ||
|
|
||
| import "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" | ||
|
|
||
| // httpServeMuxOptions accumulates options registered via | ||
| // RegisterServeMuxOption / RegisterHTTPMarshaler. Init-only — not safe for | ||
| // concurrent registration. Matches the existing config-function pattern in | ||
| // the interceptors package; the alternative (mutex/atomic) would diverge | ||
| // from the rest of the codebase for no real benefit at startup. | ||
| var httpServeMuxOptions []runtime.ServeMuxOption | ||
|
|
||
| // RegisterServeMuxOption appends a runtime.ServeMuxOption that initHTTP | ||
| // passes to runtime.NewServeMux. Registered options are applied AFTER core's | ||
| // built-ins (the incoming-header matcher derived from HTTPHeaderPrefixes, | ||
| // the application/proto and application/protobuf marshalers, and the | ||
| // span-route middleware), so: | ||
| // | ||
| // - Last-write-wins options — WithMarshalerOption for a given MIME, | ||
| // WithErrorHandler, WithRoutingErrorHandler, WithIncomingHeaderMatcher — | ||
| // can intentionally override core's defaults. Overriding the incoming | ||
| // header matcher disables the HTTPHeaderPrefixes wiring; reimplement it | ||
| // yourself if you still need that behavior. | ||
| // - Additive options — WithMiddlewares, WithMetadata, | ||
| // WithForwardResponseOption — stack with core's. | ||
| // | ||
| // Must be called before core.Run() (typically from a service's PreStart | ||
| // hook). Not safe for concurrent registration. | ||
| func RegisterServeMuxOption(opt runtime.ServeMuxOption) { | ||
| httpServeMuxOptions = append(httpServeMuxOptions, opt) | ||
| } | ||
|
|
||
| // RegisterHTTPMarshaler registers a runtime.Marshaler for the given MIME | ||
| // type on the HTTP gateway. Equivalent to | ||
| // RegisterServeMuxOption(runtime.WithMarshalerOption(mime, m)). | ||
| // | ||
| // To override the gateway's default fallback for unregistered Content-Types | ||
| // (which is protojson via runtime.JSONPb), register for runtime.MIMEWildcard. | ||
| // | ||
| // Must be called before core.Run(). Not safe for concurrent registration. | ||
| func RegisterHTTPMarshaler(mime string, m runtime.Marshaler) { | ||
| RegisterServeMuxOption(runtime.WithMarshalerOption(mime, m)) | ||
| } | ||
|
|
||
| func registeredServeMuxOptions() []runtime.ServeMuxOption { | ||
| return httpServeMuxOptions | ||
| } | ||
|
|
||
| func resetServeMuxOptionsForTest() { | ||
| httpServeMuxOptions = nil | ||
| } | ||
|
ankurs marked this conversation as resolved.
Outdated
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.