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(),
)),
)
Read More:
- Grpc client access server prompt: RPC error: code = unimplemented desc = unknown service possible reasons
- Hexo + next add bilibilibili icon through custom style
- How to set the custom blood bar UI in UE4 and how to call it
- Serverlet: How to Add, Delete, Modify and Query item code
- IDEA: How to Add @Data plug-in
- How to add directory plug-ins to jubyter notebook
- Tensorflow: How to use expand_Dim() to add dimensions
- 【Error】gRPC failure=Status{code=UNAVAILABLE, description=io exception, cause=io.netty.channel.
- How to Add migrations in Visual Studio for Mac
- How to Fix Failed to add the host to the list of known hosts
- (element UI component table) how to add a style to a table
- Add Samba user prompt failed to add entry for user
- Add samba user prompt Failed to add entry for user.
- How to Fix adb_server_notify: ADB server connection failed
- MySQL failed to add foreign key: SQL 1452 cannot add or update a child row:a foreign key constraint fails
- IntelliJ: How to Create Ktor Server
- How to clean up the disk space of Linux server
- How To Enable Telnet in Windows Server 2012
- Depending on the custom component jar package, unable to read meta data for class appears after startup
- Mybatis custom list collection parser