Documentation
¶
Overview ¶
Package gap 定义 Golaxy 应用层协议(Golaxy Application Protocol)。
GAP 运行在 GTP 或消息队列之上,负责承载应用层消息,适合服务到服务、 服务到客户端、以及路由转发等通信场景。当前包提供:
- 统一的消息接口、消息头和消息创建器
- Forward、RPC 请求/响应、单向 RPC 等基础消息模型
- 序列化与反序列化入口
- 配套的 codec 与 variant 子包,用于编解码和动态类型参数传输
当需要在稳定传输层之上表达业务消息、RPC 参数或可扩展载荷时,应优先使用 GAP。
Index ¶
- Variables
- func Marshal[T ReadableMsg](msg T) (ret binaryutil.Bytes, err error)
- func Unmarshal(msg Msg, data []byte) error
- type IMsgCreator
- type Msg
- type MsgForward
- type MsgHead
- type MsgId
- type MsgOnewayRPC
- type MsgPacket
- type MsgRPCReply
- type MsgRPCRequest
- type Origin
- type ReadableMsg
- type SerializedMsg
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ErrGAP 是 GAP 消息处理错误的根错误。 ErrGAP = errors.New("gap") )
View Source
var ( // ErrNotDeclared 表示指定消息 ID 尚未注册。 ErrNotDeclared = fmt.Errorf("%w: msg not declared", ErrGAP) )
Functions ¶
func Marshal ¶ added in v0.1.36
func Marshal[T ReadableMsg](msg T) (ret binaryutil.Bytes, err error)
Marshal 编码消息并返回池化字节缓冲区;调用方使用完后必须调用 Release。
Types ¶
type IMsgCreator ¶
type IMsgCreator interface {
// Declare 注册消息指针的具体类型;重复 ID 会 panic。
Declare(msg Msg)
// New 创建指定消息 ID 对应的新消息指针。
New(msgId MsgId) (Msg, error)
}
IMsgCreator 按消息 ID 注册并构造 GAP 消息。
type MsgForward ¶ added in v0.1.32
type MsgForward struct {
Src Origin // 原始消息来源。
Dst string // 目标通信地址。
CorrId int64 // 请求关联 ID;无需关联时为零。
TransId MsgId // 被转发消息的类型 ID。
TransData []byte // 被转发消息的已编码内容;解码时引用输入缓冲区。
}
MsgForward 将一个已编码 GAP 消息路由到目标地址。
type MsgHead ¶
type MsgHead struct {
Len uint32 // 完整消息包的字节数。
MsgId MsgId // 消息类型 ID。
Src Origin // 消息来源。
Seq int64 // 发送方分配的序号。
}
MsgHead 是每个 GAP 消息包的公共头部。
type MsgId ¶
type MsgId = uint32
MsgId 标识 GAP 消息类型。
const ( // MsgId_None 表示未设置消息类型。 MsgId_None MsgId = iota // MsgId_RPC_Request 标识需要响应的 RPC 请求。 MsgId_RPC_Request // MsgId_RPC_Reply 标识 RPC 响应。 MsgId_RPC_Reply // MsgId_OnewayRPC 标识无需响应的 RPC 通知。 MsgId_OnewayRPC // MsgId_Forward 标识封装其他消息的路由转发消息。 MsgId_Forward // MsgId_Customize 是自定义消息 ID 的起始偏移。 MsgId_Customize = 32 )
type MsgOnewayRPC ¶ added in v0.2.38
type MsgOnewayRPC struct {
CallChain variant.CallChain // 调用来源链。
Path []byte // 已编码调用路径;解码时引用输入缓冲区。
Args variant.Array // 调用参数。
}
MsgOnewayRPC 表示无需响应的 RPC 通知。
func (MsgOnewayRPC) MsgId ¶ added in v0.2.38
func (MsgOnewayRPC) MsgId() MsgId
MsgId 返回单向 RPC 消息的内置类型 ID。
type MsgPacket ¶
type MsgPacket struct {
Head MsgHead // 消息头。
Body ReadableMsg // 消息体;nil 表示只有消息头。
}
MsgPacket 组合 GAP 消息头和消息体。
type MsgRPCReply ¶
type MsgRPCReply struct {
CorrId int64 // 对应请求的关联 ID。
Rets variant.Array // 调用返回值。
Error variant.Error // 调用错误;OK 为 true 时表示成功。
}
MsgRPCReply 表示 RPC 请求的响应。
type MsgRPCRequest ¶
type MsgRPCRequest struct {
CorrId int64 // 用于匹配响应与 Future 的关联 ID。
CallChain variant.CallChain // 调用来源链。
Path []byte // 已编码调用路径;解码时引用输入缓冲区。
Args variant.Array // 调用参数。
}
MsgRPCRequest 表示需要响应的 RPC 请求。
type Origin ¶ added in v0.3.14
type Origin struct {
Svc string // 来源服务名。
Addr string // 来源通信地址。
Timestamp int64 // 来源 Unix 毫秒时间戳。
}
Origin 描述消息最初产生的服务、地址和时间。
type ReadableMsg ¶ added in v0.3.59
type ReadableMsg interface {
io.Reader
// Size 返回消息编码后的字节数。
Size() int
// MsgId 返回消息类型 ID。
MsgId() MsgId
}
ReadableMsg 表示可编码到字节流的 GAP 消息。
type SerializedMsg ¶ added in v0.2.17
SerializedMsg 将已有字节切片作为 GAP 消息体发送,不复制 Data。
Source Files
¶
Click to show internal directories.
Click to hide internal directories.