node

package
v2.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 29, 2026 License: MIT Imports: 32 Imported by: 9

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	InternalRoute   = RouteOptions{Internal: true}   // 内部路由,仅限节点间消息流转
	StatefulRoute   = RouteOptions{Stateful: true}   // 有状态路由,固定路由到绑定节点处理
	AuthorizedRoute = RouteOptions{Authorized: true} // 授权路由,流转时必需附带UID
)

Functions

This section is empty.

Types

type Actor added in v2.2.0

type Actor struct {
	// contains filtered or unexported fields
}

Actor Actor模型 拥有独立的消息队列与任务队列,保证其内部处理是线程安全的

func (*Actor) AddEventHandler added in v2.2.0

func (a *Actor) AddEventHandler(event cluster.Event, handler EventHandler)

AddEventHandler 添加事件处理器 @param event cluster.Event 事件类型 @param handler EventHandler 事件处理函数

func (*Actor) AddRouteHandler added in v2.2.0

func (a *Actor) AddRouteHandler(route int32, handler RouteHandler)

AddRouteHandler 添加路由处理器 @param route int32 路由号 @param handler RouteHandler 路由处理函数

func (*Actor) AfterFunc added in v2.2.4

func (a *Actor) AfterFunc(d time.Duration, f func()) (*Timer, error)

AfterFunc 延迟调用,与官方的time.AfterFunc用法一致 @param d time.Duration 延迟时长 @param f func() 待调用的函数 @return @1 *Timer 定时器,可通过Stop取消 @return @2 error Actor未启动时返回的错误

func (*Actor) AfterInvoke added in v2.2.4

func (a *Actor) AfterInvoke(d time.Duration, f func()) (*Timer, error)

AfterInvoke 延迟调用(线程安全) 延迟后通过任务队列串行执行函数,保证Actor内线程安全 @param d time.Duration 延迟时长 @param f func() 待调用的函数 @return @1 *Timer 定时器,可通过Stop取消 @return @2 error Actor未启动时返回的错误

func (*Actor) Deliver added in v2.2.0

func (a *Actor) Deliver(uid int64, message *cluster.Message) error

Deliver 投递消息到当前Actor中进行处理 @param uid int64 用户ID @param message *cluster.Message 待投递的消息 @return @1 error 打包消息或投递失败时返回的错误

func (*Actor) Destroy added in v2.2.0

func (a *Actor) Destroy() (ok bool)

Destroy 销毁Actor @return @1 bool 是否成功销毁,Actor不存在或已销毁时返回false

func (*Actor) ID added in v2.2.0

func (a *Actor) ID() string

ID 获取Actor的ID @return @1 string Actor的ID

func (*Actor) Invoke added in v2.2.0

func (a *Actor) Invoke(f func(), wait ...bool) error

Invoke 调用函数(Actor内线程安全) 任务写入Actor的任务队列串行执行;阻塞模式下会等待函数执行完成 @param f func() 待调用的函数 @param wait ...bool 是否等待调用完成,默认不等待 @return @1 error Actor未启动或任务入队失败时返回的错误

func (*Actor) Kind added in v2.2.0

func (a *Actor) Kind() string

Kind 获取Actor类型 @return @1 string Actor类型

func (*Actor) Next added in v2.2.0

func (a *Actor) Next(ctx Context) error

Next 投递消息到Actor中进行处理 @param ctx Context 消息上下文,写入Actor消息队列串行处理 @return @1 error Actor未启动或消息入队失败时返回的错误

func (*Actor) PID added in v2.2.0

func (a *Actor) PID() string

PID 获取Actor的唯一识别ID 由Kind与ID组合而成,用于全局唯一定位该Actor @return @1 string Actor的唯一识别ID

func (*Actor) Proxy added in v2.2.0

func (a *Actor) Proxy() *Proxy

Proxy 获取代理API @return @1 *Proxy 节点代理

func (*Actor) Push added in v2.2.1

func (a *Actor) Push(uid int64, message *cluster.Message) error

Push 推送消息到本地Node队列上进行处理 @param uid int64 用户ID @param message *cluster.Message 待推送的消息 @return @1 error 打包消息或推送失败时返回的错误

func (*Actor) SetDefaultRouteHandler added in v2.3.0

func (a *Actor) SetDefaultRouteHandler(handler RouteHandler)

SetDefaultRouteHandler 设置默认路由处理器 @param handler RouteHandler 默认路由处理器,所有未注册路由均由其处理

func (*Actor) Spawn added in v2.2.0

func (a *Actor) Spawn(creator Creator, opts ...ActorOption) (*Actor, error)

Spawn 衍生出一个Actor @param creator Creator Actor处理器创建函数 @param opts ...ActorOption Actor配置项 @return @1 *Actor 衍生出的Actor实例 @return @2 error Actor已存在或创建失败时返回的错误

type ActorOption added in v2.2.0

type ActorOption func(o *actorOptions)

ActorOption Actor配置函数

func WithActorArgs added in v2.2.0

func WithActorArgs(args ...any) ActorOption

WithActorArgs 设置传递到Processor中的参数 @param args ...any Processor参数 @return @1 ActorOption Actor配置项

func WithActorID added in v2.2.0

func WithActorID(id string) ActorOption

WithActorID 设置Actor编号 @param id string Actor编号 @return @1 ActorOption Actor配置项

func WithActorKind added in v2.2.4

func WithActorKind(kind string) ActorOption

WithActorKind 设置Actor类型 @param kind string Actor类型 @return @1 ActorOption Actor配置项

func WithActorMessageQueueSize added in v2.6.0

