mirror of
https://github.com/LazarenkoA/Ox.git
synced 2025-11-29 05:36:44 +02:00
better
This commit is contained in:
@@ -5,8 +5,9 @@ import (
|
||||
"fmt"
|
||||
grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
|
||||
"google.golang.org/grpc"
|
||||
"load_testing/worker/internal/utils"
|
||||
"load_testing/worker/proto/gen"
|
||||
"log"
|
||||
"log/slog"
|
||||
"net"
|
||||
)
|
||||
|
||||
@@ -17,7 +18,9 @@ func NewGRPCServer(ctx context.Context, port int, worker gen.WorkerServer) error
|
||||
}
|
||||
actualAddr := listener.Addr().String() // ip:port
|
||||
|
||||
srv := grpc.NewServer(grpc.ChainStreamInterceptor(grpc_recovery.StreamServerInterceptor()))
|
||||
logger := utils.Logger().With("name", "grpc")
|
||||
|
||||
srv := grpc.NewServer(grpc.ChainUnaryInterceptor(logInterceptor(logger)), grpc.ChainStreamInterceptor(grpc_recovery.StreamServerInterceptor()))
|
||||
gen.RegisterWorkerServer(srv, worker)
|
||||
|
||||
go func() {
|
||||
@@ -25,6 +28,19 @@ func NewGRPCServer(ctx context.Context, port int, worker gen.WorkerServer) error
|
||||
srv.Stop()
|
||||
}()
|
||||
|
||||
log.Println("сервер запущен на", actualAddr)
|
||||
logger.InfoContext(ctx, fmt.Sprintf("сервер запущен на %s", actualAddr))
|
||||
return srv.Serve(listener)
|
||||
}
|
||||
|
||||
func logInterceptor(logger *slog.Logger) func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
|
||||
return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
|
||||
_, err = handler(ctx, req)
|
||||
if err != nil {
|
||||
logger.ErrorContext(ctx, "grpc error", "error", err)
|
||||
} else {
|
||||
logger.InfoContext(ctx, fmt.Sprintf("grpc method %s", info.FullMethod))
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user