- Option">type Option
- RecoveryHandlerFunc">type RecoveryHandlerFunc
- 例子:
默认情况下,一个恐慌将转换成一个gRPC错误’ code.Internal ‘
处理可以通过提供替代恢复功能进行定制。
func StreamServerInterceptor(opts …Option) grpc.StreamServerInterceptor
func UnaryServerInterceptor(opts …Option) grpc.UnaryServerInterceptor
type Option
func WithRecoveryHandler(f RecoveryHandlerFunc) Option
type RecoveryHandlerFunc
type RecoveryHandlerFunc func(p interface{}) (err error)
例子:
var (
customFunc grpc_recovery.RecoveryHandlerFunc
)
// Initialization shows an initialization sequence with a custom recovery handler func.
func main() {
//定义customfunc来处理恐慌
customFunc = func(p interface{}) (err error) {
return status.Errorf(codes.Unknown, "panic triggered: %v", p)
}
opts := []grpc_recovery.Option{
grpc_recovery.WithRecoveryHandler(customFunc),
}
// 创建一个服务器。恢复处理程序通常位于链的最后,以便其他中间件
//(例如,日志记录)可以在恢复状态下进行操作,而不会直接受到任何恐慌的影响
_ = grpc.NewServer(
grpc_middleware.WithUnaryServerChain(
grpc_recovery.UnaryServerInterceptor(opts...),
),
grpc_middleware.WithStreamServerChain(
grpc_recovery.StreamServerInterceptor(opts...),
),
)
}