func WithActorMessageQueueSize(size int32) ActorOption

WithActorMessageQueueSize 设置消息队列大小 @param size int32 消息队列大小 @return @1 ActorOption Actor配置项

func WithActorMessageWriteTimeout added in v2.6.0

func WithActorMessageWriteTimeout(timeout time.Duration) ActorOption

WithActorMessageWriteTimeout 设置消息入队超时时间 @param timeout time.Duration 消息入队超时时间 @return @1 ActorOption Actor配置项

func WithActorNonDispatch added in v2.2.4

func WithActorNonDispatch() ActorOption

WithActorNonDispatch 设置Actor不可调度 @return @1 ActorOption Actor配置项

func WithActorNonWait added in v2.2.3

func WithActorNonWait() ActorOption

WithActorNonWait 设置Actor无需等待属性(Node组件关闭时无需等待此Actor结束) @return @1 ActorOption Actor配置项

func WithActorTaskQueueSize added in v2.6.0

func WithActorTaskQueueSize(size int32) ActorOption

WithActorTaskQueueSize 设置任务队列大小 @param size int32 任务队列大小 @return @1 ActorOption Actor配置项

func WithActorTaskWriteTimeout added in v2.6.0

func WithActorTaskWriteTimeout(timeout time.Duration) ActorOption

WithActorTaskWriteTimeout 设置任务入队超时时间 @param timeout time.Duration 任务入队超时时间 @return @1 ActorOption Actor配置项

type BaseProcessor added in v2.2.0

type BaseProcessor struct{}

BaseProcessor 基础处理器 提供所有回调的空实现,可嵌入自定义Processor中避免实现空方法

func (*BaseProcessor) Destroy added in v2.2.0

func (b *BaseProcessor) Destroy()

Destroy 销毁回调

func (*BaseProcessor) Init added in v2.2.0

func (b *BaseProcessor) Init()

Init 初始化回调

func (*BaseProcessor) Start added in v2.2.0

func (b *BaseProcessor) Start()

Start 启动回调

type Context

