Skip to content

Commit

Permalink
other: v2 vivid 体验优化、邮箱池返回了未重置的邮箱修复
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed Jun 6, 2024
1 parent 9d1280d commit a10a132
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 45 deletions.
12 changes: 12 additions & 0 deletions minotaur/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ type Application struct {
server *transport.Server
}

func (a *Application) GetSystem() *vivid.ActorSystem {
return a.actorSystem
}

func (a *Application) GetContext() vivid.ActorContext {
return a.actorSystem.GetContext()
}

func (a *Application) Launch() {
defer func(a *Application) {
close(a.closed)
Expand Down Expand Up @@ -69,3 +77,7 @@ func (a *Application) EventBus() *pulse.Pulse {
func (a *Application) ActorSystem() *vivid.ActorSystem {
return a.actorSystem
}

func (a *Application) ActorOf(ofo vivid.ActorOfO) vivid.ActorRef {
return a.actorSystem.ActorOf(ofo)
}
25 changes: 5 additions & 20 deletions minotaur/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,17 @@ type AccountManager struct {
func (e *AccountManager) OnReceive(ctx vivid.MessageContext) {
switch ctx.GetMessage().(type) {
case vivid.OnPreStart:
ctx.BindBehavior(vivid.BehaviorOf[transport.ServerConnOpenedEvent](e.onConnOpened))
ctx.BindBehavior(vivid.BehaviorOf(e.onConnOpened))
e.EventBus().Subscribe("conn-opened", ctx, transport.ServerConnOpenedEvent{})
}
}

func (e *AccountManager) onConnOpened(ctx vivid.MessageContext, message transport.ServerConnOpenedEvent) {
vivid.ActorOf[*Account](e.ActorSystem(), vivid.NewActorOptions[*Account]().WithInit(func(account *Account) {
account.ConnActor = message.ConnActor
e.ActorSystem().ActorOf(vivid.OfO(func(actorOptions *vivid.ActorOptions[*Account]) {
actorOptions.WithInit(func(account *Account) {
account.ConnActor = message.ConnActor
})
}))

vivid.ActorOfF[*Account](e.ActorSystem(), func(options *vivid.ActorOptions[*Account]) {
options.
WithName("account").
WithInit(func(account *Account) {
account.ConnActor = message.ConnActor
})
})

vivid.ActorOfI(e.ActorSystem(), new(Account), func(options *vivid.ActorOptions[*Account]) {
options.
WithName("account").
WithInit(func(account *Account) {
account.ConnActor = message.ConnActor
})
})

}

type Account struct {
Expand Down
4 changes: 2 additions & 2 deletions minotaur/transport/conn_actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ func (c *ConnActor) OnReceive(ctx vivid.MessageContext) {

func (c *ConnActor) Write(packet Packet) {
if c.writer == nil {
return
panic("should not happen, writer is nil")
}
c.writer.Tell(connWriteMessage{packet})
}

func (c *ConnActor) React(packet Packet) {
if c.reader == nil {
return
panic("should not happen, reader is nil")
}
c.reader.Tell(ConnReceivePacketMessage{packet})
}
2 changes: 1 addition & 1 deletion minotaur/vivid/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (i *FreeActor[T]) GetActor() T {
}

func onReceive(actor Actor, ctx MessageContext) {
actorCtx := ctx.getContext()
actorCtx, _ := ctx.GetContext().(*_ActorCore)
if actorCtx == nil {
actor.OnReceive(ctx)
return
Expand Down
8 changes: 8 additions & 0 deletions minotaur/vivid/actor_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ type ActorContext interface {

// UnbindBehavior 解绑一个已绑定的行为
UnbindBehavior(message Message)

// ActorOf 创建一个 Actor 并返回 ActorRef
// - ActorOfO 对象可通过 OfO 函数快速创建
ActorOf(ofo ActorOfO) ActorRef
}

type _ActorContext struct {
Expand Down Expand Up @@ -67,3 +71,7 @@ func (c *_ActorContext) UnbindBehavior(message Message) {

delete(c.behaviors, reflect.TypeOf(message))
}

func (c *_ActorContext) ActorOf(ofo ActorOfO) ActorRef {
return ofo.generate(c)
}
4 changes: 4 additions & 0 deletions minotaur/vivid/actor_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ type _ActorCore struct {
messageHook func(MessageContext) bool // 消息钩子
}

func (a *_ActorCore) GetContext() ActorContext {
return a._ActorContext
}

func (a *_ActorCore) GetMailboxFactory() MailboxFactory {
return a.mailboxFactory
}
Expand Down
26 changes: 26 additions & 0 deletions minotaur/vivid/actor_ofo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package vivid

// ActorOfO 该接口用于根据 ActorOptions 创建 Actor 并返回 ActorRef
// - 接口用于内部实现,外部无法直接实现
type ActorOfO interface {
generate(of ActorOwner) ActorRef
}

type actorOfO[T Actor] struct {
options []func(actorOptions *ActorOptions[T])
}

func (o *actorOfO[T]) generate(of ActorOwner) ActorRef {
var opts = new(ActorOptions[T])
for _, opt := range o.options {
opt(opts)
}
return ActorOf(of, opts)
}

// OfO 创建一个 ActorOfO 对象
func OfO[T Actor](options ...func(actorOptions *ActorOptions[T])) ActorOfO {
return &actorOfO[T]{
options: options,
}
}
10 changes: 10 additions & 0 deletions minotaur/vivid/actor_owner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package vivid

// ActorOwner 提供了作为合格 Actor 所有者的接口定义
type ActorOwner interface {
// GetSystem 获取 Actor 所属的 ActorSystem
GetSystem() *ActorSystem

// GetContext 获取 Actor 上下文
GetContext() ActorContext
}
14 changes: 6 additions & 8 deletions minotaur/vivid/actor_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ type ActorSystem struct {
cluster string // 集群名称
}

func (s *ActorSystem) ActorOf(ofo ActorOfO) ActorRef {
return s.userGuard.ActorOf(ofo)
}

func (s *ActorSystem) Context() context.Context {
return s.ctx
}
Expand All @@ -80,20 +84,14 @@ func (s *ActorSystem) Shutdown() {
s.waitGroup.Wait()
}

func (s *ActorSystem) getSystem() *ActorSystem {
func (s *ActorSystem) GetSystem() *ActorSystem {
return s
}

func (s *ActorSystem) GetDeadLetters() DeadLetterStream {
return s.deadLetters
}

type actorOf interface {
getSystem() *ActorSystem

getContext() *_ActorCore
}

func (s *ActorSystem) BindMailboxFactory(f MailboxFactory) MailboxFactoryId {
s.mailboxFactorRW.Lock()
defer s.mailboxFactorRW.Unlock()
Expand Down Expand Up @@ -178,7 +176,7 @@ func (s *ActorSystem) getActor(id ActorId) *_ActorCore {
return s.actors[id]
}

func (s *ActorSystem) getContext() *_ActorCore {
func (s *ActorSystem) GetContext() ActorContext {
return s.userGuard
}

Expand Down
2 changes: 1 addition & 1 deletion minotaur/vivid/internal_actor_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

type internalActorContext interface {
actorOf
ActorOwner

// getLock 获取当前 ActorContext 的锁
// - 所有函数均不操作锁,应由外部调用者自行操作
Expand Down
1 change: 0 additions & 1 deletion minotaur/vivid/mailbox_fifo.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ func (f *FIFO) reset() {
f.Stop()
} else {
f.cond.L.Unlock()
return
}

f.cond.L.Lock()
Expand Down
12 changes: 11 additions & 1 deletion minotaur/vivid/mailbox_priority.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ func (p *Priority) GetLockable() sync.Locker {

func (p *Priority) reset() {
p.cond.L.Lock()
if p.status < fifoStateStopping {
p.cond.L.Unlock()
p.Stop()
} else {
p.cond.L.Unlock()
}

p.cond.L.Lock()
defer p.cond.L.Unlock()

p.status = priorityStateNone
p.buffer = p.buffer[:0]
p.cond.L.Unlock()
}
13 changes: 12 additions & 1 deletion minotaur/vivid/message_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

type MessageContext interface {
context.Context
actorOf
ActorOwner
ActorRef

// GetContext 获取 Actor 上下文
Expand Down Expand Up @@ -42,6 +42,9 @@ type MessageContext interface {

// UnbindBehavior 该函数是 ActorContext.UnbindBehavior 的快捷方式
UnbindBehavior(message Message)

// ActorOf 该函数是 ActorContext.ActorOf 的快捷方式
ActorOf(ofo ActorOfO) ActorRef
}

func newMessageContext(system *ActorSystem, message Message, priority int64, instantly, hasReply bool) *_MessageContext {
Expand Down Expand Up @@ -81,6 +84,10 @@ type _MessageContext struct {
InstantlyExec bool // 是否立即执行
}

func (c *_MessageContext) GetSystem() *ActorSystem {
return c.system
}

func (c *_MessageContext) Id() ActorId {
return c.GetRef().Id()
}
Expand Down Expand Up @@ -244,3 +251,7 @@ func (c *_MessageContext) BindBehavior(behavior Behavior) {
func (c *_MessageContext) UnbindBehavior(message Message) {
c.GetContext().UnbindBehavior(message)
}

func (c *_MessageContext) ActorOf(ofo ActorOfO) ActorRef {
return c.GetContext().ActorOf(ofo)
}
20 changes: 10 additions & 10 deletions minotaur/vivid/vivid.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,36 @@ func BehaviorOf[T Message](handler func(ctx MessageContext, message T)) Behavior
return adp
}

func ActorOfI[T Actor](actorOf actorOf, actor T, options ...func(options *ActorOptions[T])) ActorRef {
func ActorOfI[T Actor](actorOf ActorOwner, actor T, options ...func(options *ActorOptions[T])) ActorRef {
var opts = NewActorOptions[T]().WithConstruct(actor)
for _, opt := range options {
opt(opts)
}
return ActorOf(actorOf, opts)
}

func ActorOfF[T Actor](actorOf actorOf, options ...func(options *ActorOptions[T])) ActorRef {
func ActorOfF[T Actor](actorOf ActorOwner, options ...func(options *ActorOptions[T])) ActorRef {
var opts = NewActorOptions[T]()
for _, opt := range options {
opt(opts)
}
return ActorOf(actorOf, opts)
}

func ActorOf[T Actor](actorOf actorOf, options ...*ActorOptions[T]) ActorRef {
func ActorOf[T Actor](actorOf ActorOwner, options ...*ActorOptions[T]) ActorRef {
var opts = parseActorOptions(options...)
var ins = opts.Construct
if reflect.ValueOf(ins).IsNil() {
tof := reflect.TypeOf((*T)(nil)).Elem().Elem()
ins = reflect.New(tof).Interface().(T)
}
var system = actorOf.getSystem()
var system = actorOf.GetSystem()

if opts.Parent == nil {
opts.Parent = actorOf.getContext()
opts.Parent = actorOf.GetContext()
}

ctx, err := generateActor[T](actorOf.getSystem(), ins, opts)
ctx, err := generateActor[T](actorOf.GetSystem(), ins, opts)
if err != nil {
system.deadLetters.DeadLetter(NewDeadLetterEvent(DeadLetterEventTypeActorOf, DeadLetterEventActorOf{
Error: err,
Expand All @@ -63,20 +63,20 @@ func ActorOf[T Actor](actorOf actorOf, options ...*ActorOptions[T]) ActorRef {
return ctx
}

func FreeActorOf[T any](actorOf actorOf, options ...*ActorOptions[*FreeActor[T]]) ActorRef {
func FreeActorOf[T any](actorOf ActorOwner, options ...*ActorOptions[*FreeActor[T]]) ActorRef {
var opts = parseActorOptions(options...)
var ins = opts.Construct.actor
if reflect.ValueOf(ins).IsNil() {
tof := reflect.TypeOf((*T)(nil)).Elem().Elem()
ins = reflect.New(tof).Interface().(T)
}
var system = actorOf.getSystem()
var system = actorOf.GetSystem()

if opts.Parent == nil {
opts.Parent = actorOf.getContext()
opts.Parent = actorOf.GetContext()
}

ctx, err := generateActor[*FreeActor[T]](actorOf.getSystem(), &FreeActor[T]{actor: ins}, opts)
ctx, err := generateActor[*FreeActor[T]](actorOf.GetSystem(), &FreeActor[T]{actor: ins}, opts)
if err != nil {
system.deadLetters.DeadLetter(NewDeadLetterEvent(DeadLetterEventTypeActorOf, DeadLetterEventActorOf{
Error: err,
Expand Down

0 comments on commit a10a132

Please sign in to comment.