Skip to content
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

Merged
8 commits merged into from
Feb 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions modules/rpc/handler.go
Expand Up @@ -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
}
Expand Down Expand Up @@ -88,3 +92,27 @@ func authorize(ctx context.Context, host service.Host) (context.Context, error)
}
return ctx, nil
}

type panicInboundMiddleware struct{}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unary

Copy link
Contributor Author

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 and authInboundMiddleware right now
maybe change all of them in another diff?


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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see where this counter update is tested..

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
}
}
62 changes: 52 additions & 10 deletions modules/rpc/handler_test.go
Expand Up @@ -31,6 +31,7 @@ import (
"go.uber.org/yarpc/api/transport"

"github.com/stretchr/testify/assert"
"github.com/uber-go/tally"
)

type fakeEnveloper struct {
Expand All @@ -54,31 +55,31 @@ func TestInboundMiddleware_fxContext(t *testing.T) {
Host: service.NopHost(),
}
stats.SetupRPCMetrics(unary.Host.Metrics())
err := unary.Handle(context.Background(), &transport.Request{}, nil, &fakeUnaryHandler{t: t})
err := unary.Handle(context.Background(), &transport.Request{}, nil, &fakeUnary{t: t})
assert.Equal(t, "handle", err.Error())
}

func TestOnewayInboundMiddleware_fxContext(t *testing.T) {
oneway := contextOnewayInboundMiddleware{
Host: service.NopHost(),
}
err := oneway.HandleOneway(context.Background(), &transport.Request{}, &fakeOnewayHandler{t: t})
err := oneway.HandleOneway(context.Background(), &transport.Request{}, &fakeOneway{t: t})
assert.Equal(t, "oneway handle", err.Error())
}

func TestInboundMiddleware_auth(t *testing.T) {
unary := authInboundMiddleware{
Host: service.NopHost(),
}
err := unary.Handle(context.Background(), &transport.Request{}, nil, &fakeUnaryHandler{t: t})
err := unary.Handle(context.Background(), &transport.Request{}, nil, &fakeUnary{t: t})
assert.EqualError(t, err, "handle")
}

func TestInboundMiddleware_authFailure(t *testing.T) {
unary := authInboundMiddleware{
Host: service.NopHostAuthFailure(),
}
err := unary.Handle(context.Background(), &transport.Request{}, nil, &fakeUnaryHandler{t: t})
err := unary.Handle(context.Background(), &transport.Request{}, nil, &fakeUnary{t: t})
assert.EqualError(t, err, "Error authorizing the service")

}
Expand All @@ -87,32 +88,73 @@ func TestOnewayInboundMiddleware_auth(t *testing.T) {
oneway := authOnewayInboundMiddleware{
Host: service.NopHost(),
}
err := oneway.HandleOneway(context.Background(), &transport.Request{}, &fakeOnewayHandler{t: t})
err := oneway.HandleOneway(context.Background(), &transport.Request{}, &fakeOneway{t: t})
assert.EqualError(t, err, "oneway handle")
}

func TestOnewayInboundMiddleware_authFailure(t *testing.T) {
oneway := authOnewayInboundMiddleware{
Host: service.NopHostAuthFailure(),
}
err := oneway.HandleOneway(context.Background(), &transport.Request{}, &fakeOnewayHandler{t: t})
err := oneway.HandleOneway(context.Background(), &transport.Request{}, &fakeOneway{t: t})
assert.EqualError(t, err, "Error authorizing the service")
}

type fakeUnaryHandler struct {
func TestInboundMiddleware_panic(t *testing.T) {
host := service.NopHost()
testScope := host.Metrics()
stats.SetupRPCMetrics(testScope)

defer testPanicHandler(t, testScope)
unary := panicInboundMiddleware{}
unary.Handle(context.Background(), &transport.Request{}, nil, &alwaysPanicUnary{})
}

func TestOnewayInboundMiddleware_panic(t *testing.T) {
host := service.NopHost()
testScope := host.Metrics()
stats.SetupRPCMetrics(testScope)

defer testPanicHandler(t, testScope)
oneway := panicOnewayInboundMiddleware{}
oneway.HandleOneway(context.Background(), &transport.Request{}, &alwaysPanicOneway{})
}

func testPanicHandler(t *testing.T, testScope tally.Scope) {
r := recover()
assert.EqualValues(t, r, _panicResponse)

snapshot := testScope.(tally.TestScope).Snapshot()
counters := snapshot.Counters()
assert.True(t, counters["panic"].Value() > 0)
}

type fakeUnary struct {
t *testing.T
}

func (f fakeUnaryHandler) Handle(ctx context.Context, _param1 *transport.Request, _param2 transport.ResponseWriter) error {
func (f fakeUnary) Handle(ctx context.Context, _param1 *transport.Request, _param2 transport.ResponseWriter) error {
assert.NotNil(f.t, ctx)
return errors.New("handle")
}

type fakeOnewayHandler struct {
type fakeOneway struct {
t *testing.T
}

func (f fakeOnewayHandler) HandleOneway(ctx context.Context, p *transport.Request) error {
func (f fakeOneway) HandleOneway(ctx context.Context, p *transport.Request) error {
assert.NotNil(f.t, ctx)
return errors.New("oneway handle")
}

type alwaysPanicUnary struct{}

func (p alwaysPanicUnary) Handle(_ context.Context, _ *transport.Request, _ transport.ResponseWriter) error {
panic("panic")
}

type alwaysPanicOneway struct{}

func (p alwaysPanicOneway) HandleOneway(_ context.Context, _ *transport.Request) error {
panic("panic")
}
3 changes: 3 additions & 0 deletions modules/rpc/internal/stats/metrics.go
Expand Up @@ -46,11 +46,14 @@ var (
RPCAuthFailCounter tally.Counter
// RPCHandleTimer is a turnaround time for rpc handler
RPCHandleTimer tally.Scope
// RPCPanicCounter counts panics occurred for rpc handler
RPCPanicCounter tally.Counter
)

// SetupRPCMetrics allocates counters for necessary setup
func SetupRPCMetrics(scope tally.Scope) {
rpcTagsScope := scope.Tagged(RPCTags)
RPCAuthFailCounter = rpcTagsScope.Tagged(map[string]string{TagMiddleware: "auth"}).Counter("fail")
RPCHandleTimer = rpcTagsScope.Tagged(RPCTags)
RPCPanicCounter = rpcTagsScope.Counter("panic")
}
2 changes: 2 additions & 0 deletions modules/rpc/yarpc.go
Expand Up @@ -126,10 +126,12 @@ func (c *dispatcherController) addDefaultMiddleware(host service.Host) {
cfg := yarpcConfig{
inboundMiddleware: []middleware.UnaryInbound{
contextInboundMiddleware{host},
panicInboundMiddleware{},
Copy link

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but then we don't have the logger with host in context

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, ok

authInboundMiddleware{host},
},
onewayInboundMiddleware: []middleware.OnewayInbound{
contextOnewayInboundMiddleware{host},
panicOnewayInboundMiddleware{},
authOnewayInboundMiddleware{host},
},
}
Expand Down