Skip to content

Commit

Permalink
refactor(vivid): optimize actor reference handling and mod status man…
Browse files Browse the repository at this point in the history
…agement

Optimize the way actor references are handled, removing the GetActorIdByActorReffunction and using the new Id() method directly from the ActorRef interface.
This change simplifies the retrieval of actor IDs and improves code clarity.

Update mod status management by introducing modStatus enum and related methods
setStatus and getStatus, replacing the previous boolean flags. This enum-basedapproach enhances the readability and maintainability of the mod lifecycle management.

BREAKING CHANGE: GetActorIdByActorRef function has been removed. Update yourcode to use the new Id() method from the ActorRef interface.

Modified files:
- event_bus.go: use Id() method for producerActorId assignment
- message_context.go: add Terminated() and SetIdleTimeout() methods
- message_options.go: remove outdated comment about WithInstantly option
- mod.go: introduce modStatus enum and related methods
- object_pool.go: rename Release method to Put for consistency- various tests: update according to the changes in the codebase
  • Loading branch information
kercylan98 committed Jun 14, 2024
1 parent 4732b99 commit 1220b60
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 40 deletions.
3 changes: 2 additions & 1 deletion minotaur/transport/server_actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/kercylan98/minotaur/toolkit/network"
"net"
"reflect"
"time"
)

func NewServerActor(system *vivid.ActorSystem, options ...*vivid.ActorOptions[*ServerActor]) vivid.TypedActorRef[ServerActorTyped] {
Expand Down Expand Up @@ -81,8 +82,8 @@ func (s *ServerActor) onServerConnOpened(ctx vivid.MessageContext, m ServerConnO
options.
WithName("conn-" + m.conn.RemoteAddr().String()).
WithStopOnParentRestart(true).
WithIdleTimeout(time.Second).
WithSupervisor(func(message, reason vivid.Message) vivid.Directive {
log.Error("connOpened", log.String("message", reflect.TypeOf(message).String()), log.Any("reason", reason))
return vivid.DirectiveStop
})
}))
Expand Down
2 changes: 1 addition & 1 deletion minotaur/vivid/event_bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (e *EventBusActor) onPublish(ctx MessageContext, m PublishMessage) {
}

