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

View File

@@ -7,13 +7,20 @@ import (
var (
OnlineUsersGauge prometheus.Gauge
FlowCounter prometheus.Counter
)
func init() {
log.Infof("Init prometheus metric...")
OnlineUsersGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "online_users",
Name: "gateway_online_users",
Help: "Total number of online users",
})
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/proto/rs/grpc_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"
"google.golang.org/protobuf/proto"
"sync"
@@ -39,8 +40,10 @@ func (s *Server) ToClient(server grpc_pb.Gateway_ToClientServer) error {
log.Errorf("ToClient proto.Marshal error: %v", err)
continue
}
dataLen := float64(len(data))
for _, cli := range client.UserMgr.GetAll() {
cli.WriteBytesPreMarshal(data)
global.FlowCounter.Add(dataLen)
}
//for _, client := range ws_handler.UserMgr.GetAll() {
@@ -49,6 +52,7 @@ func (s *Server) ToClient(server grpc_pb.Gateway_ToClientServer) error {
} else {
if cli := client.UserMgr.GetByUSN(args.USN); cli != nil {
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,
},
}
s.Base.OnCustomGrpcServerOption = s.OnCustomGrpcServerOption
s.Base.OnInit = s.OnInit
s.Base.OnClose = s.OnClose
return s
}
func (s *Server) OnCustomGrpcServerOption() []grpc.ServerOption {
return nil
}
func (s *Server) OnInit(serve *grpc.Server) {
client.GatewaySID = s.SID
grpc_pb.RegisterGatewayServer(serve, s)