Skip to content

Add interceptors - #276

Merged
marioizquierdo merged 6 commits into
twitchtv:masterfrom
bufbuild:interceptors
Sep 25, 2020
Merged

marioizquierdo merged 6 commits into
twitchtv:masterfrom
bufbuild:interceptors

Conversation

@bufdev

@bufdev bufdev commented Sep 17, 2020 •

Copy link
Copy Markdown
Contributor

Implements #265

Comment thread Makefile
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
Copy Markdown
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.

@marioizquierdo marioizquierdo left a comment

Copy link
Copy Markdown
Contributor

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.

Comment thread protoc-gen-twirp/generator.go Resolved
Comment thread protoc-gen-twirp/generator.go Resolved
Comment thread middleware_test.go Resolved
Comment thread middleware.go Resolved
Comment thread middleware.go Outdated

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

Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread middleware_test.go Resolved

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

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

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())
}

Comment thread internal/twirptest/service_test.go Resolved
@bufdev

bufdev commented Sep 18, 2020

Copy link
Copy Markdown
Contributor Author

Addressed comments, will start on client interceptors now.

@bufdev bufdev changed the title WIP: Add interceptors Add interceptors Sep 18, 2020
@bufdev

bufdev commented Sep 18, 2020

Copy link
Copy Markdown
Contributor Author

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

@marioizquierdo marioizquierdo left a comment

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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

⌂ 换网址