type Context interface {
	// GID 获取来源网关ID
	// @return @1 string 来源网关ID
	GID() string
	// NID 获取来源节点ID
	// @return @1 string 来源节点ID
	NID() string
	// CID 获取连接ID
	// @return @1 int64 连接ID
	CID() int64
	// UID 获取用户ID
	// @return @1 int64 用户ID
	UID() int64
	// Seq 获取消息序列号
	// @return @1 int32 消息序列号
	Seq() int32
	// Route 获取消息路由号
	// @return @1 int32 消息路由号
	Route() int32
	// Event 获取事件类型
	// @return @1 cluster.Event 事件类型
	Event() cluster.Event
	// Kind 上下文消息类型
	// @return @1 Kind 消息类型(事件或请求)
	Kind() Kind
	// Parse 解析消息
	// 默认反序列化目标对象;存在加密器与来源网关时先解密再反序列化
	// @param v any 解析结果目标对象
	// @return @1 error 解密或反序列化失败时返回的错误
	Parse(v any) error
	// Defer 添加defer延迟调用栈
	// 此方法功能与go defer一致,作用域也仅限于当前handler处理函数内,推荐使用Defer方法替代go defer使用
	// 区别在于使用Defer方法可以对调用栈进行取消操作
	// 同时,在调用Task和Next方法时会自动取消调用栈
	// 也可通过Cancel方法进行手动取消调用栈
	// @param fn func() 待加入调用栈的函数
	// @param bottom ...bool 是否挂载到栈底部,默认挂载到栈顶
	Defer(fn func(), bottom ...bool)
	// Cancel 取消defer调用栈
	Cancel()
	// Clone 克隆Context
	// 克隆出一个新的独立上下文,用于将消息投递给其他Actor
	// @return @1 Context 克隆后的上下文
	Clone() Context
	// Task 投递任务
	// 推荐使用此方法替代task.Add和go func,调用此方法会自动取消Defer调用栈的所有执行函数
	// @param fn func(ctx Context) 待执行的任务函数
	Task(fn func(ctx Context))
	// Proxy 获取代理API
	// @return @1 *Proxy 节点代理
	Proxy() *Proxy
	// Context 获取上下文
	// @return @1 context.Context 底层标准上下文
	Context() context.Context
	// SetValue 为上下文设置值
	// @param key any 键
	// @param val any 值
	SetValue(key, val any)
	// GetValue 获取上下文中的值
	// @param key any 键
	// @return @1 any 键对应的值,不存在时返回nil
	GetValue(key any) any
	// GetIP 获取客户端IP
	// @return @1 string 客户端IP
	// @return @2 error 来源非法或获取失败时返回的错误
	GetIP() (string, error)
	// Deliver 投递消息给节点处理
	// @param args *cluster.DeliverArgs 投递参数
	// @return @1 error 投递失败时返回的错误
	Deliver(args *cluster.DeliverArgs) error
	// Reply 回复消息
	// 根据消息来源(网关、Actor或节点)将消息回传至对应目标
	// @param message *cluster.Message 待回复的消息
	// @return @1 error 回复失败时返回的错误
	Reply(message *cluster.Message) error
	// Response 响应消息
	// 以响应方式回复当前路由消息,路由号与序列号沿用原请求
	// @param message any 响应内容
	// @return @1 error 回复失败时返回的错误
	Response(message any) error
	// Disconnect 关闭来自网关的连接
	// @param force ...bool 是否强制断开,默认非强制
	// @return @1 error 来源非法或断开失败时返回的错误
	Disconnect(force ...bool) error
	// BindGate 绑定网关
	// 未指定uid时使用当前上下文用户的uid
	// @param uid ...int64 待绑定的用户ID
	// @return @1 error 用户ID非法或绑定失败时返回的错误
	BindGate(uid ...int64) error
	// UnbindGate 解绑网关
	// 未指定uid时使用当前上下文用户的uid
	// @param uid ...int64 待解绑的用户ID
	// @return @1 error 用户ID非法或解绑失败时返回的错误
	UnbindGate(uid ...int64) error
	// BindNode 绑定节点
	// 未指定uid时使用当前上下文用户的uid
	// @param uid ...int64 待绑定的用户ID
	// @return @1 error 用户ID非法或绑定失败时返回的错误
	BindNode(uid ...int64) error
	// UnbindNode 解绑节点
	// 未指定uid时使用当前上下文用户的uid
	// @param uid ...int64 待解绑的用户ID
	// @return @1 error 用户ID非法或解绑失败时返回的错误
	UnbindNode(uid ...int64) error
	// Subscribe 订阅频道
	// 指定uids时订阅用户频道,否则订阅当前连接的频道
	// @param channel string 频道名
	// @param uids ...int64 待订阅的用户ID
	// @return @1 error 订阅失败时返回的错误
	Subscribe(channel string, uids ...int64) error
	// Unsubscribe 取消订阅
	// 指定uids时退订用户频道,否则退订当前连接的频道
	// @param channel string 频道名
	// @param uids ...int64 待退订的用户ID
	// @return @1 error 退订失败时返回的错误
	Unsubscribe(channel string, uids ...int64) error
	// BindActor 绑定Actor
	// 将当前用户与指定Actor建立绑定关系
	// @param kind string Actor类型
	// @param id string Actor编号
	// @return @1 error 绑定失败时返回的错误
	BindActor(kind, id string) error
	// UnbindActor 解绑Actor
	// @param kind string Actor类型
	UnbindActor(kind string)
	// Next 消息下放
	// 将当前消息下放到节点的调度器进行二次分发,调用此方法会自动取消Defer调用栈的所有执行函数
	// @return @1 error 分发失败时返回的错误
	Next() error
	// Spawn 衍生出一个新的Actor
	// @param creator Creator Actor处理器创建函数
	// @param opts ...ActorOption Actor配置项
	// @return @1 *Actor 衍生出的Actor实例
	// @return @2 error Actor已存在或创建失败时返回的错误
	Spawn(creator Creator, opts ...ActorOption) (*Actor, error)
	// Kill 杀死存在的一个Actor
	// @param kind string Actor类型
	// @param id string Actor编号
	// @return @1 bool 是否成功杀死
	Kill(kind, id string) bool
	// Actor 获取Actor
	// @param kind string Actor类型
	// @param id string Actor编号
	// @return @1 *Actor Actor实例
	// @return @2 bool Actor是否存在
	Actor(kind, id string) (*Actor, bool)
	// Invoke 调用函数(线程安全)
	// ctx在全局的处理器中,调用的就是proxy.Invoke
	// ctx在Actor的处理器中,调用的就是actor.Invoke
	// @param fn func() 待调用的函数
	// @param isBlock ...bool 是否阻塞调用,默认非阻塞
	// @return @1 error 任务入队失败时返回的错误
	Invoke(fn func(), isBlock ...bool) error
	// AfterFunc 延迟调用,与官方的time.AfterFunc用法一致
	// ctx在全局的处理器中,调用的就是proxy.AfterFunc
	// ctx在Actor的处理器中,调用的就是actor.AfterFunc
	// @param d time.Duration 延迟时长
	// @param f func() 待调用的函数
	// @return @1 *Timer 定时器,可通过Stop取消
	// @return @2 error 创建失败时返回的错误
	AfterFunc(d time.Duration, f func()) (*Timer, error)
	// AfterInvoke 延迟调用(线程安全)
	// 延迟后通过任务队列串行执行函数,保证线程安全
	// ctx在全局的处理器中,调用的就是proxy.AfterInvoke
	// ctx在Actor的处理器中,调用的就是actor.AfterInvoke
	// @param d time.Duration 延迟时长
	// @param f func() 待调用的函数
	// @return @1 *Timer 定时器,可通过Stop取消
	// @return @2 error 创建失败时返回的错误
	AfterInvoke(d time.Duration, f func()) (*Timer, error)
	// NewMeshClient 新建微服务客户端
	// target参数可分为三种模式:
	// 服务直连模式: 	direct://127.0.0.1:8011
	// 服务直连模式: 	direct://711baf8d-8a06-11ef-b7df-f4f19e1f0070
	// 服务发现模式: 	discovery://service_name
	// @param target string 微服务目标地址
	// @return @1 transport.Client 微服务客户端
	// @return @2 error 创建失败时返回的错误
	NewMeshClient(target string) (transport.Client, error)
	// contains filtered or unexported methods
}

Context 消息上下文 封装网关、节点、连接、用户等基础信息,并提供路由处理、消息投递与Actor操作等系列方法

type Creator added in v2.2.0

type Creator func(actor *Actor, args ...any) Processor

Creator Actor处理器创建函数

type EventHandler

type EventHandler func(ctx Context)

EventHandler 事件处理函数

type HookHandler added in v2.1.0

type HookHandler func(proxy *Proxy)

HookHandler 节点钩子处理函数

type Kind added in v2.2.0

type Kind int

Kind 消息类型

const (
	Event   Kind = iota // 事件
	Request             // 请求
)

type Middleware

type Middleware struct {
	// contains filtered or unexported fields
}

