Skip to content

Commit

Permalink
other: v2 vivid 体验优化前
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed May 17, 2024
1 parent 4340467 commit 8b0abd8
Show file tree
Hide file tree
Showing 15 changed files with 168 additions and 298 deletions.
2 changes: 2 additions & 0 deletions server/actor_srv/network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package server

56 changes: 56 additions & 0 deletions server/actor_srv/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package server

import (
"context"
"github.com/kercylan98/minotaur/server"
"github.com/kercylan98/minotaur/vivid"
)

// New 创建一个新的服务器
func New() *Server {
return &Server{}
}

type Server struct {
ctx context.Context
network server.Network
}

func (s *Server) OnPreStart(ctx vivid.ActorContext) (err error) {
if err = s.network.OnSetup(s.ctx, nil); err != nil {
return
}

ctx.Future(func() vivid.Message {
return s.network.OnRun
})
return
}

func (s *Server) OnReceived(ctx vivid.MessageContext) (err error) {
switch m := ctx.GetMessage().(type) {
case error:
if err = s.network.OnShutdown(); err != nil {
panic(err)
}
ctx.NotifyTerminated(m)
}

return
}

func (s *Server) OnDestroy(ctx vivid.ActorContext) (err error) {

}

func (s *Server) OnSaveSnapshot(ctx vivid.ActorContext) (snapshot []byte, err error) {

}

func (s *Server) OnRecoverSnapshot(ctx vivid.ActorContext, snapshot []byte) (err error) {

}

func (s *Server) OnChildTerminated(ctx vivid.ActorContext, child vivid.ActorTerminatedContext) {

}
21 changes: 0 additions & 21 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
messageEvents "github.com/kercylan98/minotaur/toolkit/nexus/events"
"github.com/kercylan98/minotaur/toolkit/nexus/queues"
"github.com/kercylan98/minotaur/toolkit/random"
"github.com/kercylan98/minotaur/vivid"
"github.com/panjf2000/ants/v2"
"reflect"
"time"
Expand Down Expand Up @@ -64,26 +63,6 @@ type server struct {
scheduler *chrono.Scheduler // 服务器使用的定时器
}

func (s *server) OnPreStart(ctx vivid.ActorContext) (err error) {
//TODO implement me
panic("implement me")
}

func (s *server) OnReceived(ctx vivid.MessageContext) (err error) {
//TODO implement me
panic("implement me")
}

func (s *server) OnDestroy(ctx vivid.ActorContext) (err error) {
//TODO implement me
panic("implement me")
}

func (s *server) OnChildTerminated(ctx vivid.ActorContext, child vivid.ActorTerminatedContext) {
//TODO implement me
panic("implement me")
}

func NewServer(network Network, options ...*Options) Server {
srv := &server{
network: network,
Expand Down
8 changes: 8 additions & 0 deletions vivid/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ type ActorTerminatedContext interface {
// GetActorId 获取 Actor 的 ID
GetActorId() ActorId

// GetActor 获取 Actor 原始对象
// - 常被用于类型断言进行不同 Actor 类型的处理
GetActor() any

// HasTerminatedMessage 判断是否有销毁消息
HasTerminatedMessage() bool

Expand Down Expand Up @@ -71,6 +75,10 @@ func (c *actorTerminatedContext) GetActorId() ActorId {
return c.core.id
}

func (c *actorTerminatedContext) GetActor() any {
return c.core.Actor
}

func (c *actorTerminatedContext) HasTerminatedMessage() bool {
return len(c.terminatedMessages) > 0
}
Expand Down
7 changes: 7 additions & 0 deletions vivid/actor_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ type ActorContext interface {

// GetActorId 获取 Actor 的 ID
GetActorId() ActorId

// Future 创建一个 Future 对象,用于异步获取 Actor 的返回值
Future(handler func() Message) Future
}

type actorContextState = uint8 // actorContext 的状态
Expand Down Expand Up @@ -191,3 +194,7 @@ func (c *actorContext) GetParentActor() ActorRef {
func (c *actorContext) GetActorId() ActorId {
return c.id
}

func (c *actorContext) Future(handler func() Message) Future {
return NewFuture(c, handler)
}
3 changes: 1 addition & 2 deletions vivid/actor_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ func NewActorSystem(name string, opts ...*ActorSystemOptions) *ActorSystem {
replyWaiters: make(map[uint64]chan any),
}
s.actorSystemExternal = new(actorSystemExternal).init(s)
s.ctx, s.cancel = context.WithCancel(context.Background())

return s
}

Expand All @@ -43,6 +41,7 @@ type ActorSystem struct {

// Run 非阻塞的运行 ActorSystem
func (s *ActorSystem) Run() (err error) {
s.ctx, s.cancel = context.WithCancel(context.Background())
pool, err := ants.NewPool(s.opts.AntsPoolSize, s.opts.AntsOptions...)
if err != nil {
return err
Expand Down
57 changes: 57 additions & 0 deletions vivid/actors/actor_terminated_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package actors

func NewActorTerminatedContext(c *actorCore, v ...Message) *ActorTerminatedContext {
return &ActorTerminatedContext{
core: c,
terminatedMessages: v,
}
}

type ActorTerminatedContext struct {
core *actorCore // Actor 核心
terminatedMessages []Message // 销毁消息
cancelTerminate bool // 是否取消销毁
}

func (c *ActorTerminatedContext) GetActorId() ActorId {
return c.core.id
}

func (c *ActorTerminatedContext) GetActor() any {
return c.core.Actor
}

func (c *ActorTerminatedContext) HasTerminatedMessage() bool {
return len(c.terminatedMessages) > 0
}

func (c *ActorTerminatedContext) GetTerminatedMessage() Message {
if len(c.terminatedMessages) == 1 {
return c.terminatedMessages[0]
} else {
return c.terminatedMessages
}
}

func (c *ActorTerminatedContext) Restart() error {
c.core.restartNum++
if _, err := c.core.restart(false); err != nil {
return err
}
c.core.restartNum = 0
c.cancelTerminate = true
return nil
}

func (c *ActorTerminatedContext) Recover() error {
c.cancelTerminate = true
return nil
}

func (c *ActorTerminatedContext) IsPreStart() bool {
return c.core.state == actorContextStatePreStart
}

func (c *ActorTerminatedContext) GetRestartNum() int {
return c.core.restartNum
}
27 changes: 0 additions & 27 deletions vivid/components/behavior_auto_executor.go

This file was deleted.

37 changes: 37 additions & 0 deletions vivid/future.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package vivid

type Receiver interface {
Tell(v Message, opts ...MessageOption) error
Ask(v Message, opts ...MessageOption) (any, error)
}

type Future interface {
// IsCompleted 判断 Future 是否已完成
IsCompleted() bool
}

// NewFuture 创建一个新的 Future 对象,该对象用于异步获取 Actor 的返回值
func NewFuture(ctx ActorContext, handler func() Message) Future {
f := &future{
ctx: ctx,
}
c := ctx.(*actorContext)
var h = func() {
m := handler()
f.done = true
c.core.Tell(m)
}
if err := c.system.gp.Submit(h); err != nil {
go h()
}
return f
}

type future struct {
ctx ActorContext
done bool
}

func (f *future) IsCompleted() bool {
return f.done
}
55 changes: 0 additions & 55 deletions vivid/test/1244/main.go

This file was deleted.

35 changes: 0 additions & 35 deletions vivid/test/1245/main.go

This file was deleted.

Loading

0 comments on commit 8b0abd8

Please sign in to comment.