Go - Kit 笔记 - 01 - Endpoint

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/vrg000/article/details/83656149

Endpoint – 端点

位于github.com/go-kit/kit/endpoint/包,里面有一个endpoint.go文件,定义了Endpoint的相关接口。

//端点 -- go-kit的基本模块,实则是一个(函数类型)。
//定义好函数后,把函数注册到http或grpc上,就可以实现业务函数的回调。
type Endpoint func(ctx context.Context, request interface{}) (response interface{}, err error)

//空端点 -- 一个端点的实例,不做任何事情。测试用的。
func Nop(context.Context, interface{}) (interface{}, error) { return struct{}{}, nil }

//中间件 -- 其实就是装饰器、装饰者模式,用于装饰端点。
//go-kit所有对端点的操作都是由装饰器来完成的。
type Middleware func(Endpoint) Endpoint

//链 -- 中间件的辅助功能,按照传参顺序组合中间件。
//第一个中间件为最外层,最后一个为最内层
func Chain(outer Middleware, others ...Middleware) Middleware {
	return func(next Endpoint) Endpoint {
		for i := len(others) - 1; i >= 0; i-- { // reverse
			next = others[i](next)
		}
		return outer(next)
	}
}

//故障器 -- 没用过额。。
type Failer interface {
	Failed() error
}

猜你喜欢

转载自blog.csdn.net/vrg000/article/details/83656149