Middleware 中间件 用于在路由处理器执行前后进行统一拦截处理

func (*Middleware) Next

func (m *Middleware) Next(ctx Context)

Next 下一个中间件 @param ctx Context 消息上下文

func (*Middleware) Skip

func (m *Middleware) Skip(ctx Context, skip int)

Skip 跳过N个中间件 依次执行后续中间件,耗尽后执行最终的路由处理器 @param ctx Context 消息上下文 @param skip int 跳过的中间件数量

type MiddlewareHandler

type MiddlewareHandler func(middleware *Middleware, ctx Context)

MiddlewareHandler 中间件处理函数

type Node

type Node struct {
	component.Base
	// contains filtered or unexported fields
}

Node 节点服务器

func NewNode

func NewNode(opts ...Option) *Node

NewNode 创建节点服务器 创建后会初始化代理、任务器、路由器、事件触发器与调度器等内部组件,并处于关闭状态 @param opts ...Option 节点配置项 @return @1 *Node 节点服务器实例

func (*Node) Close added in v2.2.3

func (n *Node) Close()

Close 关闭节点 将状态置为挂起,停止接收新消息并等待任务、路由与事件队列中的存量消息处理完成

func (*Node) Destroy

func (n *Node) Destroy()

Destroy 销毁节点服务器 将状态置为关闭,解注册服务实例、停止连接与传输服务器并释放内部组件资源

func (*Node) Init

func (n *Node) Init()

Init 初始化节点 校验实例ID、实例名称、编解码器、定位器与注册器等必要配置,缺失时直接终止进程

func (*Node) Name

func (n *Node) Name() string

Name 组件名称 @return @1 string 组件名称

func (*Node) Proxy

func (n *Node) Proxy() *Proxy

Proxy 获取节点代理 @return @1 *Proxy 节点代理

func (*Node) Start

func (n *Node) Start()

Start 启动节点 将状态置为工作中,随后依次启动连接服务器、传输服务器、注册服务实例并开启消息分发

type Option

type Option func(o *options)

Option 节点配置函数

func WithAddr added in v2.1.0

func WithAddr(addr string) Option

WithAddr 设置监听地址 @param addr string 监听地址 @return @1 Option 节点配置项

func WithCodec

func WithCodec(codec encoding.Codec) Option

WithCodec 设置编解码器 @param codec encoding.Codec 编解码器 @return @1 Option 节点配置项

func WithContext

func WithContext(ctx context.Context) Option

WithContext 设置启动上下文 @param ctx context.Context 启动上下文 @return @1 Option 节点配置项

func WithContextFunc added in v2.6.0

func WithContextFunc(ctxFunc func() context.Context) Option

WithContextFunc 设置自定义上下文生成器 @param ctxFunc func() context.Context 上下文生成器 @return @1 Option 节点配置项

func WithEncryptor

func WithEncryptor(encryptor crypto.Encryptor) Option

WithEncryptor 设置消息加密器 @param encryptor crypto.Encryptor 消息加密器 @return @1 Option 节点配置项

func WithExpose added in v2.3.0

func WithExpose(expose bool) Option

WithExpose 设置是否将内部通信地址暴露到公网 @param expose bool 是否暴露内部通信地址到公网 @return @1 Option 节点配置项

func WithID

func WithID(id string) Option

WithID 设置实例ID @param id string 实例ID @return @1 Option 节点配置项

func WithLinkerCallTimeout added in v2.6.0

func WithLinkerCallTimeout(callTimeout time.Duration) Option

WithLinkerCallTimeout 设置RPC调用超时时间 @param callTimeout time.Duration 内部RPC调用超时时间 @return @1 Option 节点配置项

func WithLinkerCommandQueueSize added in v2.6.0

func WithLinkerCommandQueueSize(commandQueueSize int32) Option

WithLinkerCommandQueueSize 设置消息队列大小 @param commandQueueSize int32 消息队列大小 @return @1 Option 节点配置项

func WithLinkerCommandWriteTimeout added in v2.6.0

func WithLinkerCommandWriteTimeout(commandWriteTimeout time.Duration) Option

WithLinkerCommandWriteTimeout 设置写入超时时间 @param commandWriteTimeout time.Duration 消息写入超时时间 @return @1 Option 节点配置项

func WithLinkerConnNum added in v2.6.0

func WithLinkerConnNum(connNum int) Option

WithLinkerConnNum 设置连接数 @param connNum int 内部RPC拨号连接数 @return @1 Option 节点配置项

func WithLinkerDialRetryTimes added in v2.6.0

func WithLinkerDialRetryTimes(dialRetryTimes int) Option

WithLinkerDialRetryTimes 设置内部RPC拨号重试次数 @param dialRetryTimes int 内部RPC拨号重试次数 @return @1 Option 节点配置项

func WithLinkerDialTimeout added in v2.6.0

func WithLinkerDialTimeout(dialTimeout time.Duration) Option

WithLinkerDialTimeout 设置内部RPC拨号超时时间 @param dialTimeout time.Duration 内部RPC拨号超时时间 @return @1 Option 节点配置项

func WithLinkerFaultRecoveryTime added in v2.6.0

func WithLinkerFaultRecoveryTime(faultRecoveryTime time.Duration) Option

WithLinkerFaultRecoveryTime 设置内部RPC故障恢复时间 @param faultRecoveryTime time.Duration 内部RPC故障恢复时间 @return @1 Option 节点配置项

func WithLocator

func WithLocator(locator locate.Locator) Option

WithLocator 设置定位器 @param locator locate.Locator 用户定位器 @return @1 Option 节点配置项

func WithMessageQueueSize added in v2.6.0

