Transport Pipeline 參考 Netty ChannelPipeline,提供 Inbound 和 Outbound 接口,支持對消息或 I/O 事件擴(kuò)展?;?nbsp;In/OutboundHandler 可以擴(kuò)展實(shí)現(xiàn) TLS、限流、透傳信息處理等。如下圖所示,各個(gè) BoundHandler 會串行依次執(zhí)行。
// OutboundHandler is used to process write event.
type OutboundHandler interface {
Write(ctx context.Context, conn net.Conn, send Message) (context.Context, error)
}
// InboundHandler is used to process read event.
type InboundHandler interface {
OnActive(ctx context.Context, conn net.Conn) (context.Context, error)
OnInactive(ctx context.Context, conn net.Conn) context.Context
OnRead(ctx context.Context, conn net.Conn) (context.Context, error)
OnMessage(ctx context.Context, args, result Message) (context.Context, error)
}
Kitex 支持連接級別和請求級別限流,限流是為了保障服務(wù)的可用性,當(dāng)達(dá)到閾值應(yīng)當(dāng)及時(shí)限流,放到 Transport 層可以達(dá)到及時(shí)限流的目的,實(shí)現(xiàn)見 limiter_inbound.go。
元信息透傳是基于傳輸協(xié)議透傳一些 RPC 額外的信息給下游,同時(shí)讀取傳輸協(xié)議中上游透傳的信息,實(shí)現(xiàn)見 transmeta_bound.go。
為更明確的為使用者元信息透傳的擴(kuò)展能力,Kitex 單獨(dú)定義了信息透傳的處理接口 MetaHandler,這里會執(zhí)行 MetaHandler 進(jìn)行透傳信息的處理。
// MetaHandler reads or writes metadata through certain protocol.
type MetaHandler interface {
WriteMeta(ctx context.Context, msg Message) (context.Context, error)
ReadMeta(ctx context.Context, msg Message) (context.Context, error)
}
option: ?WithBoundHandler
?
svr := xxxservice.NewServer(handler, server.WithBoundHandler(yourBoundHandler))
option: ?WithBoundHandler
?
cli, err := xxxservice.NewClient(targetService, client.WithBoundHandler(yourBoundHandler))
更多建議: