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

issues/266: use http.Handler as the http middleware instead of http.HandlerFunc #269

Merged
merged 10 commits into from
Jun 8, 2023

Conversation

komuw
Copy link
Owner

@komuw komuw commented Jun 7, 2023

What:

  • Use http.Handler as the http middleware instead of http.HandlerFunc
  • With this change, a middleware accepts a http.Handler and returns a http.HandlerFunc.
    This is more in line with the mantra; accept interfaces, return concrete types

Why:

Benchmarks:

func oneMiddleware(wrappedHandler http.HandlerFunc) http.HandlerFunc {
	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)
		},
	)
}
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.

komuw added 8 commits June 7, 2023 22:59
	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.
@komuw komuw marked this pull request as ready for review June 7, 2023 21:02
@codecov-commenter
Copy link

Codecov Report

Patch coverage: 96.66% and project coverage change: +0.48 🎉

Comparison is base (ce5ce80) 77.58% compared to head (1b6c919) 78.06%.

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              
Impacted Files Coverage Δ
mux/mux.go 76.11% <84.21%> (+4.96%) ⬆️
middleware/auth.go 100.00% <100.00%> (ø)
middleware/client_ip.go 100.00% <100.00%> (ø)
middleware/cors.go 96.44% <100.00%> (ø)
middleware/csrf.go 88.07% <100.00%> (ø)
middleware/fingerprint.go 75.00% <100.00%> (ø)
middleware/gzip.go 68.15% <100.00%> (ø)
middleware/loadshed.go 76.71% <100.00%> (ø)
middleware/log.go 64.38% <100.00%> (ø)
middleware/middleware.go 100.00% <100.00%> (ø)
... and 8 more

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@komuw komuw merged commit c5a2a85 into main Jun 8, 2023
@komuw komuw deleted the issues/266-handler-everywhere branch June 8, 2023 11:35
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.

use http.Handler instead of http.HandlerFunc
2 participants