|
| 1 | +package grpc |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "maps" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/buildbarn/bb-storage/pkg/program" |
| 9 | + grpcpb "github.com/buildbarn/bb-storage/pkg/proto/configuration/grpc" |
| 10 | + "github.com/buildbarn/bb-storage/pkg/util" |
| 11 | + "github.com/jhump/protoreflect/v2/grpcreflect" |
| 12 | + "github.com/jhump/protoreflect/v2/protoresolve" |
| 13 | + v1reflectiongrpc "google.golang.org/grpc/reflection/grpc_reflection_v1" |
| 14 | + v1alphareflectiongrpc "google.golang.org/grpc/reflection/grpc_reflection_v1alpha" |
| 15 | + |
| 16 | + "google.golang.org/grpc" |
| 17 | + "google.golang.org/grpc/codes" |
| 18 | + "google.golang.org/grpc/reflection" |
| 19 | + "google.golang.org/grpc/status" |
| 20 | +) |
| 21 | + |
| 22 | +type combinedServiceInfoProvider struct { |
| 23 | + server reflection.ServiceInfoProvider |
| 24 | + extraServices map[string]grpc.ServiceInfo |
| 25 | +} |
| 26 | + |
| 27 | +var _ reflection.ServiceInfoProvider = (*combinedServiceInfoProvider)(nil) |
| 28 | + |
| 29 | +// GetServiceInfo returns the currently available services, which might have |
| 30 | +// changed since the creation of this reflection server. |
| 31 | +func (p *combinedServiceInfoProvider) GetServiceInfo() map[string]grpc.ServiceInfo { |
| 32 | + services := make(map[string]grpc.ServiceInfo) |
| 33 | + maps.Copy(services, p.extraServices) |
| 34 | + maps.Copy(services, p.server.GetServiceInfo()) |
| 35 | + return services |
| 36 | +} |
| 37 | + |
| 38 | +// registerReflection registers the google.golang.org/grpc/reflection/ service |
| 39 | +// on a grpc.Server and calls remote backends in case for relayed services. The |
| 40 | +// connections to the backend will run with the backendCtx. |
| 41 | +func registerReflection(backendCtx context.Context, s *grpc.Server, serverRelayConfiguration []*grpcpb.ServerRelayConfiguration, group program.Group, grpcClientFactory ClientFactory) error { |
| 42 | + // Accumulate all the service names. |
| 43 | + relayServices := make(map[string]grpc.ServiceInfo) |
| 44 | + for _, relay := range serverRelayConfiguration { |
| 45 | + for _, serviceMethod := range relay.Methods { |
| 46 | + if !strings.HasPrefix(serviceMethod, "/") { |
| 47 | + return status.Errorf(codes.InvalidArgument, "Malformed service method name %q, should start with '/'", serviceMethod) |
| 48 | + } |
| 49 | + pos := strings.LastIndex(serviceMethod, "/") |
| 50 | + if pos == -1 || pos == 0 { |
| 51 | + return status.Errorf(codes.InvalidArgument, "Malformed name %q, expected '/' between service and method", serviceMethod) |
| 52 | + } |
| 53 | + serviceName := serviceMethod[1:pos] |
| 54 | + // According to ServiceInfoProvider docs for ServerOptions.Services, |
| 55 | + // the reflection service is only interested in the service names. |
| 56 | + relayServices[serviceName] = grpc.ServiceInfo{} |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // Make a combined descriptor and extension resolver. |
| 61 | + reflectionBackends := []protoresolve.Resolver{} |
| 62 | + for relayIdx, relay := range serverRelayConfiguration { |
| 63 | + grpcClient, err := grpcClientFactory.NewClientFromConfiguration(relay.Endpoint, group) |
| 64 | + if err != nil { |
| 65 | + return util.StatusWrapf(err, "Failed to create relay RPC client %d", relayIdx+1) |
| 66 | + } |
| 67 | + resolver := grpcreflect.NewClientAuto(backendCtx, grpcClient).AsResolver() |
| 68 | + reflectionBackends = append(reflectionBackends, resolver) |
| 69 | + } |
| 70 | + combinedRemoteResolver := protoresolve.Combine(reflectionBackends...) |
| 71 | + |
| 72 | + serverOptions := reflection.ServerOptions{ |
| 73 | + Services: &combinedServiceInfoProvider{ |
| 74 | + server: s, |
| 75 | + extraServices: relayServices, |
| 76 | + }, |
| 77 | + DescriptorResolver: combinedRemoteResolver, |
| 78 | + ExtensionResolver: protoresolve.TypesFromDescriptorPool(combinedRemoteResolver), |
| 79 | + } |
| 80 | + v1reflectiongrpc.RegisterServerReflectionServer(s, reflection.NewServerV1(serverOptions)) |
| 81 | + v1alphareflectiongrpc.RegisterServerReflectionServer(s, reflection.NewServer(serverOptions)) |
| 82 | + return nil |
| 83 | +} |
0 commit comments