feat 模块增加after start

This commit is contained in:
2026-01-20 12:11:26 +08:00
parent 5aeff7c786
commit c0f6cd90c6
11 changed files with 43 additions and 38 deletions

View File

@@ -6,8 +6,10 @@ import (
"git.hlsq.asia/mmorpg/service-common/log" "git.hlsq.asia/mmorpg/service-common/log"
"git.hlsq.asia/mmorpg/service-common/module" "git.hlsq.asia/mmorpg/service-common/module"
"git.hlsq.asia/mmorpg/service-gateway/config" "git.hlsq.asia/mmorpg/service-gateway/config"
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
"git.hlsq.asia/mmorpg/service-gateway/internal/grpc_server" "git.hlsq.asia/mmorpg/service-gateway/internal/grpc_server"
"github.com/judwhite/go-svc" "github.com/judwhite/go-svc"
"sync"
) )
type Program struct { type Program struct {
@@ -23,8 +25,8 @@ func (p *Program) Init(_ svc.Environment) error {
p.moduleList = append(p.moduleList, (&module.DB{}).Bind(config.Get().DB)) p.moduleList = append(p.moduleList, (&module.DB{}).Bind(config.Get().DB))
p.moduleList = append(p.moduleList, &ModuleWebServer{}) p.moduleList = append(p.moduleList, &ModuleWebServer{})
p.moduleList = append(p.moduleList, &ModuleWebsocketServer{}) p.moduleList = append(p.moduleList, &ModuleWebsocketServer{})
p.moduleList = append(p.moduleList, (&module.Grpc{}).Bind(grpc_server.NewServer(config.Get().Serve.Grpc)))
p.moduleList = append(p.moduleList, &ModuleLoginQueue{}) p.moduleList = append(p.moduleList, &ModuleLoginQueue{})
p.moduleList = append(p.moduleList, (&module.Grpc{}).Bind(grpc_server.NewServer(config.Get().Serve.Grpc)))
p.moduleList = append(p.moduleList, (&module.Prometheus{}).Bind(config.Get().Metric)) p.moduleList = append(p.moduleList, (&module.Prometheus{}).Bind(config.Get().Metric))
p.moduleList = append(p.moduleList, (&module.Tracer{}).Bind(config.Get().Metric, common.KeyDiscoverServiceNameGateway)) p.moduleList = append(p.moduleList, (&module.Tracer{}).Bind(config.Get().Metric, common.KeyDiscoverServiceNameGateway))
p.moduleList = append(p.moduleList, &module.Discover{}) p.moduleList = append(p.moduleList, &module.Discover{})
@@ -42,11 +44,20 @@ func (p *Program) Init(_ svc.Environment) error {
} }
func (p *Program) Start() error { func (p *Program) Start() error {
ready := &sync.WaitGroup{}
ready.Add(len(p.moduleList))
for _, m := range p.moduleList { for _, m := range p.moduleList {
if err := m.Start(); err != nil { if err := m.Start(ready); err != nil {
return err return err
} }
} }
ready.Wait()
for _, m := range p.moduleList {
if err := m.AfterStart(); err != nil {
return err
}
}
global.ServiceStatus = global.ServiceStatusReady
log.Infof(fmt.Sprintf("%v Start successful...", config.Get().App.Name)) log.Infof(fmt.Sprintf("%v Start successful...", config.Get().App.Name))
return nil return nil

View File

@@ -10,6 +10,7 @@ import (
// ModuleBase 基础模块,或者一些零散的模块 // ModuleBase 基础模块,或者一些零散的模块
type ModuleBase struct { type ModuleBase struct {
module.DefaultModule
} }
func (m *ModuleBase) Init() error { func (m *ModuleBase) Init() error {
@@ -24,15 +25,3 @@ func (m *ModuleBase) Init() error {
utils.InitSnowflake(int64(rand.Intn(1000))) utils.InitSnowflake(int64(rand.Intn(1000)))
return nil return nil
} }
func (m *ModuleBase) Start() error {
return nil
}
func (m *ModuleBase) Stop() error {
return nil
}
func (m *ModuleBase) Bind(_ ...any) module.Module {
return m
}

View File

@@ -5,10 +5,12 @@ import (
"git.hlsq.asia/mmorpg/service-gateway/internal/global" "git.hlsq.asia/mmorpg/service-gateway/internal/global"
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/login" "git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/login"
"runtime" "runtime"
"sync"
) )
// ModuleLoginQueue 登录队列模块 // ModuleLoginQueue 登录队列模块
type ModuleLoginQueue struct { type ModuleLoginQueue struct {
module.DefaultModule
login *login.Login login *login.Login
queueUp *login.QueueUp queueUp *login.QueueUp
} }
@@ -19,8 +21,9 @@ func (m *ModuleLoginQueue) Init() error {
return nil return nil
} }
func (m *ModuleLoginQueue) Start() error { func (m *ModuleLoginQueue) Start(ready *sync.WaitGroup) error {
m.login.Start(runtime.NumCPU()) m.login.Start(runtime.NumCPU())
ready.Done()
return nil return nil
} }
@@ -28,7 +31,3 @@ func (m *ModuleLoginQueue) Stop() error {
m.login.Stop() m.login.Stop()
return nil return nil
} }
func (m *ModuleLoginQueue) Bind(_ ...any) module.Module {
return m
}

View File

@@ -14,6 +14,7 @@ import (
// ModuleWebServer Web服务模块 // ModuleWebServer Web服务模块
type ModuleWebServer struct { type ModuleWebServer struct {
module.DefaultModule
wg *sync.WaitGroup wg *sync.WaitGroup
server *http.Server server *http.Server
} }
@@ -23,7 +24,7 @@ func (m *ModuleWebServer) Init() error {
return nil return nil
} }
func (m *ModuleWebServer) Start() error { func (m *ModuleWebServer) Start(ready *sync.WaitGroup) error {
m.wg.Add(1) m.wg.Add(1)
go func() { go func() {
defer m.wg.Done() defer m.wg.Done()
@@ -31,6 +32,7 @@ func (m *ModuleWebServer) Start() error {
Addr: fmt.Sprintf("%v:%v", config.Get().Serve.Http.Address, config.Get().Serve.Http.Port), Addr: fmt.Sprintf("%v:%v", config.Get().Serve.Http.Address, config.Get().Serve.Http.Port),
Handler: http_gateway.InitRouter(), Handler: http_gateway.InitRouter(),
} }
ready.Done()
if err := m.server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { if err := m.server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Errorf("http server failed: %v", err.Error()) log.Errorf("http server failed: %v", err.Error())
} }
@@ -46,7 +48,3 @@ func (m *ModuleWebServer) Stop() error {
m.wg.Wait() m.wg.Wait()
return nil return nil
} }
func (m *ModuleWebServer) Bind(_ ...any) module.Module {
return m
}

View File

@@ -14,6 +14,7 @@ import (
// ModuleWebsocketServer Websocket服务模块 // ModuleWebsocketServer Websocket服务模块
type ModuleWebsocketServer struct { type ModuleWebsocketServer struct {
module.DefaultModule
wg *sync.WaitGroup wg *sync.WaitGroup
server *websocket.WSServer server *websocket.WSServer
} }
@@ -28,10 +29,11 @@ func (m *ModuleWebsocketServer) Init() error {
return nil return nil
} }
func (m *ModuleWebsocketServer) Start() error { func (m *ModuleWebsocketServer) Start(ready *sync.WaitGroup) error {
m.wg.Add(1) m.wg.Add(1)
go func() { go func() {
defer m.wg.Done() defer m.wg.Done()
ready.Done()
_ = m.server.Run( _ = m.server.Run(
fmt.Sprintf("tcp4://0.0.0.0:%v", config.Get().Serve.Socket.Web.Port), fmt.Sprintf("tcp4://0.0.0.0:%v", config.Get().Serve.Socket.Web.Port),
true, true,
@@ -55,7 +57,3 @@ func (m *ModuleWebsocketServer) Stop() error {
m.wg.Wait() m.wg.Wait()
return nil return nil
} }
func (m *ModuleWebsocketServer) Bind(_ ...any) module.Module {
return m
}

2
go.mod
View File

@@ -4,7 +4,7 @@ go 1.24.0
require ( require (
bou.ke/monkey v1.0.2 bou.ke/monkey v1.0.2
git.hlsq.asia/mmorpg/service-common v0.0.0-20260117160658-22d48542a852 git.hlsq.asia/mmorpg/service-common v0.0.0-20260120040718-1edebb439c58
github.com/alicebob/miniredis/v2 v2.35.0 github.com/alicebob/miniredis/v2 v2.35.0
github.com/gin-contrib/cors v1.7.6 github.com/gin-contrib/cors v1.7.6
github.com/gin-gonic/gin v1.11.0 github.com/gin-gonic/gin v1.11.0

4
go.sum
View File

@@ -2,8 +2,8 @@ bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI=
bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA= bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA=
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
git.hlsq.asia/mmorpg/service-common v0.0.0-20260117160658-22d48542a852 h1:ehZ54MgGL3CO7KKDPxybFabIXxiQud1TGMcKjrSTcnw= git.hlsq.asia/mmorpg/service-common v0.0.0-20260120040718-1edebb439c58 h1:Bgn3rrISGqzyNA4ax5o4tiTy3tK3QcM/+tCr80fOBhE=
git.hlsq.asia/mmorpg/service-common v0.0.0-20260117160658-22d48542a852/go.mod h1:Dazg+4woCv9Jk7jgT2qUSGWhZOXx/0WYfJO+FCUDyhw= git.hlsq.asia/mmorpg/service-common v0.0.0-20260120040718-1edebb439c58/go.mod h1:Dazg+4woCv9Jk7jgT2qUSGWhZOXx/0WYfJO+FCUDyhw=
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0= github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/alicebob/miniredis/v2 v2.35.0 h1:QwLphYqCEAo1eu1TqPRN2jgVMPBweeQcR21jeqDCONI= github.com/alicebob/miniredis/v2 v2.35.0 h1:QwLphYqCEAo1eu1TqPRN2jgVMPBweeQcR21jeqDCONI=

View File

@@ -1,5 +1,17 @@
package global package global
var ServiceStatus = ServiceStatusStarting
type ServiceStatusCode int32
const (
ServiceStatusStarting ServiceStatusCode = iota // 启动中
ServiceStatusReady // 就绪
ServiceStatusStopping // 停止中
)
var GatewaySID string
const ( const (
KeyGatewayAccessToken = "gateway:token:access:%v" KeyGatewayAccessToken = "gateway:token:access:%v"
KeyGatewayRefreshToken = "gateway:token:refresh:%v" KeyGatewayRefreshToken = "gateway:token:refresh:%v"

View File

@@ -5,7 +5,7 @@ import (
"git.hlsq.asia/mmorpg/service-common/discover/common" "git.hlsq.asia/mmorpg/service-common/discover/common"
"git.hlsq.asia/mmorpg/service-common/net/grpc/service" "git.hlsq.asia/mmorpg/service-common/net/grpc/service"
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb" "git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/client" "git.hlsq.asia/mmorpg/service-gateway/internal/global"
"google.golang.org/grpc" "google.golang.org/grpc"
) )
@@ -33,7 +33,7 @@ func (s *Server) OnCustomGrpcServerOption() []grpc.ServerOption {
} }
func (s *Server) OnInit(serve *grpc.Server) { func (s *Server) OnInit(serve *grpc.Server) {
client.GatewaySID = s.SID global.GatewaySID = s.SID
grpc_pb.RegisterGatewayServer(serve, s) grpc_pb.RegisterGatewayServer(serve, s)
} }

View File

@@ -13,8 +13,6 @@ import (
"time" "time"
) )
var GatewaySID string
type Client struct { type Client struct {
sync.WaitGroup sync.WaitGroup
conn socket.ISocketConn // Socket conn socket.ISocketConn // Socket

View File

@@ -45,7 +45,7 @@ func (c *Client) handle(event Event) {
if c.Status == 0 { if c.Status == 0 {
c.Status = 1 c.Status = 1
UserMgr.Add(c.USN, c) UserMgr.Add(c.USN, c)
redis.GetClient().HSet(c.ctx, fmt.Sprintf(global.KeyGatewayInfo, c.USN), global.HFieldInfoGatewaySID, GatewaySID) redis.GetClient().HSet(c.ctx, fmt.Sprintf(global.KeyGatewayInfo, c.USN), global.HFieldInfoGatewaySID, global.GatewaySID)
c.WriteMessage(ss_pb.MessageID_MESSAGE_ID_LOGIN_SUCCESS, &ss_pb.S2C_LoginSuccess{ c.WriteMessage(ss_pb.MessageID_MESSAGE_ID_LOGIN_SUCCESS, &ss_pb.S2C_LoginSuccess{
InstanceID: 1, InstanceID: 1,
}) })
@@ -61,7 +61,7 @@ func (c *Client) onEnter(msg *ss_pb.C2S_EnterInstance) {
} }
resp, err := client.Enter(c.ctx, &grpc_pb.EnterReq{ resp, err := client.Enter(c.ctx, &grpc_pb.EnterReq{
USN: c.USN, USN: c.USN,
GatewaySID: GatewaySID, GatewaySID: global.GatewaySID,
InstanceID: msg.InstanceID, InstanceID: msg.InstanceID,
}) })
if err != nil { if err != nil {