JOJ3/internal/executor/sandbox/grpc.go
张泊明518370910136 153c261ab8
All checks were successful
submodules sync / sync (push) Successful in 39s
build / build (push) Successful in 1m19s
build / trigger-build-image (push) Successful in 7s
feat(executor): default 2 GiB grpc msg size
2024-11-06 07:46:44 -05:00

58 lines
1.4 KiB
Go

package sandbox
import (
"context"
"log/slog"
"math"
"github.com/criyle/go-judge/pb"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
)
// copied from https://github.com/criyle/go-judger-demo/blob/master/apigateway/main.go
func createExecClient(execServer, token string) (pb.ExecutorClient, error) {
conn, err := createGRPCConnection(execServer, token)
if err != nil {
slog.Error("gRPC connection", "error", err)
return nil, err
}
return pb.NewExecutorClient(conn), nil
}
func createGRPCConnection(addr, token string) (*grpc.ClientConn, error) {
// max size according to https://protobuf.dev/programming-guides/encoding/#size-limit
opts := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithDefaultCallOptions(
grpc.MaxCallRecvMsgSize(math.MaxInt32), // 2 GB
),
}
if token != "" {
opts = append(opts, grpc.WithPerRPCCredentials(newTokenAuth(token)))
}
return grpc.NewClient(addr, opts...)
}
type tokenAuth struct {
token string
}
func newTokenAuth(token string) credentials.PerRPCCredentials {
return &tokenAuth{token: token}
}
// Return value is mapped to request headers.
func (t *tokenAuth) GetRequestMetadata(ctx context.Context, in ...string) (
map[string]string, error,
) {
return map[string]string{
"authorization": "Bearer " + t.token,
}, nil
}
func (*tokenAuth) RequireTransportSecurity() bool {
return false
}