actorgo

package module
v1.0.20 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2026 License: MIT Imports: 15 Imported by: 0

README

ActorGo

ActorGo 是 Go Actor 游戏服务端框架。当前协议为 AGP/1:Packet / ClusterMessage 固定 Protobuf,业务 Body 支持 JSON/PB;客户端交互只有 Request、Response、Notify。

更完整的近期变更说明见:_docs/api-changes-2026-07.md。

本次改造要点

  • 删除 Pomelo/Simple 运行时与 INetParser
  • 稳定 Method ID + Typed Handler,取消 FuncName 反射调用
  • TCP:uint32 big-endian length + Packet PB
  • WebSocket:一条 Binary Message 对应一个 Packet,子协议 agp.v1
  • AGP / HTTP:客户端只提交 Method ID 和 Body,不接受 Actor Target
  • NATS:Protobuf ClusterMessage(session 使用 Session,无 SessionSnapshot)
  • AGP / HTTP / NATS 顶层调用共用 Actor 方法表;内部子 Actor 使用 ActorPath
  • 单一 mailbox;底层 Post 不再暴露为业务 API
  • Application 编解码收敛为 BodyCodecs() + SetDefaultBodyCodec
  • 旧 Call / CallWait / CallType 已由 Invoke / Notify 取代

不包含 Pomelo 兼容、gRPC 和 MessagePack。

Method 定义

业务 proto 使用标准 Protobuf:

service PlayerService {
  rpc Login(LoginRequest) returns (LoginResponse);
}
protoc -I . \
  --go_out=. --go_opt=paths=source_relative \
  api/player.proto

Actor 初始化时直接注册(无 error 返回值):

const LoginMethodID uint32 = 1001

func (a *PlayerActor) OnInit() {
    a.Methods().Register(LoginMethodID, a.Login)
}

func (a *PlayerActor) Login(
    ctx *facade.RequestContext,
    request *playerv1.LoginRequest,
) (*playerv1.LoginResponse, error) {
    return &playerv1.LoginResponse{}, nil
}
  • Request:func(*RequestContext, *Request) (*Response, error)
  • Notify:func(*RequestContext, *Request) error
  • 子 Actor 的 Register 只安装本地 mailbox,不写入外部方法表

挂载 httpactor 后,所有顶层 Actor 的 Methods().Register 方法统一经 POST /actor/{methodID} 暴露。

启动

app := actorgo.Configure(profileFilePath, nodeID, actorgo.Standalone)

// 可选:切换默认 Body Codec(JSON 与 PB 始终同时注册)
app.SetDefaultBodyCodec(cfacade.CodecJSON)

app.AddActors(playerActor)

app.Register(parser.New("client", []facade.IConnector{
    connector.NewTCP(":9000"),
    connector.NewWS(":9001"),
}))
app.Register(httpactor.NewComponent("actor-api", "127.0.0.1:9080"))
app.Startup()

跨节点调用

ctx := cfacade.NewRequestContext(context.Background())
ctx.Codec = cfacade.CodecProtobuf
// 需要玩家上下文时再设置 ctx.Session
result := app.ActorSystem().InvokeNode(ctx, "center-1", methodID, req)

本节点顶层方法使用 Invoke(ctx, methodID, req);跨节点顶层方法使用 InvokeNode(ctx, nodeID, methodID, req)。完整 ActorPath 仅用于服务端内部的 InvokeTarget / NotifyTarget,客户端 AGP/HTTP 都不能指定 target。

curl JSON

curl -X POST "http://127.0.0.1:9080/actor/1001" \
  -H "Content-Type: application/json" \
  -H "X-ActorGo-Timeout-Ms: 3000" \
  -d '{"account":"demo"}'

PB 调用将 Content-Type 改为 application/x-protobuf,并使用 --data-binary @request.pb。

Notify 方法成功返回 HTTP 202;错误 Body 为 HTTPError。完整 Header/状态码见 HTTP 文档。

与现有 Gin 共存

actorHandler := httpactor.NewHandler(app)
if err := httpServer.RegisterActorRoutes(actorHandler); err != nil {
    panic(err)
}

文档与验证

go test ./net/proto ./net/serializer ./net/method \
  ./net/parser ./net/httpactor ./net/actor ./net/connector

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AppBuilder

type AppBuilder struct {
	*Application
	// contains filtered or unexported fields
}

AppBuilder collects components and top-level Actor handlers before the application lifecycle starts.

func Configure

func Configure(profileFilePath, nodeIDStr string, mode NodeMode) *AppBuilder

Configure loads the requested node from a profile and returns its builder.

func ConfigureNode

func ConfigureNode(node cfacade.INode, mode NodeMode) *AppBuilder

ConfigureNode builds an application from a programmatically supplied node.

func (*AppBuilder) AddActors

func (p *AppBuilder) AddActors(actors ...cfacade.IActorHandler)

AddActors queues top-level Actor handlers for creation before listeners start.

func (*AppBuilder) Register

func (p *AppBuilder) Register(component ...cfacade.IComponent)

Register queues components for registration during Startup.

func (*AppBuilder) Startup

func (p *AppBuilder) Startup()

