Skip to content

Commit

Permalink
build(deps): update grpc middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
criyle committed Jan 25, 2025
1 parent 5de715c commit 58989da
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 118 deletions.
56 changes: 47 additions & 9 deletions cmd/go-judge/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,19 @@ import (
"github.com/criyle/go-judge/worker"
ginzap "github.com/gin-contrib/zap"
"github.com/gin-gonic/gin"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/auth"
grpc_logging "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/recovery"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
ginprometheus "github.com/zsais/go-gin-prometheus"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zapgrpc"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/status"
)

Expand Down Expand Up @@ -345,16 +346,53 @@ func initDebugRoute(mux *http.ServeMux) {
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
}

func InterceptorLogger(l *zap.Logger) grpc_logging.Logger {
return grpc_logging.LoggerFunc(func(ctx context.Context, lvl grpc_logging.Level, msg string, fields ...any) {
f := make([]zap.Field, 0, len(fields)/2)

for i := 0; i < len(fields); i += 2 {
key := fields[i]
value := fields[i+1]

switch v := value.(type) {
case string:
f = append(f, zap.String(key.(string), v))
case int:
f = append(f, zap.Int(key.(string), v))
case bool:
f = append(f, zap.Bool(key.(string), v))
default:
f = append(f, zap.Any(key.(string), v))
}
}

logger := l.WithOptions(zap.AddCallerSkip(1)).With(f...)

switch lvl {
case grpc_logging.LevelDebug:
logger.Debug(msg)
case grpc_logging.LevelInfo:
logger.Info(msg)
case grpc_logging.LevelWarn:
logger.Warn(msg)
case grpc_logging.LevelError:
logger.Error(msg)
default:
panic(fmt.Sprintf("unknown level %v", lvl))
}
})
}

func newGRPCServer(conf *config.Config, esServer pb.ExecutorServer) *grpc.Server {
grpc_zap.ReplaceGrpcLoggerV2(logger)
grpclog.SetLoggerV2(zapgrpc.NewLogger(logger))
streamMiddleware := []grpc.StreamServerInterceptor{
grpc_prometheus.StreamServerInterceptor,
grpc_zap.StreamServerInterceptor(logger),
grpc_logging.StreamServerInterceptor(InterceptorLogger(logger)),
grpc_recovery.StreamServerInterceptor(),
}
unaryMiddleware := []grpc.UnaryServerInterceptor{
grpc_prometheus.UnaryServerInterceptor,
grpc_zap.UnaryServerInterceptor(logger),
grpc_logging.UnaryServerInterceptor(InterceptorLogger(logger)),
grpc_recovery.UnaryServerInterceptor(),
}
if conf.AuthToken != "" {
Expand All @@ -363,8 +401,8 @@ func newGRPCServer(conf *config.Config, esServer pb.ExecutorServer) *grpc.Server
unaryMiddleware = append(unaryMiddleware, grpc_auth.UnaryServerInterceptor(authFunc))
}
grpcServer := grpc.NewServer(
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(streamMiddleware...)),
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(unaryMiddleware...)),
grpc.ChainStreamInterceptor(streamMiddleware...),
grpc.ChainUnaryInterceptor(unaryMiddleware...),
grpc.MaxRecvMsgSize(int(conf.GRPCMsgSize.Byte())),
)
pb.RegisterExecutorServer(grpcServer, esServer)
Expand Down
2 changes: 1 addition & 1 deletion env/mount_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/criyle/go-sandbox/container"
"github.com/criyle/go-sandbox/pkg/mount"
"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"
)

// Mount defines single mount point configuration.
Expand Down
10 changes: 4 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/godbus/dbus/v5 v5.1.0
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/gorilla/websocket v1.5.3
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.2.0
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/koding/multiconfig v0.0.0-20171124222453-69c27309b2d7
github.com/prometheus/client_golang v1.20.5
Expand All @@ -25,11 +25,11 @@ require (
golang.org/x/term v0.28.0
google.golang.org/grpc v1.70.0
google.golang.org/protobuf v1.36.4
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
)

require (
cloud.google.com/go/compute/metadata v0.5.2 // indirect
cloud.google.com/go/compute/metadata v0.6.0 // indirect
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bytedance/sonic v1.12.7 // indirect
Expand All @@ -44,7 +44,6 @@ require (
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.24.0 // indirect
github.com/goccy/go-json v0.10.4 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/klauspost/cpuid/v2 v2.2.9 // indirect
Expand All @@ -64,9 +63,8 @@ require (
golang.org/x/arch v0.13.0 // indirect
golang.org/x/crypto v0.32.0 // indirect
golang.org/x/text v0.21.0 // indirect
google.golang.org/genproto v0.0.0-20250124145028-65684f501c47 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250124145028-65684f501c47 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

retract (
Expand Down
Loading

0 comments on commit 58989da

Please sign in to comment.