Endpoint
定义约定了一种实现形式
file: endpoint.go
// Endpoint is the fundamental building block of servers and clients.
// It represents a single RPC method.
type Endpoint 定义了 服务器和客户端的基本构建块,他表示实现单个RPC的方法
传入 context 上下文,request 请求并返回 response 响应及 err 错误
type Endpoint func(ctx context.Context, request interface{}) (response interface{}, err error)
Nop 是一个不执行任何操作,并返回 nil 的 端点(endpoint)
func Nop(context.Context, interface{}) (interface{}, error) { return struct{}{}, nil }
// Middleware is a chainable behavior modifier for endpoints.
中间件(Middleware) 是定义端点的 行为链接修改器
type Middleware func(Endpoint) Endpoint
Chain is a helper function for composing middlewares. Requests will
traverse them in the order they’re declared. That is, the first middleware
is treated as the outermost middleware.
Chain 是组成中间件的辅助函数,请求将按照注册声明的顺序遍历执行它们,也就是说,第一个注册的中间件,被视为最外层的中间件。
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)
}
}
// Failer may be implemented by Go kit response types that contain business
// logic error details. If Failed returns a non-nil error, the Go kit transport
// layer may interpret this as a business logic error, and may encode it
// differently than a regular, successful response.
//
// It’s not necessary for your response types to implement Failer, but it may
// help for more sophisticated use cases. The addsvc example shows how Failer
// should be used by a complete application.
Failer 接口 可以由包含业务的Go kit响应类型 实现
逻辑错误详细信息。如果Failed返回非nil错误,则Go kit传输
层可以将其解释为业务逻辑错误,并对其进行编码
不同于常规的,成功的回应。
您的响应类型不必实现Failer,但它可能
帮助更复杂的用例。addsvc示例显示了Failer
应该由完整的应用程序使用。
type Failer interface {
Failed() error
}
简要分析
- 该程序定义了两个 func 类型
type Endpoint func(ctx context.Context, request interface{}) (response interface{}, err error) type Middleware func(Endpoint) Endpoint
- 定义了一个 接口类型
Failer
- 实现一个方法 Chain 构建 出 Middleware 编历的方法