微前端架构如何改变企业的开发模式与效率提升
550
2022-10-22
chain - 处理程序包装链拥有作用域数据
chain
go get github.com/codemodus/chain
Package chain aids the composition of nested http.Handler instances.
Nesting functions is a simple concept. If your nested handler order does not need to be composable, please do not use this or any similar package and avoid adding a dependency to your project.
Usage
type Chain func New(handlers ...func(http.Handler) http.Handler) *Chain func (c *Chain) Append(handlers ...func(http.Handler) http.Handler) *Chain func (c *Chain) Copy(chain *Chain) func (c *Chain) End(handler http.Handler) http.Handler func (c *Chain) EndFn(handlerFunc http.HandlerFunc) http.Handler func (c *Chain) Merge(chains ...*Chain) *Chain
Setup
import ( // ... "github.com/codemodus/chain")func main() { // ... // Nested handlers write either "0" or "1" to the response body before // and after ServeHTTP() is called. // // endHandler writes "_END_" to the response body. ch00 := New(nestedHandler0, nestedHandler0) ch001 := ch00.Append(nestedHandler1) ch1 := New(nestedHandler1) ch1001 := ch1.Merge(ch001) mux := http.NewServeMux() mux.Handle("/00_End", ch00.EndFn(endHandler)) // Resp Body: "00_END_00" mux.Handle("/001_End", ch001.EndFn(endHandler)) // Resp Body: "001_END_100" mux.Handle("/1001_End", ch1001.EndFn(endHandler)) // Resp Body: "1001_END_1001" // ...}
Nestable http.Handler
func nestableHandler(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // ... next.ServeHTTP(w, r) // ... })}
More Info
Changes in go1.7+/chain2.0+
As of Go 1.7, the http package's Request type includes a field (accessed via the Context() method) which holds an implementation of context.Context. Further, the context package has been added to the standard library. There is now no need for the custom Handler defined in previous versions of chain. Please refer to the following command to ease the process of updating your source.
sed -r -e 's/chain\.Handler/http.Handler/g' \ -e 's/[a-zA-Z0-9]+ context\.Context, ([a-zA-Z0-9]+) (http\.ResponseWriter)/\1 \2/' \ -e 's/ServeHTTPContext\([a-zA-Z0-9]+, /ServeHTTP(/'
Beyond this, any usage of chain.Set(context.Context) will need to be modified manually. Adding the affected logic as a nested handler is a simple and effective alternative. Don't forget to run gofmt/goimports.
Documentation
View the GoDoc
Benchmarks
These results are for comparison of normally nested functions, and chained functions. Each benchmark includes 10 functions prior to the final handler.
go1.7benchmark iter time/iter bytes alloc allocs--------- ---- --------- ----------- ------BenchmarkChain10 20000 61.01 μs/op 3684 B/op 51 allocs/opBenchmarkChain10-4 20000 68.62 μs/op 3691 B/op 51 allocs/opBenchmarkChain10-8 20000 69.33 μs/op 3696 B/op 51 allocs/opBenchmarkNest10 20000 60.36 μs/op 3684 B/op 51 allocs/opBenchmarkNest10-4 20000 70.82 μs/op 3692 B/op 51 allocs/opBenchmarkNest10-8 20000 71.03 μs/op 3697 B/op 51 allocs/op
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~