From 3549fcca11691299e311928fb79ee15863a276cf Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Mon, 5 Feb 2024 12:06:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20modular=20=E5=8C=85=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=20Block=20=E6=8E=A5=E5=8F=A3=EF=BC=8C=E5=BD=93=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E5=8C=96=E6=9C=8D=E5=8A=A1=E5=AE=9E=E7=8E=B0=20modula?= =?UTF-8?q?r.Service=20=E5=90=8E=E5=8F=AF=E9=80=89=E6=8B=A9=E7=9A=84?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=20Block=20=E6=8E=A5=E5=8F=A3=EF=BC=8C?= =?UTF-8?q?=E8=AF=A5=E6=8E=A5=E5=8F=A3=E5=B0=86=E9=80=82=E7=94=A8=E4=BA=8E?= =?UTF-8?q?=E5=85=B7=E6=9C=89=E9=98=BB=E5=A1=9E=E7=AD=89=E5=BE=85=E9=9C=80?= =?UTF-8?q?=E6=B1=82=E7=9A=84=E6=9C=8D=E5=8A=A1=EF=BC=8C=E4=BE=8B=E5=A6=82?= =?UTF-8?q?=E7=BD=91=E7=BB=9C=E6=9C=8D=E5=8A=A1=E5=99=A8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modular/block.go | 10 +++++++ .../service/services/server/server.go | 28 +++++++++++++++++++ modular/example/main.go | 2 ++ modular/modular.go | 16 +++++++++++ 4 files changed, 56 insertions(+) create mode 100644 modular/block.go create mode 100644 modular/example/internal/service/services/server/server.go diff --git a/modular/block.go b/modular/block.go new file mode 100644 index 0000000..06df717 --- /dev/null +++ b/modular/block.go @@ -0,0 +1,10 @@ +package modular + +// Block 标识模块化服务为阻塞进程的服务,当实现了 Service 且实现了 Block 接口时,模块化应用程序会在 Service.OnMount 阶段完成后执行 OnBlock 函数 +// +// 该接口适用于 Http 服务、WebSocket 服务等需要阻塞进程的服务。需要注意的是, OnBlock 的执行不能保证按照 Service 的注册顺序执行 +type Block interface { + Service + // OnBlock 阻塞进程 + OnBlock() +} diff --git a/modular/example/internal/service/services/server/server.go b/modular/example/internal/service/services/server/server.go new file mode 100644 index 0000000..9c080a3 --- /dev/null +++ b/modular/example/internal/service/services/server/server.go @@ -0,0 +1,28 @@ +package server + +import ( + "github.com/kercylan98/minotaur/server" + "time" +) + +type Service struct { + srv *server.Server +} + +func (s *Service) OnInit() { + s.srv = server.New(server.NetworkNone, server.WithLimitLife(time.Second*3)) +} + +func (s *Service) OnPreload() { + +} + +func (s *Service) OnMount() { + +} + +func (s *Service) OnBlock() { + if err := s.srv.RunNone(); err != nil { + panic(err) + } +} diff --git a/modular/example/main.go b/modular/example/main.go index 35d7a1c..3ff1e99 100644 --- a/modular/example/main.go +++ b/modular/example/main.go @@ -4,11 +4,13 @@ import ( "github.com/kercylan98/minotaur/modular" "github.com/kercylan98/minotaur/modular/example/internal/service/services/attack" "github.com/kercylan98/minotaur/modular/example/internal/service/services/login" + "github.com/kercylan98/minotaur/modular/example/internal/service/services/server" ) func main() { modular.RegisterServices( new(attack.Service), + new(server.Service), new(login.Service), ) modular.Run() diff --git a/modular/modular.go b/modular/modular.go index a0d5011..e700368 100644 --- a/modular/modular.go +++ b/modular/modular.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/kercylan98/minotaur/utils/log" "reflect" + "sync" ) var application *modular @@ -56,4 +57,19 @@ func Run() { s := m.services[i] s.instance.OnMount() } + + // OnBlock + var wait = new(sync.WaitGroup) + for i := 0; i < len(m.services); i++ { + s := m.services[i] + if block, ok := s.instance.(Block); ok { + wait.Add(1) + go func(wait *sync.WaitGroup) { + defer wait.Done() + block.OnBlock() + }(wait) + } + } + wait.Wait() + log.Info("all services exited") }