actor

package
v0.0.0-...-80248e1 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2025 License: MIT Imports: 11 Imported by: 0

README

actor 模型

  • 创建一个 actor 模型
  • 每个 actor 有自己的邮箱,处理消息

done

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Actor

type Actor[KEY comparable] struct {
	Statistics *xstatistics.Statistics // 统计数据
	// contains filtered or unexported fields
}

Actor 表示一个 actor 实例

func NewActor

func NewActor[KEY comparable](key KEY, parent *Actor[KEY], behavior Behavior) *Actor[KEY]

NewActor 创建一个新的 actor

func (*Actor[KEY]) GetKey

func (p *Actor[KEY]) GetKey() KEY

func (*Actor[KEY]) GetParent

func (p *Actor[KEY]) GetParent() *Actor[KEY]

GetParent 获取父 actor

func (*Actor[KEY]) Send

func (p *Actor[KEY]) Send(messages ...any)

Send 发送消息到 actor (异步)

func (*Actor[KEY]) SendMsg

func (p *Actor[KEY]) SendMsg(messages ...*Msg)

SendMsg 发送消息到 actor (异步)

func (*Actor[KEY]) SendMsgAsync

func (p *Actor[KEY]) SendMsgAsync(msg *Msg) (resp any, err error)

SendMsgAsync 发送消息到 actor, 并等待响应 (同步)

参数:
	msg: 行为-消息
		Ctx: 上下文, 可用于设置超时, 如果 ctx 没有设置超时, 则使用默认 60 秒超时
返回:
	resp: 响应数据
	err: 错误

func (*Actor[KEY]) Start

func (p *Actor[KEY]) Start()

Start 启动actor

type Behavior

type Behavior func(messages ...any) (behavior Behavior, resp any, err error)

Behavior 定义 actor 的行为函数类型

每个行为函数接收一个消息,处理它,然后返回下一个行为

func EmptyBehavior

func EmptyBehavior(messages ...any) (behavior Behavior, resp any, err error)

EmptyBehavior 空行为,忽略所有消息

type CMD

type CMD uint32
const (
	// 用户自定义命令范围: [0,4294959999]
	CustomCommand_Begin CMD = 0          // 用户自定义命令-起始值
	CustomCommand_End   CMD = 4294959999 // 用户自定义命令-结束值
	// 系统保留命令范围: [4294960000,4294967295]
	SystemReservedCommand_Begin       CMD = 4294960000     // 系统保留命令-起始值
	SystemReservedCommand_Stop        CMD = 4294960001     // 停止 {args:无}
	SystemReservedCommand_RemoveChild CMD = 4294960002     // 移除子 actor {args: [0]:子 actor key}
	SystemReservedCommand_Spawn       CMD = 4294960003     // 创建子 actor {args: [0]:子 actor key, [1]:子 actor 行为函数} {返回: response *Actor[KEY]}
	SystemReservedCommand_GetChild    CMD = 4294960004     // 获取子 actor {args: [0]:子 actor key} {返回: response *Actor[KEY]}
	SystemReservedCommand_End         CMD = math.MaxUint32 // 系统保留命令-结束值
)

type IActor

type IActor[Key comparable] interface {
	GetKey() Key
}

type IActorMsg

type IActorMsg interface {
	SendMsg(msg ...*Msg)
	SendMsgAsync(msg *Msg) (resp any, err error)
}

type Msg

type Msg struct {
	Ctx  context.Context // 上下文, 可用于取消或设置超时
	Cmd  CMD             // 消息命令
	Args []any           // 消息参数
	// contains filtered or unexported fields
}

func NewMsg

func NewMsg(ctx context.Context, cmd CMD, args ...any) *Msg

NewMsg 创建新的 Msg

Ctx: 上下文, Cmd: 命令, Args: 参数

func (*Msg) IsSync

func (p *Msg) IsSync() bool

IsSync 是否同步

如果事件有响应通道, 则认为是同步调用

Jump to

Keyboard shortcuts

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