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 12, 2024
1 parent ffd79f3 commit bd92df0
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 76 deletions.
4 changes: 4 additions & 0 deletions minotaur/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func (a *Application) Launch() {
a.server.Protocol().Launch(a.options.Network)
}

for _, hook := range a.options.LaunchedHooks {
hook(a)
}

var systemSignal = make(chan os.Signal, 1)
signal.Notify(systemSignal, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
select {
Expand Down
18 changes: 14 additions & 4 deletions minotaur/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@ import (
"github.com/kercylan98/minotaur/minotaur"
"github.com/kercylan98/minotaur/minotaur/transport"
"github.com/kercylan98/minotaur/minotaur/transport/network"
"github.com/kercylan98/minotaur/minotaur/vivid"
"testing"
)

func TestNewApplication(t *testing.T) {
app := minotaur.NewApplication(minotaur.WithNetwork(network.WebSocket(":9988")))
app.GetServer().Protocol().SubscribeConnOpenedEvent("conn_opened", app.ActorSystem(), func(event transport.ServerConnOpenedEvent) {
t.Log("conn opened")
})
app := minotaur.NewApplication(
minotaur.WithNetwork(network.WebSocket(":9988")),
minotaur.WithLaunchedHook(func(app *minotaur.Application) {
app.GetServer().
Protocol().
SubscribeConnOpenedEvent("conn_opened",
func(ctx vivid.MessageContext, event transport.ServerConnOpenedEvent) {
t.Log("conn opened")
},
)
}),
)

app.Launch()
}
8 changes: 8 additions & 0 deletions minotaur/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type Options struct {
ActorSystemName string // Actor 系统名称
EventBusActorName string // 事件总线 Actor 名称
Network transport.Network // 网络

LaunchedHooks []func(app *Application) // 启动钩子
}

// defaultApply 设置缺省值
Expand All @@ -35,6 +37,12 @@ func (o *Options) apply(options ...Option) *Options {
return o.defaultApply()
}

func WithLaunchedHook(hooks ...func(app *Application)) Option {
return func(o *Options) {
o.LaunchedHooks = append(o.LaunchedHooks, hooks...)
}
}

func WithLogger(logger *log.Logger) Option {
return func(o *Options) {
o.Logger = logger
Expand Down
15 changes: 9 additions & 6 deletions minotaur/transport/conn_actor.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package transport

import (
"fmt"
"github.com/kercylan98/minotaur/minotaur/vivid"
"net"
)
Expand Down Expand Up @@ -44,12 +45,14 @@ func (c *ConnActor) OnReceive(ctx vivid.MessageContext) {
switch ctx.GetMessage().(type) {
case vivid.OnBoot:
c.reader = ctx.GetRef()
c.writer = vivid.ActorOf[*ConnWriteActor](c.server, vivid.NewActorOptions[*ConnWriteActor]().WithConstruct(func() *ConnWriteActor {
return &ConnWriteActor{
conn: c.conn,
writer: c.connWriter,
}
}()))
c.writer = vivid.ActorOf[*ConnWriteActor](c.server, vivid.NewActorOptions[*ConnWriteActor]().
WithName(fmt.Sprintf("conn-write-%s", c.conn.RemoteAddr().String())).
WithConstruct(func() *ConnWriteActor {
return &ConnWriteActor{
conn: c.conn,
writer: c.connWriter,
}
}()))
case ConnReceivePacketMessage:

}
Expand Down
23 changes: 21 additions & 2 deletions minotaur/transport/server_actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ type (
ServerConnClosedMessage struct {
conn net.Conn
}

ServerSubscribeConnOpenedMessage struct {
SubscribeId vivid.SubscribeId
Handler func(ctx vivid.MessageContext, event ServerConnOpenedEvent)
Options []vivid.SubscribeOption
}

ServerSubscribeConnClosedMessage struct {
SubscribeId vivid.SubscribeId
Handler func(ctx vivid.MessageContext, event ServerConnClosedEvent)
Options []vivid.SubscribeOption
}
)

type (
Expand All @@ -47,7 +59,6 @@ func NewServerActor(system *vivid.ActorSystem, options ...*vivid.ActorOptions[*S

type ServerActor struct {
ServerActorTyped
system *vivid.ActorSystem
network Network
core *serverCore
connections map[net.Conn]*ConnActor
Expand All @@ -67,11 +78,16 @@ func (s *ServerActor) OnReceive(ctx vivid.MessageContext) {
s.onServerConnOpened(ctx, m)
case ServerConnClosedMessage:
s.onServerConnClosed(ctx, m)
case ServerSubscribeConnOpenedMessage:
ctx.Become(vivid.BehaviorOf[ServerConnOpenedEvent](m.Handler))
ctx.GetSystem().Subscribe(m.SubscribeId, ctx, ServerConnOpenedEvent{})
case ServerSubscribeConnClosedMessage:
ctx.Become(vivid.BehaviorOf[ServerConnClosedEvent](m.Handler))
ctx.GetSystem().Subscribe(m.SubscribeId, ctx, ServerConnClosedEvent{})
}
}

func (s *ServerActor) onBoot(ctx vivid.MessageContext) {
s.system = ctx.GetSystem()
s.connections = make(map[net.Conn]*ConnActor)
s.core = new(serverCore).init(ctx.GetRef())
}
Expand Down Expand Up @@ -101,6 +117,9 @@ func (s *ServerActor) onServerShutdown(ctx vivid.MessageContext, m ServerShutdow

func (s *ServerActor) onServerConnOpened(ctx vivid.MessageContext, m ServerConnOpenedMessage) {
conn := newConn(ctx.GetContext(), m.conn, m.writer)
vivid.ActorOfI(ctx, conn, func(options *vivid.ActorOptions[*ConnActor]) {
options.WithName(fmt.Sprintf("conn-%s", m.conn.RemoteAddr().String()))
})
s.connections[m.conn] = conn
ctx.Reply(ConnCore(conn))
ctx.GetSystem().Publish(ctx, ServerConnOpenedEvent{ConnActor: conn})
Expand Down
12 changes: 6 additions & 6 deletions minotaur/transport/server_actor_typed.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ type ServerActorTyped interface {
Shutdown()

// SubscribeConnOpenedEvent 订阅连接打开事件
SubscribeConnOpenedEvent(subscribeId vivid.SubscribeId, subscriber vivid.Subscriber, handler func(ServerConnOpenedEvent), options ...vivid.SubscribeOption)
SubscribeConnOpenedEvent(subscribeId vivid.SubscribeId, handler func(ctx vivid.MessageContext, event ServerConnOpenedEvent), options ...vivid.SubscribeOption)

// SubscribeConnClosedEvent 订阅连接关闭事件
SubscribeConnClosedEvent(subscribeId vivid.SubscribeId, subscriber vivid.Subscriber, handler func(ServerConnClosedEvent), options ...vivid.SubscribeOption)
SubscribeConnClosedEvent(subscribeId vivid.SubscribeId, handler func(ctx vivid.MessageContext, event ServerConnClosedEvent), options ...vivid.SubscribeOption)
}

type ServerActorTypedImpl struct {
Expand All @@ -30,10 +30,10 @@ func (s *ServerActorTypedImpl) Shutdown() {
s.ref.Tell(ServerShutdownMessage{})
}

func (s *ServerActorTypedImpl) SubscribeConnOpenedEvent(subscribeId vivid.SubscribeId, subscriber vivid.Subscriber, handler func(ServerConnOpenedEvent), options ...vivid.SubscribeOption) {
s.ref.GetSystem().Subscribe(subscribeId, subscriber, handler, options...)
func (s *ServerActorTypedImpl) SubscribeConnOpenedEvent(subscribeId vivid.SubscribeId, handler func(ctx vivid.MessageContext, event ServerConnOpenedEvent), options ...vivid.SubscribeOption) {
s.ref.Tell(ServerSubscribeConnOpenedMessage{SubscribeId: subscribeId, Handler: handler, Options: options})
}

func (s *ServerActorTypedImpl) SubscribeConnClosedEvent(subscribeId vivid.SubscribeId, subscriber vivid.Subscriber, handler func(ServerConnClosedEvent), options ...vivid.SubscribeOption) {
s.ref.GetSystem().Subscribe(subscribeId, subscriber, handler, options...)
func (s *ServerActorTypedImpl) SubscribeConnClosedEvent(subscribeId vivid.SubscribeId, handler func(ctx vivid.MessageContext, event ServerConnClosedEvent), options ...vivid.SubscribeOption) {
s.ref.Tell(ServerSubscribeConnClosedMessage{SubscribeId: subscribeId, Handler: handler, Options: options})
}
58 changes: 0 additions & 58 deletions minotaur/transport/server_test.go

This file was deleted.

0 comments on commit bd92df0

Please sign in to comment.