Skip to content

go-actor is a lightweight message framework using actor model

Notifications You must be signed in to change notification settings

lsg2020/go-actor

Repository files navigation

go-actor

  • go-actor is a lightweight message framework using actor model

初衷

  • 想能在代码逻辑上方便的写无锁的同步rpc调用代码,同时又不会阻塞住其他服务对这个Actor的调用
  • 一个Actor可以有多种身份,想能比较方便的分类管理Actor,分类通过Actor地址调用目标Actor
  • 节点间使用多种传输方式,比如游戏玩家移动这种同步可以使用tcp实现快速通知消息丢失也不太紧要,平台奖励类的可以使用mq通知

快速开始

  • 创建ActorSystem ActorSystem用来分类管理Actor,调用某个Actor的时候需要基于他所在的同名ActorSystem
    • goactor.WithName: 指定ActorSystem名字,同类同名
    • goactor.WithInstanceId: 指定节点id,同类型下唯一
    • goactor.WithEtcd: etcd注册地址,同类型使用同组etcd
    • goactor.WithTransport: 关联传输器
	system, err := goactor.NewActorSystem(
		goactor.WithName("hello"),
		goactor.WithInstanceId(1),
		goactor.WithEtcd("http://127.0.0.1:2379"),
		goactor.WithTransport(trans),
	)
  • 创建执行器
    • SingleGoroutine: 单协程执行器,每个执行器同时只有一个协程在执行逻辑代码,当执行到Executer.Wait时会让出执行权给同执行器的其他协程处理消息,自己挂起直到Executer.OnResponse对应的session唤醒并等待其他协程让出执行权
	single := &executer.SingleGoroutine{}
	single.Start(context.Background(), 1)
  • 创建协议
    • protobuf
      • go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
      • go install github.com/lsg2020/go-actor/tools/protoc-gen-gactor
      • 定义协议文件例如
      • 生成 protoc -I . --go_out=. --gactor_out=. *.proto
	proto := protocols.NewProtobuf(1)
	hello.RegisterHelloService(&HelloService{}, proto)
  • 创建Actor并指定个名字注册到ActorSystem
	func (hello *HelloActor) OnInit(a goactor.Actor) {
		hello.actor = a
		hello.addr = system.Register(a, "hello")
	}

	goactor.NewActor(&HelloActor{}, single, goactor.ActorWithProto(proto))

TODO

  • context取消的传递
  • prometheus指标增加
  • transport 支持多个system

About

go-actor is a lightweight message framework using actor model

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages