...

Source file src/google.golang.org/grpc/interceptor.go

Documentation: google.golang.org/grpc

     1  /*
     2   *
     3   * Copyright 2016 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  package grpc
    20  
    21  import (
    22  	"context"
    23  )
    24  
    25  // UnaryInvoker is called by UnaryClientInterceptor to complete RPCs.
    26  type UnaryInvoker func(ctx context.Context, method string, req, reply any, cc *ClientConn, opts ...CallOption) error
    27  
    28  // UnaryClientInterceptor intercepts the execution of a unary RPC on the client.
    29  // Unary interceptors can be specified as a DialOption, using
    30  // WithUnaryInterceptor() or WithChainUnaryInterceptor(), when creating a
    31  // ClientConn. When a unary interceptor(s) is set on a ClientConn, gRPC
    32  // delegates all unary RPC invocations to the interceptor, and it is the
    33  // responsibility of the interceptor to call invoker to complete the processing
    34  // of the RPC.
    35  //
    36  // method is the RPC name. req and reply are the corresponding request and
    37  // response messages. cc is the ClientConn on which the RPC was invoked. invoker
    38  // is the handler to complete the RPC and it is the responsibility of the
    39  // interceptor to call it. opts contain all applicable call options, including
    40  // defaults from the ClientConn as well as per-call options.
    41  //
    42  // The returned error must be compatible with the status package.
    43  type UnaryClientInterceptor func(ctx context.Context, method string, req, reply any, cc *ClientConn, invoker UnaryInvoker, opts ...CallOption) error
    44  
    45  // Streamer is called by StreamClientInterceptor to create a ClientStream.
    46  type Streamer func(ctx context.Context, desc *StreamDesc, cc *ClientConn, method string, opts ...CallOption) (ClientStream, error)
    47  
    48  // StreamClientInterceptor intercepts the creation of a ClientStream. Stream
    49  // interceptors can be specified as a DialOption, using WithStreamInterceptor()
    50  // or WithChainStreamInterceptor(), when creating a ClientConn. When a stream
    51  // interceptor(s) is set on the ClientConn, gRPC delegates all stream creations
    52  // to the interceptor, and it is the responsibility of the interceptor to call
    53  // streamer.
    54  //
    55  // desc contains a description of the stream. cc is the ClientConn on which the
    56  // RPC was invoked. streamer is the handler to create a ClientStream and it is
    57  // the responsibility of the interceptor to call it. opts contain all applicable
    58  // call options, including defaults from the ClientConn as well as per-call
    59  // options.
    60  //
    61  // StreamClientInterceptor may return a custom ClientStream to intercept all I/O
    62  // operations. The returned error must be compatible with the status package.
    63  type StreamClientInterceptor func(ctx context.Context, desc *StreamDesc, cc *ClientConn, method string, streamer Streamer, opts ...CallOption) (ClientStream, error)
    64  
    65  // UnaryServerInfo consists of various information about a unary RPC on
    66  // server side. All per-rpc information may be mutated by the interceptor.
    67  type UnaryServerInfo struct {
    68  	// Server is the service implementation the user provides. This is read-only.
    69  	Server any
    70  	// FullMethod is the full RPC method string, i.e., /package.service/method.
    71  	FullMethod string
    72  }
    73  
    74  // UnaryHandler defines the handler invoked by UnaryServerInterceptor to complete the normal
    75  // execution of a unary RPC.
    76  //
    77  // If a UnaryHandler returns an error, it should either be produced by the
    78  // status package, or be one of the context errors. Otherwise, gRPC will use
    79  // codes.Unknown as the status code and err.Error() as the status message of the
    80  // RPC.
    81  type UnaryHandler func(ctx context.Context, req any) (any, error)
    82  
    83  // UnaryServerInterceptor provides a hook to intercept the execution of a unary RPC on the server. info
    84  // contains all the information of this RPC the interceptor can operate on. And handler is the wrapper
    85  // of the service method implementation. It is the responsibility of the interceptor to invoke handler
    86  // to complete the RPC.
    87  type UnaryServerInterceptor func(ctx context.Context, req any, info *UnaryServerInfo, handler UnaryHandler) (resp any, err error)
    88  
    89  // StreamServerInfo consists of various information about a streaming RPC on
    90  // server side. All per-rpc information may be mutated by the interceptor.
    91  type StreamServerInfo struct {
    92  	// FullMethod is the full RPC method string, i.e., /package.service/method.
    93  	FullMethod string
    94  	// IsClientStream indicates whether the RPC is a client streaming RPC.
    95  	IsClientStream bool
    96  	// IsServerStream indicates whether the RPC is a server streaming RPC.
    97  	IsServerStream bool
    98  }
    99  
   100  // StreamServerInterceptor provides a hook to intercept the execution of a streaming RPC on the server.
   101  // info contains all the information of this RPC the interceptor can operate on. And handler is the
   102  // service method implementation. It is the responsibility of the interceptor to invoke handler to
   103  // complete the RPC.
   104  type StreamServerInterceptor func(srv any, ss ServerStream, info *StreamServerInfo, handler StreamHandler) error
   105  

View as plain text