Initial QSfera import
This commit is contained in:
+15
@@ -0,0 +1,15 @@
|
||||
// Copyright 2017 David Ackroyd. All Rights Reserved.
|
||||
// See LICENSE for licensing terms.
|
||||
|
||||
/*
|
||||
`grpc_recovery` are interceptors that recover from gRPC handler panics.
|
||||
|
||||
Server Side Recovery Middleware
|
||||
|
||||
By default a panic will be converted into a gRPC error with `code.Internal`.
|
||||
|
||||
Handling can be customised by providing an alternate recovery function.
|
||||
|
||||
Please see examples for simple examples of use.
|
||||
*/
|
||||
package grpc_recovery
|
||||
Generated
Vendored
+62
@@ -0,0 +1,62 @@
|
||||
// Copyright 2017 David Ackroyd. All Rights Reserved.
|
||||
// See LICENSE for licensing terms.
|
||||
|
||||
package grpc_recovery
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
// RecoveryHandlerFunc is a function that recovers from the panic `p` by returning an `error`.
|
||||
type RecoveryHandlerFunc func(p interface{}) (err error)
|
||||
|
||||
// RecoveryHandlerFuncContext is a function that recovers from the panic `p` by returning an `error`.
|
||||
// The context can be used to extract request scoped metadata and context values.
|
||||
type RecoveryHandlerFuncContext func(ctx context.Context, p interface{}) (err error)
|
||||
|
||||
// UnaryServerInterceptor returns a new unary server interceptor for panic recovery.
|
||||
func UnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor {
|
||||
o := evaluateOptions(opts)
|
||||
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (_ interface{}, err error) {
|
||||
panicked := true
|
||||
|
||||
defer func() {
|
||||
if r := recover(); r != nil || panicked {
|
||||
err = recoverFrom(ctx, r, o.recoveryHandlerFunc)
|
||||
}
|
||||
}()
|
||||
|
||||
resp, err := handler(ctx, req)
|
||||
panicked = false
|
||||
return resp, err
|
||||
}
|
||||
}
|
||||
|
||||
// StreamServerInterceptor returns a new streaming server interceptor for panic recovery.
|
||||
func StreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor {
|
||||
o := evaluateOptions(opts)
|
||||
return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) (err error) {
|
||||
panicked := true
|
||||
|
||||
defer func() {
|
||||
if r := recover(); r != nil || panicked {
|
||||
err = recoverFrom(stream.Context(), r, o.recoveryHandlerFunc)
|
||||
}
|
||||
}()
|
||||
|
||||
err = handler(srv, stream)
|
||||
panicked = false
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func recoverFrom(ctx context.Context, p interface{}, r RecoveryHandlerFuncContext) error {
|
||||
if r == nil {
|
||||
return status.Errorf(codes.Internal, "%v", p)
|
||||
}
|
||||
return r(ctx, p)
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
// Copyright 2017 David Ackroyd. All Rights Reserved.
|
||||
// See LICENSE for licensing terms.
|
||||
|
||||
package grpc_recovery
|
||||
|
||||
import "context"
|
||||
|
||||
var (
|
||||
defaultOptions = &options{
|
||||
recoveryHandlerFunc: nil,
|
||||
}
|
||||
)
|
||||
|
||||
type options struct {
|
||||
recoveryHandlerFunc RecoveryHandlerFuncContext
|
||||
}
|
||||
|
||||
func evaluateOptions(opts []Option) *options {
|
||||
optCopy := &options{}
|
||||
*optCopy = *defaultOptions
|
||||
for _, o := range opts {
|
||||
o(optCopy)
|
||||
}
|
||||
return optCopy
|
||||
}
|
||||
|
||||
type Option func(*options)
|
||||
|
||||
// WithRecoveryHandler customizes the function for recovering from a panic.
|
||||
func WithRecoveryHandler(f RecoveryHandlerFunc) Option {
|
||||
return func(o *options) {
|
||||
o.recoveryHandlerFunc = RecoveryHandlerFuncContext(func(ctx context.Context, p interface{}) error {
|
||||
return f(p)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// WithRecoveryHandlerContext customizes the function for recovering from a panic.
|
||||
func WithRecoveryHandlerContext(f RecoveryHandlerFuncContext) Option {
|
||||
return func(o *options) {
|
||||
o.recoveryHandlerFunc = f
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user