Startup installs the built-in cluster components when required, registers user components, and then enters the blocking application lifecycle.

type Application

type Application struct {
	cfacade.INode
	// contains filtered or unexported fields
}

Application owns the node configuration and coordinates codecs, components, Actor routing, discovery, and cluster lifecycle.

func NewApp

func NewApp(profileFilePath, nodeIDStr string, mode NodeMode) *Application

NewApp create new application instance It resolves the node entry from the supplied profile before constructing it.

func NewAppNode

func NewAppNode(node cfacade.INode, mode NodeMode) *Application

NewAppNode creates an application from an already resolved node definition.

func (*Application) ActorSystem

func (a *Application) ActorSystem() cfacade.IActorSystem

func (*Application) All

func (a *Application) All() []cfacade.IComponent

func (*Application) BodyCodecs added in v1.0.20

func (a *Application) BodyCodecs() cfacade.IBodyCodecRegistry

BodyCodecs returns the shared allow-list used by AGP, HTTP, and cluster calls.

func (*Application) Cluster

func (a *Application) Cluster() cfacade.ICluster

func (*Application) DieChan

func (a *Application) DieChan() chan bool

func (*Application) Discovery

func (a *Application) Discovery() cfacade.IDiscovery

func (*Application) Find

func (a *Application) Find(name string) cfacade.IComponent

func (*Application) Methods added in v1.0.20

func (a *Application) Methods() cfacade.IMethodTable

Methods returns the MethodID-to-Actor route table populated during Actor initialization.

func (*Application) NodeMode

func (a *Application) NodeMode() NodeMode

func (*Application) OnShutdown

func (a *Application) OnShutdown(fn ...func())

func (*Application) Register

func (a *Application) Register(components ...cfacade.IComponent)

func (*Application) Remove

func (a *Application) Remove(name string) cfacade.IComponent

Remove component by name

func (*Application) Running

func (a *Application) Running() bool

func (*Application) SetCluster

func (a *Application) SetCluster(cluster cfacade.ICluster)

SetCluster installs the cross-node transport before the application starts.

func (*Application) SetDefaultBodyCodec added in v1.0.20

func (a *Application) SetDefaultBodyCodec(id int32)

SetDefaultBodyCodec selects the default body codec before Startup. JSON and protobuf remain registered simultaneously.

func (*Application) SetDiscovery

func (a *Application) SetDiscovery(discovery cfacade.IDiscovery)

SetDiscovery installs discovery before the application starts.

func (*Application) Shutdown

func (a *Application) Shutdown()

func (*Application) StartTime

func (a *Application) StartTime() string

func (*Application) Startup

func (a *Application) Startup()

Startup load components before startup

type NodeMode

type NodeMode byte
const (
	Cluster    NodeMode = 1 // 集群模式
	Standalone NodeMode = 2 // 单机模式
)

Directories

Path Synopsis
components
gin
Package cgin from https://github.com/gin-contrib/zap/
Package cgin from https://github.com/gin-contrib/zap/
extend
base58
Package file from https://github.com/akamensky/base58/blob/master/base58.go
Package file from https://github.com/akamensky/base58/blob/master/base58.go
gob
map
Package file from https://github.com/gogf/gf
Package file from https://github.com/gogf/gf
mapstructure
Package exposes functionality to convert one arbitrary Go type into another, typically to convert a map[string]interface{} into a native Go structure.
Package exposes functionality to convert one arbitrary Go type into another, typically to convert a map[string]interface{} into a native Go structure.
net
queue
Package provides an efficient implementation of a multi-producer, single-consumer lock-free queue.
Package provides an efficient implementation of a multi-producer, single-consumer lock-free queue.
regex
Package file from https://github.com/gogf/gf
Package file from https://github.com/gogf/gf
slice
Package code from: https://github.com/beego/beego/blob/develop/core/utils/slice.go
Package code from: https://github.com/beego/beego/blob/develop/core/utils/slice.go
snowflake
Package code from: https://github.com/bwmarrin/snowflake snowflake provides a very simple Twitter snowflake generator and parser.
Package code from: https://github.com/bwmarrin/snowflake snowflake provides a very simple Twitter snowflake generator and parser.
sync
Package file from https://github.com/beego/beego/blob/develop/core/utils/safemap.go
Package file from https://github.com/beego/beego/blob/develop/core/utils/safemap.go
time
Package code from: https://github.com/golang-module/carbon
Package code from: https://github.com/golang-module/carbon
time_wheel
Package file from https://github.com/RussellLuo/timingwheel
Package file from https://github.com/RussellLuo/timingwheel
utils
Package file from https://github.com/gogf/gf
Package file from https://github.com/gogf/gf
rotatelogs
Package rotatelogs is a port of File-RotateLogs from Perl (https://metacpan.org/release/File-RotateLogs), and it allows you to automatically rotate output files when you write to them according to the filename pattern that you can specify.
Package rotatelogs is a port of File-RotateLogs from Perl (https://metacpan.org/release/File-RotateLogs), and it allows you to automatically rotate output files when you write to them according to the filename pattern that you can specify.
net

Jump to

Keyboard shortcuts

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