github.com/erda-project/erda-infra@v1.0.10-0.20240327085753-f3a249292aeb/pkg/transport/grpc/grpc.go (about)

     1  // Copyright (c) 2021 Terminus, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package grpc
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/erda-project/erda-infra/pkg/transport/interceptor"
    21  	"google.golang.org/grpc"
    22  	"google.golang.org/grpc/metadata"
    23  )
    24  
    25  // ServiceRegistrar wraps a single method that supports service registration. It
    26  // enables users to pass concrete types other than grpc.Server to the service
    27  // registration methods exported by the IDL generated code.
    28  //
    29  // Upward compatible for google.golang.org/grpc v1.28.0
    30  // and ServiceRegistrar define in google.golang.org/grpc v1.32.0+
    31  type ServiceRegistrar interface {
    32  	// RegisterService registers a service and its implementation to the
    33  	// concrete type implementing this interface.  It may not be called
    34  	// once the server has started serving.
    35  	// desc describes the service and its methods and handlers. impl is the
    36  	// service implementation which is passed to the method handlers.
    37  	RegisterService(desc *grpc.ServiceDesc, impl interface{})
    38  }
    39  
    40  // ClientConnInterface defines the functions clients need to perform unary and
    41  // streaming RPCs.  It is implemented by *ClientConn, and is only intended to
    42  // be referenced by generated code.
    43  //
    44  // Upward compatible for google.golang.org/grpc v1.26.0
    45  // and ClientConnInterface define in google.golang.org/grpc v1.28.0+
    46  type ClientConnInterface interface {
    47  	// Invoke performs a unary RPC and returns after the response is received
    48  	// into reply.
    49  	Invoke(ctx context.Context, method string, args interface{}, reply interface{}, opts ...grpc.CallOption) error
    50  	// NewStream begins a streaming RPC.
    51  	NewStream(ctx context.Context, desc *grpc.StreamDesc, method string, opts ...grpc.CallOption) (grpc.ClientStream, error)
    52  }
    53  
    54  // HandleOption is handle option.
    55  type HandleOption func(*HandleOptions)
    56  
    57  // HandleOptions is handle options.
    58  type HandleOptions struct {
    59  	Interceptor interceptor.Interceptor
    60  }
    61  
    62  // DefaultHandleOptions .
    63  func DefaultHandleOptions() *HandleOptions {
    64  	return &HandleOptions{}
    65  }
    66  
    67  // WithInterceptor .
    68  func WithInterceptor(o interceptor.Interceptor) HandleOption {
    69  	return func(opts *HandleOptions) {
    70  		if opts.Interceptor != nil {
    71  			opts.Interceptor = interceptor.Chain(opts.Interceptor, o)
    72  		} else {
    73  			opts.Interceptor = o
    74  		}
    75  	}
    76  }
    77  
    78  // CallOptionFromContext for client
    79  func CallOptionFromContext(ctx context.Context) []grpc.CallOption {
    80  	md, ok := metadata.FromOutgoingContext(ctx)
    81  	if ok && md != nil {
    82  		return []grpc.CallOption{grpc.Header(&md)}
    83  	}
    84  	return nil
    85  }