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 a register, so that the gRPC service can be invoked in-process to provide a HTTP server. #947

Merged
merged 14 commits into from Aug 28, 2019

Conversation

@hb-chen
Copy link
Contributor

commented Jun 14, 2019

sometimes we don't need the HTTP gateway to be a RPC.
just convert the gRPC service to an HTTP service.
this reduces one remote call.

example code

func main() {
	s := grpc.NewServer(
	)
	srv := service{}
	pb.RegisterExampleServiceServer(s, &srv)

	ctx := context.Background()
	ctx, cancel := context.WithCancel(ctx)
	defer cancel()

	mux := runtime.NewServeMux()
	err := pb.RegisterExampleServiceHandlerServer(ctx, mux, &srv)
	if err != nil {
		log.Panic(err)
	}

	if err := http.ListenAndServe(serveAddr, mux); err != nil {
		log.Fatalf("http failed to serve: %v", err)
	}
}
@googlebot

This comment has been minimized.

Copy link

commented Jun 14, 2019

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here (e.g. I signed it!) and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@googlebot googlebot added the cla: no label Jun 14, 2019

@johanbrandhorst

This comment has been minimized.

Copy link
Collaborator

commented Jun 14, 2019

Hi @hb-chen! Thanks for your PR. This is an interesting new feature! You'll need to regenerate all the example files with this template change: https://github.com/grpc-ecosystem/grpc-gateway/blob/master/CONTRIBUTING.md#i-want-to-regenerate-the-files-after-making-changes.

@hb-chen

This comment has been minimized.

Copy link
Contributor Author

commented Jun 15, 2019

I signed it!

@googlebot

This comment has been minimized.

Copy link

commented Jun 15, 2019

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@hb-chen

This comment has been minimized.

Copy link
Contributor Author

commented Jun 17, 2019

