9
现状
-
内置的中间件无法删除(中间件只能添加不能删)
func (s *engine) bindRoute(fr featuredRoutes, router httpx.Router, metrics *stat.Metrics, route Route, verifier func(chain alice.Chain) alice.Chain) error { chain := alice.New( handler.TracingHandler, s.getLogHandler(), handler.MaxConns(s.conf.MaxConns), handler.BreakerHandler(route.Method, route.Path, metrics), handler.SheddingHandler(s.getShedder(fr.priority), metrics), handler.TimeoutHandler(time.Duration(s.conf.Timeout)*time.Millisecond), handler.RecoverHandler, handler.MetricHandler(metrics), handler.PromethousHandler(route.Path), handler.MaxBytesHandler(s.conf.MaxBytes), handler.GunzipHandler, ) chain = s.appendAuthHandler(fr, chain, verifier) for _, middleware := range s.middlewares { chain = chain.Append(convertMiddleware(middleware)) } handle := chain.ThenFunc(route.Handler) return router.Handle(route.Method, route.Path, handle) }
-
某些场景下需要能够删除一些内置的中间件。举个例子:在内置的
DetailedLogHandler
或者LogHandler
中间件无法满足业务需求的情况下,我会自己实现一个LogHandler
并注入到go-zero框架,那么在功能上就会和内置的LogHandler
重叠并且存在不必要的性能损耗。