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 interceptors #276

Merged
merged 6 commits into from
Sep 25, 2020
Merged

Add interceptors #276

merged 6 commits into from
Sep 25, 2020

Conversation

bufdev
Copy link
Contributor

@bufdev bufdev commented Sep 17, 2020

Implements #265

@@ -28,7 +28,7 @@ test_python_client: generate build/clientcompat build/pycompat

setup:
./install_proto.bash
GOPATH=$(CURDIR)/_tools go install github.com/twitchtv/retool/...
GO111MODULE=off GOPATH=$(CURDIR)/_tools GOBIN=$(CURDIR)/_tools/bin go get github.com/twitchtv/retool
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 is needed for any development system that has GO111MODULE and GOBIN set, and has no effect on other systems for Twirp's Makefile logic.

Copy link
Contributor

@marioizquierdo marioizquierdo left a comment

Choose a reason for hiding this comment

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

Nice. It is looking good 👍
Looking forward to complete the implementation on the client side.

protoc-gen-twirp/generator.go Show resolved Hide resolved
protoc-gen-twirp/generator.go Outdated Show resolved Hide resolved
middleware_test.go Outdated Show resolved Hide resolved
middleware.go Outdated Show resolved Hide resolved
middleware.go Outdated

// ChainInterceptors chains the Interceptors.
//
// Returns nil if interceptors is empty.
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's remember to add more comments explaining Method, Interceptor and ChainInterceptors. The extended comments should contain some basic example. We should also add a new page for interceptors in the docs, and make sure it is linked from docs for Hooks, and from other pages that mention hooks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a bit more Godoc, and a skeleton for the docs folder - could you propose some more language you'd like to add here? Would be helpful.

middleware_test.go Outdated Show resolved Hide resolved
@@ -574,6 +575,48 @@ func TestErroringHooks(t *testing.T) {
})
}

func TestInterceptor(t *testing.T) {
interceptor := func(next twirp.Method) twirp.Method {
return func(ctx context.Context, request interface{}) (interface{}, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

verify that the context contains info about the request:

methodName, _ := twirp.MethodName(ctx)
if methodName != "MakeHat" {
  return nil, fmt.Errorf("unexpected methodName: %q", methodName)
}
serviceName, _ := twirp.ServiceName(ctx)
if serviceName != "Haberdasher" {
  return nil, fmt.Errorf("unexpected serviceName: %q", serviceName)
}
packageName, _ := twirp.PackageName(ctx)
if packageName != "twirp.internal.twirptest" {
  return nil, fmt.Errorf("unexpected packageName: %q", packageName)
}

later, after calling the service, we should also verify that the context was annotated with the status code:

response, err := next(ctx, request)

status, ok := twirp.StatusCode(ctx)
if status != "200" || !ok {
    return nil, fmt.Errorf("unexpected status in context: %q", status)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added the first part, but the second part (status on context) isn't how contexts work - contexts are immutable, and we'd have to return a new context to do this (which isn't standard). I'd expect to get the status code off of the error, not the context, and all other middleware of this variety in the Golang ecosystem does the same.

Copy link

Choose a reason for hiding this comment

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

good idea, thanks. Getting statusCode from context - doesn't work.
This is how I did:

twerr, ok := err.(twirp.Error)
if ok {
	statusCode = twirp.ServerHTTPStatusFromErrorCode(twerr.Code())
}

internal/twirptest/service_test.go Show resolved Hide resolved
@bufdev
Copy link
Contributor Author

bufdev commented Sep 18, 2020

Addressed comments, will start on client interceptors now.

@bufdev bufdev changed the title WIP: Add interceptors Add interceptors Sep 18, 2020
@bufdev
Copy link
Contributor Author

bufdev commented Sep 18, 2020

Update: the client interceptors are now added, including testing.

Copy link
Contributor

@marioizquierdo marioizquierdo left a comment

Choose a reason for hiding this comment

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

Looks good to me. The initial documentation looks good, we can edit later.

@marioizquierdo
Copy link
Contributor

Will merge in master with the release v7.1 soon. Great job! 👍

@marioizquierdo marioizquierdo merged commit 403a70c into twitchtv:master Sep 25, 2020
@bufdev bufdev deleted the interceptors branch September 25, 2020 17:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants