feat 自定义拦截器

This commit is contained in:
2026-01-16 23:51:45 +08:00
parent b76595d164
commit ba16c2cce3
5 changed files with 20 additions and 4 deletions

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-20260116143417-d944729ad0e5 git.hlsq.asia/mmorpg/service-common v0.0.0-20260116155014-3de703eb83c5
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-20260116143417-d944729ad0e5 h1:gkPNZ/OiXQhtiqRMdLrL5Zw580zgb9082jkZQqP71Fk= git.hlsq.asia/mmorpg/service-common v0.0.0-20260116155014-3de703eb83c5 h1:b7pTgEyYUKpHtAAt4ea/3EYfX0Y0lYwhjrgf3kTyn0w=
git.hlsq.asia/mmorpg/service-common v0.0.0-20260116143417-d944729ad0e5/go.mod h1:Dazg+4woCv9Jk7jgT2qUSGWhZOXx/0WYfJO+FCUDyhw= git.hlsq.asia/mmorpg/service-common v0.0.0-20260116155014-3de703eb83c5/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

@@ -7,13 +7,20 @@ import (
var ( var (
OnlineUsersGauge prometheus.Gauge OnlineUsersGauge prometheus.Gauge
FlowCounter prometheus.Counter
) )
func init() { func init() {
log.Infof("Init prometheus metric...") log.Infof("Init prometheus metric...")
OnlineUsersGauge = prometheus.NewGauge(prometheus.GaugeOpts{ OnlineUsersGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "online_users", Name: "gateway_online_users",
Help: "Total number of online users", Help: "Total number of online users",
}) })
prometheus.MustRegister(OnlineUsersGauge) prometheus.MustRegister(OnlineUsersGauge)
FlowCounter = prometheus.NewCounter(prometheus.CounterOpts{
Name: "gateway_flow_counter",
Help: "Total number of flow",
})
prometheus.MustRegister(FlowCounter)
} }

View File

@@ -5,6 +5,7 @@ import (
"git.hlsq.asia/mmorpg/service-common/log" "git.hlsq.asia/mmorpg/service-common/log"
"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-common/proto/ss/ss_pb" "git.hlsq.asia/mmorpg/service-common/proto/ss/ss_pb"
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/client" "git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/client"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
"sync" "sync"
@@ -39,8 +40,10 @@ func (s *Server) ToClient(server grpc_pb.Gateway_ToClientServer) error {
log.Errorf("ToClient proto.Marshal error: %v", err) log.Errorf("ToClient proto.Marshal error: %v", err)
continue continue
} }
dataLen := float64(len(data))
for _, cli := range client.UserMgr.GetAll() { for _, cli := range client.UserMgr.GetAll() {
cli.WriteBytesPreMarshal(data) cli.WriteBytesPreMarshal(data)
global.FlowCounter.Add(dataLen)
} }
//for _, client := range ws_handler.UserMgr.GetAll() { //for _, client := range ws_handler.UserMgr.GetAll() {
@@ -49,6 +52,7 @@ func (s *Server) ToClient(server grpc_pb.Gateway_ToClientServer) error {
} else { } else {
if cli := client.UserMgr.GetByUSN(args.USN); cli != nil { if cli := client.UserMgr.GetByUSN(args.USN); cli != nil {
cli.WriteBytes(ss_pb.MessageID(args.MessageID), args.Payload) cli.WriteBytes(ss_pb.MessageID(args.MessageID), args.Payload)
global.FlowCounter.Add(float64(len(args.Payload)))
} }
} }
} }

View File

@@ -21,11 +21,16 @@ func NewServer(ttl int64) *Server {
EtcdTTL: ttl, EtcdTTL: ttl,
}, },
} }
s.Base.OnCustomGrpcServerOption = s.OnCustomGrpcServerOption
s.Base.OnInit = s.OnInit s.Base.OnInit = s.OnInit
s.Base.OnClose = s.OnClose s.Base.OnClose = s.OnClose
return s return s
} }
func (s *Server) OnCustomGrpcServerOption() []grpc.ServerOption {
return nil
}
func (s *Server) OnInit(serve *grpc.Server) { func (s *Server) OnInit(serve *grpc.Server) {
client.GatewaySID = s.SID client.GatewaySID = s.SID
grpc_pb.RegisterGatewayServer(serve, s) grpc_pb.RegisterGatewayServer(serve, s)