Skip to content

feat: add SkipMiddlewareOnNotFound to short-circuit 404/405 requests #2761#2910

Open
its-ernest wants to merge 3 commits intolabstack:masterfrom
its-ernest:fix/skip-middleware-on-not-found
Open

feat: add SkipMiddlewareOnNotFound to short-circuit 404/405 requests #2761#2910
its-ernest wants to merge 3 commits intolabstack:masterfrom
its-ernest:fix/skip-middleware-on-not-found

Conversation

@its-ernest
Copy link

@its-ernest its-ernest commented Mar 4, 2026

Fix for issue: #2761

This PR addresses #2761 by introducing SkipMiddlewareOnNotFound. This allows developers to avoid executing heavy global middleware (Auth, DB logging) for requests that result in a 404 or 405, improving performance and reducing log noise

Testing with below cmd saves 3 allocations.

go test -bench=BenchmarkMiddleware404 -benchmem

Sample output:

BenchmarkMiddleware404/Normal_404-8             {"time":"2026-03-04T22:00:36.5728429Z","level":"ERROR","msg":"failed to shut down server within given timeout","error":"context deadline exceeded"}
 1416462               890.7 ns/op           894 B/op         10 allocs/op
BenchmarkMiddleware404/Optimized_404-8           1000000              1472 ns/op             515 B/op          7 allocs/op
PASS
ok      github.com/labstack/echo/v5     6.217s

10-7 = 3 allocations saved for faster runtime

@its-ernest its-ernest marked this pull request as draft March 4, 2026 21:56
@its-ernest its-ernest marked this pull request as ready for review March 4, 2026 21:56
@its-ernest
Copy link
Author

SkipMiddlewareOnNotFound runs router.Route before applyMiddleware, so global middleware (auth, rate limits) will not run on 404/405 when enabled; worth stating on the Echo field. nit: the comment above IsMethodNotAllowed says IsMethodAllowed and reads inverted.

Thanks for the catch. I have updated the comment for IsMethodNotAllowed and clarified the documentation for the SkipMiddlewareOnNotFound field

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.

1 participant