func WithMessageQueueSize(messageQueueSize int32) Option

WithMessageQueueSize 设置消息队列大小 @param messageQueueSize int32 消息队列大小 @return @1 Option 节点配置项

func WithMessageWriteTimeout added in v2.6.0

func WithMessageWriteTimeout(messageWriteTimeout time.Duration) Option

WithMessageWriteTimeout 设置写入超时时间 @param messageWriteTimeout time.Duration 消息写入超时时间 @return @1 Option 节点配置项

func WithMetadata added in v2.3.0

func WithMetadata(metadata map[string]string) Option

WithMetadata 设置元数据 @param metadata map[string]string 元数据 @return @1 Option 节点配置项

func WithName

func WithName(name string) Option

WithName 设置实例名称 @param name string 实例名称 @return @1 Option 节点配置项

func WithRegistry

func WithRegistry(r registry.Registry) Option

WithRegistry 设置服务注册器 @param r registry.Registry 服务注册器 @return @1 Option 节点配置项

func WithTaskQueueSize added in v2.6.0

func WithTaskQueueSize(taskQueueSize int32) Option

WithTaskQueueSize 设置任务队列大小 @param taskQueueSize int32 任务队列大小 @return @1 Option 节点配置项

func WithTaskWriteTimeout added in v2.6.0

func WithTaskWriteTimeout(taskWriteTimeout time.Duration) Option

WithTaskWriteTimeout 设置任务超时时间 @param taskWriteTimeout time.Duration 任务写入超时时间 @return @1 Option 节点配置项

func WithTransporter

func WithTransporter(transporter transport.Transporter) Option

WithTransporter 设置消息传输器 @param transporter transport.Transporter 消息传输器 @return @1 Option 节点配置项

func WithWeight added in v2.2.4

func WithWeight(weight int) Option

WithWeight 设置权重 @param weight int 服务器权重 @return @1 Option 节点配置项

type Processor added in v2.2.0

type Processor interface {
	// Init 初始化回调
	Init()
	// Start 启动回调
	Start()
	// Destroy 销毁回调
	Destroy()
}

Processor Actor处理器接口 定义Actor生命周期中的初始化、启动与销毁回调

type Proxy

type Proxy struct {
	// contains filtered or unexported fields
}

Proxy 代理 提供节点服务器对外可见的完整功能API,包括网关/节点链接、路由、事件、消息推送与Actor管理等

func (*Proxy) Actor added in v2.2.0

func (p *Proxy) Actor(kind, id string) (*Actor, bool)

Actor 获取Actor @param kind string Actor类型 @param id string Actor编号 @return @1 *Actor Actor实例 @return @2 bool Actor是否存在

func (*Proxy) AddEventHandler added in v2.1.0

func (p *Proxy) AddEventHandler(event cluster.Event, handler EventHandler)

AddEventHandler 添加事件处理器 @param event cluster.Event 事件类型 @param handler EventHandler 事件处理函数

func (*Proxy) AddHookListener added in v2.1.0

func (p *Proxy) AddHookListener(hook cluster.Hook, handler HookHandler)

AddHookListener 添加钩子监听器 @param hook cluster.Hook 钩子类型 @param handler HookHandler 钩子处理函数

func (*Proxy) AddRouteHandler added in v2.1.0

func (p *Proxy) AddRouteHandler(route int32, handler RouteHandler, opts ...RouteOptions)

AddRouteHandler 添加路由处理器 @param route int32 路由号 @param handler RouteHandler 路由处理函数 @param opts ...RouteOptions 路由选项

func (*Proxy) AddServiceProvider added in v2.2.0

func (p *Proxy) AddServiceProvider(name string, desc, provider any)

AddServiceProvider 添加服务提供者 @param name string 服务名称 @param desc any 服务描述对象 @param provider any 服务提供者

func (*Proxy) AfterFunc added in v2.2.4

func (p *Proxy) AfterFunc(d time.Duration, f func()) (*Timer, error)

AfterFunc 延迟调用,与官方的time.AfterFunc用法一致 @param d time.Duration 延迟时长 @param f func() 待调用的函数 @return @1 *Timer 定时器,可通过Stop取消 @return @2 error 节点关闭时返回的错误

func (*Proxy) AfterInvoke added in v2.2.4

func (p *Proxy) AfterInvoke(d time.Duration, f func()) (*Timer, error)

AfterInvoke 延迟调用(线程安全) 延迟后通过任务队列串行执行函数,保证线程安全 @param d time.Duration 延迟时长 @param f func() 待调用的函数 @return @1 *Timer 定时器,可通过Stop取消 @return @2 error 节点关闭时返回的错误

func (*Proxy) AskGate

func (p *Proxy) AskGate(ctx context.Context, gid string, uid int64) (string, bool, error)

AskGate 检测用户是否在给定的网关上 @param ctx context.Context 上下文 @param gid string 网关ID @param uid int64 用户ID @return @1 string 用户实际所在的网关ID @return @2 bool 用户是否在给定的网关上 @return @3 error 节点关闭时返回的错误

func (*Proxy) AskNode

func (p *Proxy) AskNode(ctx context.Context, uid int64, name, nid string) (string, bool, error)

AskNode 检测用户是否在给定的节点上 @param ctx context.Context 上下文 @param uid int64 用户ID @param name string 节点名称 @param nid string 节点ID @return @1 string 用户实际所在的节点ID @return @2 bool 用户是否在给定的节点上 @return @3 error 节点关闭时返回的错误

func (*Proxy) BindActor added in v2.2.0

func (p *Proxy) BindActor(uid int64, kind, id string) error

