go-kit endpoint 翻译

endpoint

endpoint是许多Go kit组件的基本构建块。 端点由服务器实现,并由客户端调用。


func Nop

func Nop(context.Context, interface{}) (interface{}, error)

Nop不执行任何操作,并返回nil错误。 对测试有用。


type Endpoint

type Endpoint func(ctx context.Context, request interface{}) (response interface{}, err error)

Endpoint是服务器和客户端的基本构建块。 它代表一个单独的RPC方法。


type Failer

type Failer interface {
    Failed() error
}

Failer可以被包含业务逻辑错误的Go kit响应类型实现。 如果“失败”返回非nil错误,则Go kit传输层可能会将其解释为业务逻辑错误,并且可以对其进行不同于常规的成功响应的方式进行编码。

通常的响应类型不必实现Failer,但对于更复杂的用例可能会有所帮助。 addsvc示例显示了完整的应用程序应如何使用Failer。


type Middleware

type Middleware func(Endpoint) Endpoint

Middleware is a chainable behavior modifier for endpoints.
中间件是一个对endpoints的可连接行为的装饰(装饰者模式)。


func Chain

func Chain(outer Middleware, others ...Middleware) Middleware

Chain是一个能够组织中间件的辅助方法。 请求将按照声明的顺序遍历它们。 也就是说,第一个中间件被视为最外面的中间件(洋葱模式,逐层向中心发展)。


package endpoint

import (
	"context"
)

type Endpoint func(ctx context.Context, request interface{}) (response interface{}, err error)


func Nop(context.Context, interface{}) (interface{}, error) { return struct{}{}, nil }


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
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值