When I try to support stream, I see the PR(grpc/grpc-go#906 (comment)) of grpc-go, will support for In-Process transport.
So waiting the feature and close this PR.

And now I use buffconn. grpc/grpc-go#1263 (comment)

func main() {
	s := grpc.NewServer(
	)
	srv := service{}
	pb.RegisterExampleServiceServer(s, &srv)

	bcLis := bufconn.Listen(256 * 1024)
	go s.Serve(bcLis)

	ctx := context.Background()
	ctx, cancel := context.WithCancel(ctx)
	defer cancel()

	mux := runtime.NewServeMux()
	err := pb.RegisterExampleServiceHandlerFromEndpoint(
		ctx,
		mux,
		"",
		[]grpc.DialOption{
			grpc.WithContextDialer(func(ctx context.Context, s string) (net.Conn, error) {
				log.Printf("dial with s:%s", s)
				return bcLis.Dial()
			}),
		},
	)
	if err != nil {
		log.Panic(err)
	}

	if err := http.ListenAndServe(serveAddr, mux); err != nil {
		log.Fatalf("http failed to serve: %v", err)
	}
}

@hb-chen hb-chen closed this Jun 17, 2019

@johanbrandhorst

This comment has been minimized.

Copy link
Collaborator

commented Jun 17, 2019

@hb-chen The in-process transport is still some time away, and #952 shows there's still interest in this feature. Would you be interested to merge this anyway?

@hb-chen

This comment has been minimized.

Copy link
Contributor Author

commented Jun 17, 2019

@hb-chen The in-process transport is still some time away, and #952 shows there's still interest in this feature. Would you be interested to merge this anyway?

Of course, I will continue this😁

@johanbrandhorst

This comment has been minimized.

Copy link
Collaborator

commented Jun 17, 2019

Seems like the latest version of the generated files have some compilation errors - I assume this is due to a small problem in the template. Could you take another look please?

@nicklofaso

This comment has been minimized.

Copy link

commented Jun 25, 2019

+1 for this feature. Much appreciated

@rittneje

This comment has been minimized.

Copy link

commented Jun 25, 2019

+1
We would be fine even if this new function just didn’t get generated for interfaces using streams. 🙂

@johanbrandhorst

This comment has been minimized.

Copy link
Collaborator

commented Jun 25, 2019

@hb-chen I'd be happy to merge initial support for just unary requests. What do you think?

@hb-chen

This comment has been minimized.

Copy link
Contributor Author

commented Jun 28, 2019

@hb-chen I'd be happy to merge initial support for just unary requests. What do you think?

I agree with you.
Streaming used less in gateway, and we can wait for gRPC support.

@rittneje

This comment has been minimized.

Copy link

commented Jul 14, 2019

@hb-chen Any progress on this?

@vipingoel

This comment has been minimized.

Copy link

commented Jul 15, 2019

+1
Much awaited change.

@hb-chen

This comment has been minimized.

Copy link
Contributor Author

commented Jul 30, 2019

@hb-chen Any progress on this?

Just bazel check failed.
Or you can fork my branch, and build.

@achew22

This comment has been minimized.

Copy link
Collaborator

commented Jul 30, 2019

@hb-chen, thanks so much for doing this! Looks like I need to push an update to the rules_go stuff to make Bazel work. One moment please. Would you be willing to wait for that PR (I'll mention this one in it) and then rebase off master?

@achew22

This comment has been minimized.

Copy link
Collaborator

commented Jul 30, 2019

@hb-chen, if you could rebase off of master I think this will pass CI. Thanks so much!

@googlebot

This comment has been minimized.

Copy link

commented Jul 30, 2019

All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter.

We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only @googlebot I consent. in this pull request.

Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the cla label to yes (if enabled on your project).

ℹ️ Googlers: Go here for more info.

@googlebot googlebot added cla: no and removed cla: yes labels Jul 30, 2019

@hb-chen hb-chen reopened this Jul 30, 2019

@googlebot

This comment has been minimized.

Copy link

commented Jul 30, 2019

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@googlebot googlebot added cla: yes and removed cla: no labels Jul 30, 2019

protoc-gen-grpc-gateway/gengateway/template.go Outdated Show resolved Hide resolved
client := New{{$svc.GetName}}Client(conn)
{{end}}

This comment has been minimized.

Copy link
@johanbrandhorst

johanbrandhorst Jul 30, 2019

Collaborator

Move this newline before the {{end}} to avoid a newline at the top of non streaming services.

This comment has been minimized.

Copy link
@hb-chen

hb-chen Jul 31, 2019

Author Contributor

On line 579 have streaming condition {{if eq $streaming 1}}.

This comment has been minimized.

Copy link
@johanbrandhorst

johanbrandhorst Jul 31, 2019

Collaborator

Since we decided not to support streaming endpoints fort now, can we remove this?

hb-chen and others added 3 commits Jul 31, 2019
Update protoc-gen-grpc-gateway/gengateway/template.go
Co-Authored-By: Johan Brandhorst <johan.brandhorst@gmail.com>
Update protoc-gen-grpc-gateway/gengateway/template.go
Co-Authored-By: Johan Brandhorst <johan.brandhorst@gmail.com>
@johanbrandhorst
Copy link
Collaborator

left a comment

Let's remove the streaming specific code until it's implemented by gRPC.

client := New{{$svc.GetName}}Client(conn)
{{end}}

This comment has been minimized.

Copy link
@johanbrandhorst

johanbrandhorst Jul 31, 2019

Collaborator

Since we decided not to support streaming endpoints fort now, can we remove this?

// Register{{$svc.GetName}}{{$.RegisterFuncSuffix}}Server registers the http handlers for service {{$svc.GetName}} to "mux".
// UnaryRPC :call {{$svc.GetName}}Server directly.
// StreamingRPC :currently unsupported pending https://github.com/grpc/grpc-go/issues/906.
// If the gateway proto have stream must add DialOption grpc.WithContextDialer. e.g.

This comment has been minimized.

Copy link
@johanbrandhorst

johanbrandhorst Jul 31, 2019

Collaborator

Please remove the comments below this line.

@johanbrandhorst

This comment has been minimized.

Copy link
Collaborator

commented Aug 3, 2019

The new template changes look great, just a quick thought; do you think we could add a test case to the integration tests that calls the unary methods? It shouldn't be too hard, hopefully. Just a sanity test that the new functionality works.

@johanbrandhorst

This comment has been minimized.

Copy link
Collaborator

commented Aug 16, 2019

@hb-chen I don't want to lose sight of this, would you be able to write the tests or would you like to merge this as is?

@hb-chen

This comment has been minimized.

Copy link
Contributor Author

commented Aug 19, 2019

@hb-chen I don't want to lose sight of this, would you be able to write the tests or would you like to merge this as is?

tks, I will write tests.

@johanbrandhorst

This comment has been minimized.

Copy link
Collaborator

commented Aug 28, 2019

OK I see we have some tests, I think we can merge this!

@johanbrandhorst johanbrandhorst merged commit e70d128 into grpc-ecosystem:master Aug 28, 2019

7 checks passed

ci/circleci: bazel Your tests passed on CircleCI!
Details
ci/circleci: build Your tests passed on CircleCI!
Details
ci/circleci: generate Your tests passed on CircleCI!
Details
ci/circleci: lint Your tests passed on CircleCI!
Details
ci/circleci: node_test Your tests passed on CircleCI!
Details
ci/circleci: test Your tests passed on CircleCI!
Details
cla/google All necessary CLAs are signed
@hb-chen

This comment has been minimized.

Copy link
Contributor Author

commented Aug 30, 2019

func Register{{$svc.GetName}}{{$.RegisterFuncSuffix}}Server(ctx context.Context, mux *runtime.ServeMux, server {{$svc.GetName}}Server, opts []grpc.DialOption) error

Oops, the func have an error, opts []grpc.DialOption is unused.
It used for test/bufconn mode, but I forgot to remove it.
remove it and create new pr?

@johanbrandhorst

This comment has been minimized.

Copy link
Collaborator

commented Aug 30, 2019

Hm, it would be a backward compatibility breaking change at this point... but it's a bit nasty to require something that isn't used. Make a PR and let's merge it quickly 😅.

@achew22

This comment has been minimized.

Copy link
Collaborator

commented Aug 30, 2019

+1 if we do it quickly I think I'm okay breaking

@rittneje

This comment has been minimized.

Copy link

commented Sep 14, 2019

@johanbrandhorst It looks like @hb-chen's second PR got merged, so is this issue now officially closed?

@johanbrandhorst

This comment has been minimized.

Copy link
Collaborator

commented Sep 14, 2019

Did you mean another issue? This is a pull request, and it is merged already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants
You can’t perform that action at this time.