Files
full/module/ec/market/internal/server/new.go

57 lines
1.5 KiB
Go
Raw Normal View History

// Code generated by protoc-gen-slc. DO NOT EDIT.
package server
import (
pb "bsm/full/module/ec/market/pb"
"context"
2026-09-22 21:15:34 +08:00
"git.apinb.com/bsm-sdk/core/printer"
gwRuntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
2026-09-22 21:15:34 +08:00
"google.golang.org/grpc/codes"
"google.golang.org/grpc/reflection"
2026-09-22 21:15:34 +08:00
"google.golang.org/grpc/status"
)
2026-09-22 21:15:34 +08:00
// recoverUnaryInterceptor 捕获处理过程中的 panic转为 Internal 错误返回,避免进程崩溃。
func recoverUnaryInterceptor(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
defer func() {
if r := recover(); r != nil {
printer.Error("grpc panic recovered: %v", r)
err = status.Error(codes.Internal, "internal server error")
}
}()
return handler(ctx, req)
}
type Server struct {
Grpc *grpc.Server
Ctx context.Context
Mux *gwRuntime.ServeMux
grpcConns map[string]*grpc.ClientConn // 连接池
}
func New(grpcServ *grpc.Server) *Server {
standalone := grpcServ == nil
if standalone {
2026-09-22 21:15:34 +08:00
grpcServ = grpc.NewServer(grpc.UnaryInterceptor(recoverUnaryInterceptor))
}
srv := &Server{
Ctx: context.Background(),
Grpc: grpcServ,
grpcConns: make(map[string]*grpc.ClientConn),
}
// register service to grpc.Server
pb.RegisterAgencyServer(srv.Grpc, NewAgencyServer())
pb.RegisterDataServer(srv.Grpc, NewDataServer())
pb.RegisterSupplyServer(srv.Grpc, NewSupplyServer())
if standalone {
reflection.Register(srv.Grpc)
}
return srv
}