-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrpc.go
43 lines (39 loc) · 1.32 KB
/
grpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package panicrecovery
import (
"context"
"log"
"runtime/debug"
"google.golang.org/grpc"
)
// RecoverInterceptor is a gRPC interceptor that recovers from panics in gRPC methods.
func RecoverInterceptor() grpc.UnaryServerInterceptor {
return func(
ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
) (interface{}, error) {
defer func() {
if err := recover(); err != nil {
// Log the panic error with stack trace.
log.Printf("Recovered from panic in gRPC method %s: %v\nStack Trace: %s", info.FullMethod, err, string(debug.Stack()))
}
}()
// Call the handler to execute the RPC method
return handler(ctx, req)
}
}
// RecoverStreamInterceptor is a gRPC interceptor that recovers from panics in gRPC streaming methods.
func RecoverStreamInterceptor() grpc.StreamServerInterceptor {
return func(
srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo,
handler grpc.StreamHandler,
) error {
defer func() {
if err := recover(); err != nil {
// Log the panic error with stack trace for stream-based methods.
log.Printf("Recovered from panic in gRPC streaming method %s: %v\nStack Trace: %s", info.FullMethod, err, string(debug.Stack()))
}
}()
// Call the handler to execute the streaming RPC method
return handler(srv, stream)
}
}