-
Notifications
You must be signed in to change notification settings - Fork 280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add rpc panic recovery middleware #240
Changes from all commits
fa9f503
a1c9886
e7cb200
d71bcf9
e1c28fa
c4574f3
1352997
cc4b154
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,9 +27,13 @@ import ( | |
"go.uber.org/fx/modules/rpc/internal/stats" | ||
"go.uber.org/fx/service" | ||
"go.uber.org/fx/ulog" | ||
|
||
"github.com/pkg/errors" | ||
"go.uber.org/yarpc/api/transport" | ||
) | ||
|
||
const _panicResponse = "Server Error" | ||
|
||
type contextInboundMiddleware struct { | ||
service.Host | ||
} | ||
|
@@ -88,3 +92,27 @@ func authorize(ctx context.Context, host service.Host) (context.Context, error) | |
} | ||
return ctx, nil | ||
} | ||
|
||
type panicInboundMiddleware struct{} | ||
|
||
func (p panicInboundMiddleware) Handle(ctx context.Context, req *transport.Request, resw transport.ResponseWriter, handler transport.UnaryHandler) error { | ||
defer panicRecovery(ctx) | ||
return handler.Handle(ctx, req, resw) | ||
} | ||
|
||
type panicOnewayInboundMiddleware struct{} | ||
|
||
func (p panicOnewayInboundMiddleware) HandleOneway(ctx context.Context, req *transport.Request, handler transport.OnewayHandler) error { | ||
defer panicRecovery(ctx) | ||
return handler.HandleOneway(ctx, req) | ||
} | ||
|
||
func panicRecovery(ctx context.Context) { | ||
if err := recover(); err != nil { | ||
stats.RPCPanicCounter.Inc(1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see where this counter update is tested.. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there way to test metrics count/timer is called? I would like to test in #251 as well. Maybe you can point me somewhere metrics is tested. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is an example: |
||
ulog.Logger(ctx).Error("Panic recovered serving request", "error", errors.Errorf("panic in handler: %+v", err)) | ||
// rethrow panic back to yarpc | ||
// before https://github.com/yarpc/yarpc-go/issues/734 fixed, throw a generic error. | ||
panic(_panicResponse) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,10 +126,12 @@ func (c *dispatcherController) addDefaultMiddleware(host service.Host) { | |
cfg := yarpcConfig{ | ||
inboundMiddleware: []middleware.UnaryInbound{ | ||
contextInboundMiddleware{host}, | ||
panicInboundMiddleware{}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think panic middleware should be the very first in the slice, to catch all possible issues. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but then we don't have the logger with host in context There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, ok |
||
authInboundMiddleware{host}, | ||
}, | ||
onewayInboundMiddleware: []middleware.OnewayInbound{ | ||
contextOnewayInboundMiddleware{host}, | ||
panicOnewayInboundMiddleware{}, | ||
authOnewayInboundMiddleware{host}, | ||
}, | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this follows the naming convention with
contextInboundMiddleware
andauthInboundMiddleware
right nowmaybe change all of them in another diff?