BindActor 绑定Actor @param uid int64 用户ID @param kind string Actor类型 @param id string Actor编号 @return @1 error 节点关闭或绑定失败时返回的错误

func (*Proxy) BindGate

func (p *Proxy) BindGate(ctx context.Context, gid string, cid, uid int64) error

BindGate 绑定网关 @param ctx context.Context 上下文 @param gid string 网关ID @param cid int64 连接ID @param uid int64 用户ID,绑定后用户与该连接关联 @return @1 error 节点关闭或绑定失败时返回的错误

func (*Proxy) BindNode

func (p *Proxy) BindNode(ctx context.Context, uid int64, nameAndNID ...string) error

BindNode 绑定节点 单个用户可以绑定到多个节点服务器上,相同名称的节点服务器只能绑定一个,多次绑定会到相同名称的节点服务器会覆盖之前的绑定。 绑定操作会通过发布订阅方式同步到网关服务器和其他相关节点服务器上。 @param ctx context.Context 上下文 @param uid int64 用户ID @param nameAndNID ...string 名称与节点ID对;缺省时使用当前节点 @return @1 error 节点关闭或绑定失败时返回的错误

func (*Proxy) Broadcast

func (p *Proxy) Broadcast(ctx context.Context, args *cluster.BroadcastArgs) (int64, error)

Broadcast 推送广播消息 要想获得推送成功的目标数,需将args.Ack设为true @param ctx context.Context 上下文 @param args *cluster.BroadcastArgs 广播参数 @return @1 int64 广播成功的目标数 @return @2 error 节点关闭或推送失败时返回的错误

func (*Proxy) Deliver

func (p *Proxy) Deliver(ctx context.Context, args *cluster.DeliverArgs) error

Deliver 投递消息给节点处理 @param ctx context.Context 上下文 @param args *cluster.DeliverArgs 投递参数 @return @1 error 节点关闭、目标为当前节点或投递失败时返回的错误

func (*Proxy) Disconnect

func (p *Proxy) Disconnect(ctx context.Context, args *cluster.DisconnectArgs) error

Disconnect 断开连接 @param ctx context.Context 上下文 @param args *cluster.DisconnectArgs 断开参数 @return @1 error 节点关闭或断开失败时返回的错误

func (*Proxy) FetchGateList

func (p *Proxy) FetchGateList(ctx context.Context, states ...cluster.State) ([]*registry.ServiceInstance, error)

FetchGateList 拉取网关列表 @param ctx context.Context 上下文 @param states ...cluster.State 状态过滤条件 @return @1 []*registry.ServiceInstance 网关服务实例列表 @return @2 error 节点关闭时返回的错误

func (*Proxy) FetchNodeList

func (p *Proxy) FetchNodeList(ctx context.Context, states ...cluster.State) ([]*registry.ServiceInstance, error)

FetchNodeList 拉取节点列表 @param ctx context.Context 上下文 @param states ...cluster.State 状态过滤条件 @return @1 []*registry.ServiceInstance 节点服务实例列表 @return @2 error 节点关闭时返回的错误

func (*Proxy) GetID added in v2.1.0

func (p *Proxy) GetID() string

GetID 获取当前节点ID @return @1 string 当前节点ID

func (*Proxy) GetIP

func (p *Proxy) GetIP(ctx context.Context, args *cluster.GetIPArgs) (string, error)

GetIP 获取客户端IP @param ctx context.Context 上下文 @param args *cluster.GetIPArgs 查询参数 @return @1 string 客户端IP @return @2 error 节点关闭或查询失败时返回的错误

func (*Proxy) GetName added in v2.1.0

func (p *Proxy) GetName() string

GetName 获取当前节点名称 @return @1 string 当前节点名称

func (*Proxy) GetState added in v2.1.0

func (p *Proxy) GetState() cluster.State

GetState 获取当前节点状态 @return @1 cluster.State 当前节点状态

func (*Proxy) HasGate added in v2.2.0

func (p *Proxy) HasGate(gid string) (bool, error)

HasGate 检测是否存在某个网关 @param gid string 网关ID @return @1 bool 网关是否存在 @return @2 error 节点关闭时返回的错误

func (*Proxy) HasNode added in v2.2.0

func (p *Proxy) HasNode(nid string) bool

HasNode 检测是否存在某个节点 @param nid string 节点ID @return @1 bool 节点是否存在

func (*Proxy) Invoke

func (p *Proxy) Invoke(f func(), wait ...bool) error

Invoke 调用函数(线程安全) @param f func() 待调用的函数 @param wait ...bool 是否等待调用完成,默认不等待 @return @1 error 节点关闭或任务入队失败时返回的错误

func (*Proxy) IsOnline added in v2.1.0

func (p *Proxy) IsOnline(ctx context.Context, args *cluster.IsOnlineArgs) (bool, error)

IsOnline 检测是否在线 @param ctx context.Context 上下文 @param args *cluster.IsOnlineArgs 查询参数 @return @1 bool 是否在线 @return @2 error 节点关闭或查询失败时返回的错误

func (*Proxy) Kill added in v2.2.0

func (p *Proxy) Kill(kind, id string) bool

Kill 杀死存在的一个Actor @param kind string Actor类型 @param id string Actor编号 @return @1 bool 是否成功杀死,节点关闭或Actor不存在时返回false

func (*Proxy) LocateGate

func (p *Proxy) LocateGate(ctx context.Context, uid int64) (string, error)

LocateGate 定位用户所在网关 @param ctx context.Context 上下文 @param uid int64 用户ID @return @1 string 用户所在的网关ID @return @2 error 节点关闭时返回的错误

func (*Proxy) LocateNode

func (p *Proxy) LocateNode(ctx context.Context, uid int64, name string) (string, error)

LocateNode 定位用户所在节点 @param ctx context.Context 上下文 @param uid int64 用户ID @param name string 节点名称 @return @1 string 用户所在的节点ID @return @2 error 节点关闭时返回的错误

func (*Proxy) LocateNodes added in v2.5.0

func (p *Proxy) LocateNodes(ctx context.Context, uid int64) (map[string]string, error)

LocateNodes 定位用户所在节点列表 @param ctx context.Context 上下文 @param uid int64 用户ID @return @1 map[string]string 用户绑定的节点名称到节点ID的映射 @return @2 error 节点关闭时返回的错误

func (*Proxy) Multicast

func (p *Proxy) Multicast(ctx context.Context, args *cluster.MulticastArgs) (int64, error)

Multicast 推送组播消息 要想获得推送成功的目标数,需将args.Ack设为true @param ctx context.Context 上下文 @param args *cluster.MulticastArgs 组播参数 @return @1 int64 组播成功的目标数 @return @2 error 节点关闭或推送失败时返回的错误

func (*Proxy) NewMeshClient added in v2.1.0

func (p *Proxy) NewMeshClient(target string) (transport.Client, error)

NewMeshClient 新建微服务客户端 target参数可分为三种模式: 服务直连模式: direct://127.0.0.1:8011 服务直连模式: direct://711baf8d-8a06-11ef-b7df-f4f19e1f0070 服务发现模式: discovery://service_name @param target string 微服务目标地址 @return @1 transport.Client 微服务客户端 @return @2 error 节点关闭或未配置消息传输器时返回的错误

func (*Proxy) PackBuffer added in v2.2.0

func (p *Proxy) PackBuffer(message any) ([]byte, error)

PackBuffer 打包Buffer @param message any 待打包的消息内容 @return @1 []byte 打包后的消息字节 @return @2 error 打包失败时返回的错误

func (*Proxy) PackMessage added in v2.2.0

func (p *Proxy) PackMessage(message *cluster.Message) ([]byte, error)

PackMessage 打包消息 @param message *cluster.Message 待打包的消息 @return @1 []byte 打包后的消息字节 @return @2 error 打包失败时返回的错误

func (*Proxy) Publish added in v2.3.0

func (p *Proxy) Publish(ctx context.Context, args *cluster.PublishArgs) (int64, error)

Publish 发布消息 要想获得推送成功的目标数,需将args.Ack设为true @param ctx context.Context 上下文 @param args *cluster.PublishArgs 发布参数 @return @1 int64 发布成功的目标数 @return @2 error 节点关闭或发布失败时返回的错误

func (*Proxy) Push

func (p *Proxy) Push(ctx context.Context, args *cluster.PushArgs) error

Push 推送消息 args.Ack设为true时可获得消息真实发送的情况 @param ctx context.Context 上下文 @param args *cluster.PushArgs 推送参数 @return @1 error 节点关闭或推送失败时返回的错误

func (*Proxy) RouteGroup added in v2.1.0

func (p *Proxy) RouteGroup(groups ...func(group *RouterGroup)) *RouterGroup

RouteGroup 路由组 @param groups ...func(group *RouterGroup) 路由组配置函数 @return @1 *RouterGroup 路由组

func (*Proxy) Router

func (p *Proxy) Router() *Router

Router 路由器 @return @1 *Router 路由器

func (*Proxy) SetDefaultRouteHandler added in v2.1.0

func (p *Proxy) SetDefaultRouteHandler(handler RouteHandler)

SetDefaultRouteHandler 设置默认路由处理器,所有未注册的路由均走默认路由处理器 @param handler RouteHandler 默认路由处理函数

func (*Proxy) SetState added in v2.1.0

func (p *Proxy) SetState(state cluster.State) error

SetState 设置当前节点状态 @param state cluster.State 目标状态 @return @1 error 状态设置失败时返回的错误

func (*Proxy) Spawn added in v2.2.0

func (p *Proxy) Spawn(creator Creator, opts ...ActorOption) (*Actor, error)

Spawn 衍生出一个新的Actor @param creator Creator Actor处理器创建函数 @param opts ...ActorOption Actor配置项 @return @1 *Actor 衍生出的Actor实例 @return @2 error 节点关闭或创建失败时返回的错误

func (*Proxy) Stat

func (p *Proxy) Stat(ctx context.Context, kind session.Kind) (int64, error)

Stat 统计会话总数 @param ctx context.Context 上下文 @param kind session.Kind 会话类型 @return @1 int64 会话总数 @return @2 error 节点关闭时返回的错误

func (*Proxy) Subscribe added in v2.3.0

func (p *Proxy) Subscribe(ctx context.Context, args *cluster.SubscribeArgs) error

Subscribe 订阅频道 @param ctx context.Context 上下文 @param args *cluster.SubscribeArgs 订阅参数 @return @1 error 节点关闭或订阅失败时返回的错误

func (*Proxy) Trigger added in v2.1.0

func (p *Proxy) Trigger() *Trigger

Trigger 事件触发器 @return @1 *Trigger 事件触发器

func (*Proxy) UnbindActor added in v2.2.0

func (p *Proxy) UnbindActor(uid int64, kind string) error

UnbindActor 解绑Actor @param uid int64 用户ID @param kind string Actor类型 @return @1 error 节点关闭或解绑失败时返回的错误

func (*Proxy) UnbindGate

func (p *Proxy) UnbindGate(ctx context.Context, uid int64) error

