Tag Archives: grpc

How to Add custom middleware for GRPC server

1. Objective:

a. To customize a middleware to capture global code 500 error (panic error) in grpc server.

b. Grpc middleware is different from HTTP gin middleware. Gin can use use use or handlerfunc to enable middleware, but grpc can’t. Here we use the go grpc middleware plug-in to demonstrate.

c. Grpc client is only an active calling interface, so it is unnecessary to be a middleware.

2. Writing middleware

Install plug-in dependencies:

go get github.com/grpc-ecosystem/go-grpc-middleware

All the codes of middleware, in which the return value type is fixed (the return value form of go grpc middleware plug-in is the maximum value form)

package middlewares

import (
	"context"
	"fmt"

	"google.golang.org/grpc"
)


// StreamGSError500 Catching Fatal Errors in Streaming Code
func StreamGSError500(address string) grpc.StreamServerInterceptor {
	return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) (err error) {
		
		fmt.Println("StreamGSError500 The service has been added to the listener===")
		defer func() {
			if err := recover(); err != nil {
				//Print error stack information
				fmt.Println(err)
				
			}
		}()

		err = handler(srv, stream)
		return err
	}
}

// UnaryGSError500 Catching fatal errors in simple code
func UnaryGSError500(address string) grpc.UnaryServerInterceptor {
	return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (_ interface{}, err error) {

		fmt.Println("UnaryGSError500 The service has been added to the listener===")
		defer func() {
			if err := recover(); err != nil {
				//Print error stack information
				fmt.Println(err)
				
			}
		}()

		resp, err := handler(ctx, req)
		return resp, err
	}
}

3. Add middleware when starting grpc server

introduce:

import (
    "github.com/grpc-ecosystem/go-grpc-middleware"
	grpcRecovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
	"google.golang.org/grpc"

)

Startup:

    var address string = "127.0.0.1:9600"


	// Instantiate the grpc server and insert the median price
	grpcServer := grpc.NewServer(
		grpc.StreamInterceptor(grpc_middleware.ChainStreamServer( // Stream Interceptor
			middlewares.StreamGSError500(address),
            grpcRecovery.StreamServerInterceptor(),
		)),
		grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer( // Simple Interceptor
			middlewares.UnaryGSError500(address),
            grpcRecovery.StreamServerInterceptor(),
		)),
	)

Python Grpc Error: A file with this name is already in the pool

1.Error:
rank.proto: A file with this name is already in the pool.

[E 210422 14:48:32 flask_server:87] grpc request execution failed, details: Couldn't build proto file into descriptor pool!
    Invalid proto descriptor for file "rank.proto":
      rank.proto: A file with this name is already in the pool.

2. Solutions

(1) Check the current protobuf version, assuming it is 3.12.2
pip3 show protobuf

(2)Uninstall this version
pip uninstall protobuf

(3)Use the following to reinstall the corresponding version
pip install --no-binary protobuf protobuf==3.12.2

Grpc client access server prompt: RPC error: code = unimplemented desc = unknown service possible reasons

Possible causes of specific errors and Solutions

Specific error

{"file":"/cmd/channel/xxx/prepay.go","level":"error","line":220,"msg":"xxx.DoPrepayGRPC error: xxx2Client.CreateOrder: rpc error: code = Unimplemented desc = unknown service xxx_global.xxx2","pid":63405,"requestid":"2_1615862450_86","sdkid":"xxx_global","time":"2021-03-16T10:40:50+08:00"}

Possible causes

1. The service called by the client is inconsistent with the service protocol of grpc;
2. When proto is consistent, the package name of proto is changed in the process of application generation.

Solution

1. You need to check whether the proto used by the client is consistent with that used by the service;
2. Check whether the package name of proto has changed.

Did not encounter other reasons leading to this error, know the students are welcome to leave a message in the comments area ha~