-
Notifications
You must be signed in to change notification settings - Fork 566
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move mockpachd's panic handler into a package; use in real and test p…
…achds
- Loading branch information
Showing
9 changed files
with
87 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
load("@rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "recovery", | ||
srcs = ["recovery.go"], | ||
importpath = "github.com/pachyderm/pachyderm/v2/src/internal/middleware/recovery", | ||
visibility = ["//src:__subpackages__"], | ||
deps = [ | ||
"@org_golang_google_grpc//:grpc", | ||
"@org_golang_google_grpc//codes", | ||
"@org_golang_google_grpc//status", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Package recovery implements a GRPC server interceptor that recovers from panicking RPCs. | ||
package recovery | ||
|
||
import ( | ||
"context" | ||
"runtime" | ||
|
||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
const ( | ||
PanicCode = codes.Aborted | ||
panicLen = 32768 | ||
) | ||
|
||
// UnaryServerInterceptor implements grpc.UnaryServerInterceptor. It recovers from a panicking RPC | ||
// handler. | ||
func UnaryServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, retErr error) { | ||
defer func() { | ||
if err := recover(); err != nil { | ||
stack := make([]byte, panicLen) | ||
n := runtime.Stack(stack, false) | ||
stack = stack[:n] | ||
retErr = status.Errorf(PanicCode, "panic: %v\n%s", err, stack) | ||
} | ||
}() | ||
return handler(ctx, req) | ||
} | ||
|
||
var _ grpc.UnaryServerInterceptor = UnaryServerInterceptor | ||
|
||
// StreamServerInterceptor implements grpc.StreamServerInterceptor. It recovers from a panicking | ||
// RPC handler. | ||
func StreamServerInterceptor(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) (retErr error) { | ||
defer func() { | ||
if err := recover(); err != nil { | ||
stack := make([]byte, panicLen) | ||
runtime.Stack(stack, false) | ||
retErr = status.Errorf(PanicCode, "panic: %v\n%s", err, stack) | ||
} | ||
}() | ||
return handler(srv, ss) | ||
} | ||
|
||
var _ grpc.StreamServerInterceptor = StreamServerInterceptor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters