-
Notifications
You must be signed in to change notification settings - Fork 4
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
issues/266: use http.Handler as the http middleware instead of http.HandlerFunc #269
Merged
Conversation
This file contains 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
count := 0 return func(w http.ResponseWriter, r *http.Request) { count = count + 1 r.Header.Set("MY-COUNT", fmt.Sprint(count)) wrappedHandler(w, r) } } func twoMiddleware(wrappedHandler http.Handler) http.HandlerFunc { count := 0 return func(w http.ResponseWriter, r *http.Request) { count = count + 1 r.Header.Set("MY-COUNT", fmt.Sprint(count)) wrappedHandler.ServeHTTP(w, r) } } func threeMiddleware(wrappedHandler http.Handler) http.Handler { count := 0 return http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { count = count + 1 r.Header.Set("MY-COUNT", fmt.Sprint(count)) wrappedHandler.ServeHTTP(w, r) }, ) } func BenchmarkMid(b *testing.B) { b.Run("oneMiddleware", func(b *testing.B) { h := oneMiddleware(func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "ok") }) rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/someUri", nil) b.ReportAllocs() b.ResetTimer() for n := 0; n < b.N; n++ { h.ServeHTTP(rec, req) } }) b.Run("twoMiddleware", func(b *testing.B) { h := twoMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "ok") })) rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/someUri", nil) b.ReportAllocs() b.ResetTimer() for n := 0; n < b.N; n++ { h.ServeHTTP(rec, req) } }) b.Run("threeMiddleware", func(b *testing.B) { h := threeMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "ok") })) rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/someUri", nil) b.ReportAllocs() b.ResetTimer() for n := 0; n < b.N; n++ { h.ServeHTTP(rec, req) } }) } go test -benchmem -run=^$ -bench ^BenchmarkMid$ github.com/komuw/ong/mux -count=5 BenchmarkMid/oneMiddleware-8 358.7 ns/op BenchmarkMid/oneMiddleware-8 371.8 ns/op BenchmarkMid/oneMiddleware-8 343.6 ns/op BenchmarkMid/oneMiddleware-8 289.0 ns/op BenchmarkMid/oneMiddleware-8 291.4 ns/op BenchmarkMid/twoMiddleware-8 296.7 ns/op BenchmarkMid/twoMiddleware-8 353.3 ns/op BenchmarkMid/twoMiddleware-8 363.2 ns/op BenchmarkMid/twoMiddleware-8 366.2 ns/op BenchmarkMid/twoMiddleware-8 315.2 ns/op BenchmarkMid/threeMiddleware-8 299.0 ns/op BenchmarkMid/threeMiddleware-8 307.6 ns/op BenchmarkMid/threeMiddleware-8 324.9 ns/op BenchmarkMid/threeMiddleware-8 367.6 ns/op BenchmarkMid/threeMiddleware-8 373.3 ns/op The change we want is `threeMiddleware` and it seems competitive with the others.
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## main #269 +/- ##
==========================================
+ Coverage 77.58% 78.06% +0.48%
==========================================
Files 36 36
Lines 3337 3360 +23
==========================================
+ Hits 2589 2623 +34
+ Misses 623 612 -11
Partials 125 125
☔ View full report in Codecov by Sentry. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What:
http.Handler
as the http middleware instead ofhttp.HandlerFunc
http.Handler
and returns ahttp.HandlerFunc
.This is more in line with the mantra;
accept interfaces, return concrete types
Why:
http.Handler
is a more idiomatic middleware since it is an interface whereas http.HandlerFunc is an implementation of that interfaceBenchmarks:
go test -benchmem -run=^$ -bench ^BenchmarkMid$ github.com/komuw/ong/mux -count=5 BenchmarkMid/oneMiddleware-8 358.7 ns/op BenchmarkMid/oneMiddleware-8 371.8 ns/op BenchmarkMid/oneMiddleware-8 343.6 ns/op BenchmarkMid/oneMiddleware-8 289.0 ns/op BenchmarkMid/oneMiddleware-8 291.4 ns/op BenchmarkMid/twoMiddleware-8 296.7 ns/op BenchmarkMid/twoMiddleware-8 353.3 ns/op BenchmarkMid/twoMiddleware-8 363.2 ns/op BenchmarkMid/twoMiddleware-8 366.2 ns/op BenchmarkMid/twoMiddleware-8 315.2 ns/op BenchmarkMid/threeMiddleware-8 299.0 ns/op BenchmarkMid/threeMiddleware-8 307.6 ns/op BenchmarkMid/threeMiddleware-8 324.9 ns/op BenchmarkMid/threeMiddleware-8 367.6 ns/op BenchmarkMid/threeMiddleware-8 373.3 ns/op
The change we want is
twoMiddleware
and it seems competitive with the others.