// 获取生产者 ActorId
var producerActorId = GetActorIdByActorRef(m.Producer)
var producerActorId = m.Producer.Id()
var subscribeNoPriorityList []subscribeInfo
var subscribePriorityList []subscribeInfo
for _, info := range subscribeMap {
Expand Down
14 changes: 14 additions & 0 deletions minotaur/vivid/message_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ type MessageContext interface {

// ApplyMod 应用模组,该函数是 ActorContext.ApplyMod 的快捷方式
ApplyMod()

// Terminated 是否已经终止
Terminated() bool

// SetIdleTimeout 设置空闲超时时间
SetIdleTimeout(timeout time.Duration)
}

func newMessageContext(system *ActorSystem, message Message, priority int64, instantly, hasReply bool) *_MessageContext {
Expand Down Expand Up @@ -301,3 +307,11 @@ func (c *_MessageContext) UnloadMod(mods ...ModInfo) {
func (c *_MessageContext) ApplyMod() {
c.GetContext().ApplyMod()
}

func (c *_MessageContext) Terminated() bool {
return c.GetContext().Terminated()
}

func (c *_MessageContext) SetIdleTimeout(timeout time.Duration) {
c.GetContext().SetIdleTimeout(timeout)
}
1 change: 0 additions & 1 deletion minotaur/vivid/message_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ func (o *MessageOptions) apply(options []MessageOption) *MessageOptions {
}

// WithInstantly 设置消息是否立即执行,如果设置为立即执行,消息将会被立即执行,否则将会被放入邮箱等待执行
// - 由于没有放入邮箱等待执行,该消息是在当前线程中执行的,如果存在循环调用,可能会导致死锁
// - 该可选项在跨网络调用时可能不会产生效果
// - 该可选项将提供给 Dispatcher 进行处理,根据不同的 Dispatcher 实现,该可选项可能会被忽略
func WithInstantly(instantly bool) MessageOption {
Expand Down
33 changes: 15 additions & 18 deletions minotaur/vivid/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ const (
ModLifeCycleOnStop
)

const (
modStatusWaiting modStatus = iota // 等待加载
modStatusLoaded // 已加载
modStatusUnload // 已卸载
)

type modStatus uint8

// Mod 模组是用于对 Actor 在生命周期中的功能扩展
type Mod interface {
// OnLifeCycle 模组生命周期回调,当模组生命周期发生变化时,将会调用该函数
Expand Down Expand Up @@ -61,18 +69,15 @@ type ModInfo interface {
onLifeCycle(ctx ActorContext, lifeCycle ModLifeCycle)
provide(ctx ActorContext, injector do.Injector)
getModType() reflect.Type
setUnload()
setLoaded()
isUnload() bool
isLoaded() bool
setStatus(status modStatus)
getStatus() modStatus
shutdown()
}

type modInfo[T Mod] struct {
mod Mod
modType reflect.Type
unload bool
loaded bool
status modStatus
shutdownHandler func()
}

Expand All @@ -92,20 +97,12 @@ func (m *modInfo[T]) getModType() reflect.Type {
return m.modType
}

func (m *modInfo[T]) setUnload() {
m.unload = true
}

func (m *modInfo[T]) isUnload() bool {
return m.unload
}

func (m *modInfo[T]) setLoaded() {
m.loaded = false
func (m *modInfo[T]) setStatus(status modStatus) {
m.status = status
}

func (m *modInfo[T]) isLoaded() bool {
return m.loaded
func (m *modInfo[T]) getStatus() modStatus {
return m.status
}

func (m *modInfo[T]) shutdown() {
Expand Down
14 changes: 0 additions & 14 deletions minotaur/vivid/vivid.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,3 @@ func GetActor[T any](ctx Context) (t T) {
return t
}
}

// GetActorIdByActorRef 通过 ActorRef 获取 ActorId
func GetActorIdByActorRef(ref ActorRef) ActorId {
switch v := ref.(type) {
case *_ActorCore:
return v.GetId()
case *_LocalActorRef:
return v.core.GetId()
case *_RemoteActorRef:
return v.actorId
default:
return ""
}
}
30 changes: 30 additions & 0 deletions toolkit/log/silent_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package log

import (
"context"
"log/slog"
)

// NewSilentLogger 创建一个静默日志记录器,该记录器不会输出任何日志
func NewSilentLogger() *Logger {
return slog.New(new(SilentHandler))
}

type SilentHandler struct {
}

func (s SilentHandler) Enabled(ctx context.Context, level slog.Level) bool {
return false
}

func (s SilentHandler) Handle(ctx context.Context, record slog.Record) error {
return nil
}

func (s SilentHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return nil
}

func (s SilentHandler) WithGroup(name string) slog.Handler {
return nil
}
4 changes: 2 additions & 2 deletions toolkit/pools/object_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func (op *ObjectPool[T]) Get() T {
return op.p.Get().(T)
}

// Release 将使用完成的对象放回缓冲区
func (op *ObjectPool[T]) Release(data T) {
// Put 将使用完成的对象放回缓冲区
func (op *ObjectPool[T]) Put(data T) {
op.releaser(data)
op.p.Put(data)
}
2 changes: 1 addition & 1 deletion toolkit/pools/object_pool_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func BenchmarkPool_Get2Put(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
msg := pool.Get()
pool.Release(msg)
pool.Put(msg)
}
})
b.StopTimer()
Expand Down
2 changes: 1 addition & 1 deletion toolkit/pools/object_pool_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func ExampleNewObjectPool() {

m := *p.Get()
m[1] = 1
p.Release(&m)
p.Put(&m)
fmt.Println(m)
// Output:
// map[]
Expand Down
2 changes: 1 addition & 1 deletion toolkit/pools/object_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestObjectPool_Release(t *testing.T) {
msg := pool.Get()
m := *msg
m["test"] = 1
pool.Release(msg)
pool.Put(msg)
if len(m) != 0 {
t.Error("TestObjectPool_Release failed")
}
Expand Down

0 comments on commit 1220b60

Please sign in to comment.