UnbindGate 解绑网关 @param ctx context.Context 上下文 @param uid int64 用户ID @return @1 error 节点关闭或解绑失败时返回的错误

func (*Proxy) UnbindNode

func (p *Proxy) UnbindNode(ctx context.Context, uid int64, nameAndNID ...string) error

UnbindNode 解绑节点 解绑时会对对应名称的节点服务器进行解绑,解绑时会对解绑节点ID进行校验,不匹配则解绑失败。 解绑操作会通过发布订阅方式同步到网关服务器和其他相关节点服务器上。 @param ctx context.Context 上下文 @param uid int64 用户ID @param nameAndNID ...string 名称与节点ID对;缺省时使用当前节点 @return @1 error 节点关闭或解绑失败时返回的错误

func (*Proxy) Unsubscribe added in v2.3.0

func (p *Proxy) Unsubscribe(ctx context.Context, args *cluster.UnsubscribeArgs) error

Unsubscribe 取消订阅频道 @param ctx context.Context 上下文 @param args *cluster.UnsubscribeArgs 取消订阅参数 @return @1 error 节点关闭或取消订阅失败时返回的错误

type RouteHandler

type RouteHandler func(ctx Context)

RouteHandler 路由处理器

type RouteOptions added in v2.1.0

type RouteOptions struct {
	// 是否内部的路由,默认非内部
	// 外部路由可在客户端、网关、节点间进行消息流转
	// 内部路由仅限于在节点间进行消息流转
	Internal bool

	// 是否有状态路由,默认无状态
	// 无状态路由消息会根据负载均衡策略分配到不同的节点服务器进行处理
	// 有状态路由消息会在绑定节点服务器后,固定路由到绑定的节点服务器进行处理
	Stateful bool

	// 是否授权路由,默认非授权
	// 授权路由在集群间流转时必需附带UID信息,否则无法进行路由投递
	// 该参数可在网关层对未授权连接进行提前拦截,降低节点服对于攻击处理的压力
	Authorized bool

	// 路由中间件
	Middlewares []MiddlewareHandler
}

RouteOptions 路由选项

type Router

type Router struct {
	// contains filtered or unexported fields
}

Router 路由器 承载节点内的路由队列,负责路由消息的接收与处理

func (*Router) AddRouteHandler

func (r *Router) AddRouteHandler(route int32, handler RouteHandler, opts ...RouteOptions)

AddRouteHandler 添加路由处理器 @param route int32 路由号 @param handler RouteHandler 路由处理函数 @param opts ...RouteOptions 路由选项

func (*Router) CheckRouteStateful

func (r *Router) CheckRouteStateful(route int32) (stateful bool, exist bool)

CheckRouteStateful 是否为有状态路由 @param route int32 路由号 @return @1 bool 是否是有状态路由 @return @2 bool 路由是否存在

func (*Router) Group

func (r *Router) Group(groups ...func(group *RouterGroup)) *RouterGroup

Group 路由组 @param groups ...func(group *RouterGroup) 路由组配置函数 @return @1 *RouterGroup 路由组

func (*Router) HasDefaultRouteHandler

func (r *Router) HasDefaultRouteHandler() bool

HasDefaultRouteHandler 是否存在默认路由处理器 @return @1 bool 是否存在默认路由处理器

func (*Router) SetDefaultRouteHandler

func (r *Router) SetDefaultRouteHandler(handler RouteHandler)

SetDefaultRouteHandler 设置默认路由处理器,所有未注册的路由均走默认路由处理器 @param handler RouteHandler 默认路由处理函数

func (*Router) SetPostRouteHandler added in v2.2.6

func (r *Router) SetPostRouteHandler(handler RouteHandler)

SetPostRouteHandler 设置后置路由处理器 @param handler RouteHandler 后置路由处理函数

func (*Router) SetPreRouteHandler added in v2.2.6

func (r *Router) SetPreRouteHandler(handler RouteHandler)

SetPreRouteHandler 设置前置路由处理器 @param handler RouteHandler 前置路由处理函数

type RouterGroup

type RouterGroup struct {
	// contains filtered or unexported fields
}

RouterGroup 路由组 用于集中管理一组路由及其中间件

func (*RouterGroup) AddRouteHandler

func (g *RouterGroup) AddRouteHandler(route int32, handler RouteHandler, opts ...RouteOptions) *RouterGroup

AddRouteHandler 添加路由处理器 会合并路由组中间件与指定路由自身的中间件 @param route int32 路由号 @param handler RouteHandler 路由处理函数 @param opts ...RouteOptions 路由选项 @return @1 *RouterGroup 路由组(支持链式调用)

func (*RouterGroup) Middleware

func (g *RouterGroup) Middleware(middlewares ...MiddlewareHandler) *RouterGroup

Middleware 添加中间件 @param middlewares ...MiddlewareHandler 中间件处理函数 @return @1 *RouterGroup 路由组(支持链式调用)

type Scheduler added in v2.2.0

type Scheduler struct {
	// contains filtered or unexported fields
}

Scheduler 调度器 负责Actor的创建、销毁以及用户与Actor关系的维护和消息分发

type Timer added in v2.2.4

type Timer struct {
	// contains filtered or unexported fields
}

Timer 定时器

func (*Timer) Stop added in v2.2.4

func (t *Timer) Stop() (ok bool)

Stop 停止定时器 停止成功且节点非空时递减节点等待计数 @return @1 bool 是否成功停止定时器

type Trigger added in v2.1.0

type Trigger struct {
	// contains filtered or unexported fields
}

Trigger 事件触发器

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL