123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- // Code generated by goctl. DO NOT EDIT!
- // Source: flowsrv.proto
- package server
- import (
- "context"
- "encoding/json"
- "github.com/Shopify/sarama"
- "github.com/zeromicro/go-zero/core/logx"
- gozerotrace "github.com/zeromicro/go-zero/core/trace"
- "go.opentelemetry.io/otel"
- "go.opentelemetry.io/otel/attribute"
- "go.opentelemetry.io/otel/propagation"
- oteltrace "go.opentelemetry.io/otel/trace"
- "net/http"
- "ylink/comm/kafka"
- "ylink/comm/model"
- "ylink/comm/trace"
- "ylink/comm/utils"
- "ylink/flowsrv/rpc/internal/logic"
- "ylink/flowsrv/rpc/internal/svc"
- "ylink/flowsrv/rpc/pb"
- )
- type FlowsrvServer struct {
- svcCtx *svc.ServiceContext
- pb.UnimplementedFlowsrvServer
- ConsumerGroup *kafka.ConsumerGroup
- }
- func NewFlowsrvServer(svcCtx *svc.ServiceContext) *FlowsrvServer {
- server := &FlowsrvServer{
- svcCtx: svcCtx,
- ConsumerGroup: kafka.NewConsumerGroup(&kafka.ConsumerGroupConfig{
- KafkaVersion: sarama.V1_0_0_0,
- OffsetsInitial: sarama.OffsetNewest,
- IsReturnErr: false,
- },
- svcCtx.Config.KqMsgBoxConsumerConf.Brokers,
- []string{svcCtx.Config.KqMsgBoxConsumerConf.Topic},
- svcCtx.Config.KqMsgBoxConsumerConf.GroupId),
- }
- server.subscribe()
- return server
- }
- func (s *FlowsrvServer) Connect(in *pb.CommandReq, stream pb.Flowsrv_ConnectServer) error {
- l := logic.NewConnectLogic(stream.Context(), s.svcCtx)
- return l.Connect(in, stream)
- }
- func (s *FlowsrvServer) Disconnect(ctx context.Context, in *pb.CommandReq) (*pb.CommandResp, error) {
- l := logic.NewDisconnectLogic(ctx, s.svcCtx)
- return l.Disconnect(in)
- }
- func (s *FlowsrvServer) Setup(_ sarama.ConsumerGroupSession) error {
- return nil
- }
- func (s *FlowsrvServer) Cleanup(_ sarama.ConsumerGroupSession) error {
- return nil
- }
- func (s *FlowsrvServer) ConsumeClaim(sess sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
- for msg := range claim.Messages() {
- if msg.Topic == s.svcCtx.Config.KqMsgBoxConsumerConf.Topic {
- s.handleMessage(sess, msg)
- }
- }
- return nil
- }
- func (s *FlowsrvServer) runWithCtx(callback func(ctx context.Context), kv ...attribute.KeyValue) {
- propagator := otel.GetTextMapPropagator()
- tracer := otel.GetTracerProvider().Tracer(gozerotrace.TraceName)
- ctx := propagator.Extract(context.Background(), propagation.HeaderCarrier(http.Header{}))
- spanName := utils.CallerFuncName()
- spanCtx, span := tracer.Start(ctx, spanName, oteltrace.WithSpanKind(oteltrace.SpanKindServer), oteltrace.WithAttributes(kv...))
- defer span.End()
- propagator.Inject(spanCtx, propagation.HeaderCarrier(http.Header{}))
- callback(spanCtx)
- }
- func (s *FlowsrvServer) handleMessage(sess sarama.ConsumerGroupSession, msg *sarama.ConsumerMessage) {
- traceId := kafka.GetTraceFromHeader(msg.Headers)
- if len(traceId) == 0 {
- return
- }
- trace.RunOnTracing(traceId, func(ctx context.Context) {
- var message model.KqMessage
- if err := json.Unmarshal(msg.Value, &message); err != nil {
- logx.Errorf("unmarshal msg error: %v", err)
- return
- }
- trace.StartTrace(ctx, "FlowsrvServer.handleMessage.SendMessage", func(ctx context.Context) {
- //if len(message.ReceiverId) == 0 || message.ReceiverId == "" {
- // // 玩家发的消息
- // p2cMap := ext.IdMap.Get(message.GameId).(*treemap.Map)
- // message.ReceiverId = p2cMap.Get(message.SenderId).(string)
- // logx.Infof("receiver: %s", message.ReceiverId)
- // b, _ := json.Marshal(message)
- // s.svcCtx.KqMsgBoxProducer.SendMessage(ctx, string(b), message.ReceiverId)
- //} else {
- // s.svcCtx.KqMsgBoxProducer.SendMessage(ctx, string(msg.Value), message.ReceiverId)
- //}
- logx.WithContext(ctx).Infof("headers: %v", msg.Headers)
- logx.WithContext(ctx).Infof("traceId: %s", msg.Headers[0].Value)
- logx.WithContext(ctx).Infof("flowsrv recv message: %v", string(msg.Value))
- sess.MarkMessage(msg, "")
- }, attribute.String("msg.key", string(msg.Key)))
- })
- //s.runWithCtx(func(ctx context.Context) {
- // var message model.KqCmdMessage
- // if err := json.Unmarshal(msg.Value, &message); err != nil {
- // logx.WithContext(ctx).Errorf("unmarshal msg error: %v", err)
- // return
- // }
- // trace.StartTrace(ctx, "FlowsrvServer.handleMessage.SendMessage", func(ctx context.Context) {
- // //if len(message.ReceiverId) == 0 || message.ReceiverId == "" {
- // // // 玩家发的消息
- // // p2cMap := ext.IdMap.Get(message.GameId).(*treemap.Map)
- // // message.ReceiverId = p2cMap.Get(message.SenderId).(string)
- // // logx.Infof("receiver: %s", message.ReceiverId)
- // // b, _ := json.Marshal(message)
- // // s.svcCtx.KqMsgBoxProducer.SendMessage(ctx, string(b), message.ReceiverId)
- // //} else {
- // // s.svcCtx.KqMsgBoxProducer.SendMessage(ctx, string(msg.Value), message.ReceiverId)
- // //}
- // logx.WithContext(ctx).Infof("flowsrv recv message: %v", string(msg.Value))
- // sess.MarkMessage(msg, "")
- // }, attribute.String("msg.key", string(msg.Key)))
- //})
- }
- func (s *FlowsrvServer) subscribe() {
- go s.ConsumerGroup.RegisterHandleAndConsumer(s)
- }
|