Home >Backend Development >Golang >Chain custom GRPC client interceptor/DialOptions

Chain custom GRPC client interceptor/DialOptions

PHPz
PHPzforward
2024-02-14 08:27:10897browse

链自定义 GRPC 客户端拦截器/DialOptions

Custom GRPC client interceptors and DialOptions are very useful tools when communicating over the network. These tools can help developers add additional functionality and processing logic to the GRPC client to meet specific needs. In this article, PHP editor Banana will introduce how to use these tools to customize and optimize the behavior of the GRPC client. By using these interceptors and DialOptions, developers can easily implement custom request and response processing, connection management and other functions, thereby improving the scalability and performance of the system. Let’s explore these powerful features together!

Question content

I want to link some DialOptions/client-side interceptors. But for some reason only the latest custom interceptor is called:

CB5C9B4EECA35A2077063ECDCD731918

I added TransportCredentials so there are no errors on startup (about missing transport security).

What am I missing here?

Workaround

You must chain the (client | server) interceptor:

Seegrpc.WithChainUnaryInterceptor

For example:

func main() {
    myInt1 := func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
        print("testInterceptor invoked")
        return invoker(ctx, method, req, reply, cc, opts...)
    }
    myInt2 := func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
        print("testInterceptor2 invoked")
        return invoker(ctx, method, req, reply, cc, opts...)
    }
    opts := []grpc.DialOption{
        grpc.WithTransportCredentials(insecure.NewCredentials()),
        grpc.WithChainUnaryInterceptor(
            myInt1,
            myInt2,
        ),
    }

    _, err := grpc.DialContext(context.Background(), "my-adress:443", opts...)
    if err != nil {
        log.Fatal(err)
    }
}

The above is the detailed content of Chain custom GRPC client interceptor/DialOptions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete