From 7f26df41666be6cec500264e2df7315e6e925c4c Mon Sep 17 00:00:00 2001 From: Prem Nair Date: Tue, 21 Nov 2023 21:08:02 +0000 Subject: [PATCH] Migrate to connectrpc.com/connect --- buf.gen.yaml | 4 +- conn.go | 2 +- gen/greet/greet.pb.go | 2 +- gen/greet/greetconnect/greet.connect.go | 133 +++++++++++++++--------- go.mod | 3 +- go.sum | 4 + interceptor.go | 2 +- interceptor_test.go | 2 +- metrics_test.go | 2 +- 9 files changed, 96 insertions(+), 58 deletions(-) diff --git a/buf.gen.yaml b/buf.gen.yaml index ff98166..f5ba34a 100644 --- a/buf.gen.yaml +++ b/buf.gen.yaml @@ -1,11 +1,11 @@ version: v1 plugins: - - name: go + - plugin: buf.build/protocolbuffers/go:v1.31.0 out: gen opt: - module=github.com/easyCZ/connect-go-prometheus/gen - - name: connect-go + - plugin: buf.build/connectrpc/go:v1.12.0 out: gen opt: - module=github.com/easyCZ/connect-go-prometheus/gen diff --git a/conn.go b/conn.go index d806e8d..da27fa1 100644 --- a/conn.go +++ b/conn.go @@ -5,7 +5,7 @@ import ( "google.golang.org/protobuf/proto" - "github.com/bufbuild/connect-go" + "connectrpc.com/connect" ) type streamingConn struct { diff --git a/gen/greet/greet.pb.go b/gen/greet/greet.pb.go index 1ebcb03..3b0b58b 100644 --- a/gen/greet/greet.pb.go +++ b/gen/greet/greet.pb.go @@ -1,6 +1,6 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.28.1 +// protoc-gen-go v1.31.0 // protoc (unknown) // source: proto/greet.proto diff --git a/gen/greet/greetconnect/greet.connect.go b/gen/greet/greetconnect/greet.connect.go index ac4f44e..22854f1 100644 --- a/gen/greet/greetconnect/greet.connect.go +++ b/gen/greet/greetconnect/greet.connect.go @@ -5,9 +5,9 @@ package greetconnect import ( + connect "connectrpc.com/connect" context "context" errors "errors" - connect_go "github.com/bufbuild/connect-go" greet "github.com/easyCZ/connect-go-prometheus/gen/greet" http "net/http" strings "strings" @@ -18,19 +18,40 @@ import ( // generated with a version of connect newer than the one compiled into your binary. You can fix the // problem by either regenerating this code with an older version of connect or updating the connect // version compiled into your binary. -const _ = connect_go.IsAtLeastVersion0_1_0 +const _ = connect.IsAtLeastVersion0_1_0 const ( // GreetServiceName is the fully-qualified name of the GreetService service. GreetServiceName = "greet.v1.GreetService" ) +// These constants are the fully-qualified names of the RPCs defined in this package. They're +// exposed at runtime as Spec.Procedure and as the final two segments of the HTTP route. +// +// Note that these are different from the fully-qualified method names used by +// google.golang.org/protobuf/reflect/protoreflect. To convert from these constants to +// reflection-formatted method names, remove the leading slash and convert the remaining slash to a +// period. +const ( + // GreetServiceGreetProcedure is the fully-qualified name of the GreetService's Greet RPC. + GreetServiceGreetProcedure = "/greet.v1.GreetService/Greet" + // GreetServiceServerStreamGreetProcedure is the fully-qualified name of the GreetService's + // ServerStreamGreet RPC. + GreetServiceServerStreamGreetProcedure = "/greet.v1.GreetService/ServerStreamGreet" + // GreetServiceClientStreamGreetProcedure is the fully-qualified name of the GreetService's + // ClientStreamGreet RPC. + GreetServiceClientStreamGreetProcedure = "/greet.v1.GreetService/ClientStreamGreet" + // GreetServiceBidirectionalGreetProcedure is the fully-qualified name of the GreetService's + // BidirectionalGreet RPC. + GreetServiceBidirectionalGreetProcedure = "/greet.v1.GreetService/BidirectionalGreet" +) + // GreetServiceClient is a client for the greet.v1.GreetService service. type GreetServiceClient interface { - Greet(context.Context, *connect_go.Request[greet.GreetRequest]) (*connect_go.Response[greet.GreetResponse], error) - ServerStreamGreet(context.Context, *connect_go.Request[greet.GreetRequest]) (*connect_go.ServerStreamForClient[greet.GreetResponse], error) - ClientStreamGreet(context.Context) *connect_go.ClientStreamForClient[greet.GreetRequest, greet.GreetResponse] - BidirectionalGreet(context.Context) *connect_go.ClientStreamForClient[greet.GreetRequest, greet.GreetResponse] + Greet(context.Context, *connect.Request[greet.GreetRequest]) (*connect.Response[greet.GreetResponse], error) + ServerStreamGreet(context.Context, *connect.Request[greet.GreetRequest]) (*connect.ServerStreamForClient[greet.GreetResponse], error) + ClientStreamGreet(context.Context) *connect.ClientStreamForClient[greet.GreetRequest, greet.GreetResponse] + BidirectionalGreet(context.Context) *connect.ClientStreamForClient[greet.GreetRequest, greet.GreetResponse] } // NewGreetServiceClient constructs a client for the greet.v1.GreetService service. By default, it @@ -40,27 +61,27 @@ type GreetServiceClient interface { // // The URL supplied here should be the base URL for the Connect or gRPC server (for example, // http://api.acme.com or https://acme.com/grpc). -func NewGreetServiceClient(httpClient connect_go.HTTPClient, baseURL string, opts ...connect_go.ClientOption) GreetServiceClient { +func NewGreetServiceClient(httpClient connect.HTTPClient, baseURL string, opts ...connect.ClientOption) GreetServiceClient { baseURL = strings.TrimRight(baseURL, "/") return &greetServiceClient{ - greet: connect_go.NewClient[greet.GreetRequest, greet.GreetResponse]( + greet: connect.NewClient[greet.GreetRequest, greet.GreetResponse]( httpClient, - baseURL+"/greet.v1.GreetService/Greet", + baseURL+GreetServiceGreetProcedure, opts..., ), - serverStreamGreet: connect_go.NewClient[greet.GreetRequest, greet.GreetResponse]( + serverStreamGreet: connect.NewClient[greet.GreetRequest, greet.GreetResponse]( httpClient, - baseURL+"/greet.v1.GreetService/ServerStreamGreet", + baseURL+GreetServiceServerStreamGreetProcedure, opts..., ), - clientStreamGreet: connect_go.NewClient[greet.GreetRequest, greet.GreetResponse]( + clientStreamGreet: connect.NewClient[greet.GreetRequest, greet.GreetResponse]( httpClient, - baseURL+"/greet.v1.GreetService/ClientStreamGreet", + baseURL+GreetServiceClientStreamGreetProcedure, opts..., ), - bidirectionalGreet: connect_go.NewClient[greet.GreetRequest, greet.GreetResponse]( + bidirectionalGreet: connect.NewClient[greet.GreetRequest, greet.GreetResponse]( httpClient, - baseURL+"/greet.v1.GreetService/BidirectionalGreet", + baseURL+GreetServiceBidirectionalGreetProcedure, opts..., ), } @@ -68,38 +89,38 @@ func NewGreetServiceClient(httpClient connect_go.HTTPClient, baseURL string, opt // greetServiceClient implements GreetServiceClient. type greetServiceClient struct { - greet *connect_go.Client[greet.GreetRequest, greet.GreetResponse] - serverStreamGreet *connect_go.Client[greet.GreetRequest, greet.GreetResponse] - clientStreamGreet *connect_go.Client[greet.GreetRequest, greet.GreetResponse] - bidirectionalGreet *connect_go.Client[greet.GreetRequest, greet.GreetResponse] + greet *connect.Client[greet.GreetRequest, greet.GreetResponse] + serverStreamGreet *connect.Client[greet.GreetRequest, greet.GreetResponse] + clientStreamGreet *connect.Client[greet.GreetRequest, greet.GreetResponse] + bidirectionalGreet *connect.Client[greet.GreetRequest, greet.GreetResponse] } // Greet calls greet.v1.GreetService.Greet. -func (c *greetServiceClient) Greet(ctx context.Context, req *connect_go.Request[greet.GreetRequest]) (*connect_go.Response[greet.GreetResponse], error) { +func (c *greetServiceClient) Greet(ctx context.Context, req *connect.Request[greet.GreetRequest]) (*connect.Response[greet.GreetResponse], error) { return c.greet.CallUnary(ctx, req) } // ServerStreamGreet calls greet.v1.GreetService.ServerStreamGreet. -func (c *greetServiceClient) ServerStreamGreet(ctx context.Context, req *connect_go.Request[greet.GreetRequest]) (*connect_go.ServerStreamForClient[greet.GreetResponse], error) { +func (c *greetServiceClient) ServerStreamGreet(ctx context.Context, req *connect.Request[greet.GreetRequest]) (*connect.ServerStreamForClient[greet.GreetResponse], error) { return c.serverStreamGreet.CallServerStream(ctx, req) } // ClientStreamGreet calls greet.v1.GreetService.ClientStreamGreet. -func (c *greetServiceClient) ClientStreamGreet(ctx context.Context) *connect_go.ClientStreamForClient[greet.GreetRequest, greet.GreetResponse] { +func (c *greetServiceClient) ClientStreamGreet(ctx context.Context) *connect.ClientStreamForClient[greet.GreetRequest, greet.GreetResponse] { return c.clientStreamGreet.CallClientStream(ctx) } // BidirectionalGreet calls greet.v1.GreetService.BidirectionalGreet. -func (c *greetServiceClient) BidirectionalGreet(ctx context.Context) *connect_go.ClientStreamForClient[greet.GreetRequest, greet.GreetResponse] { +func (c *greetServiceClient) BidirectionalGreet(ctx context.Context) *connect.ClientStreamForClient[greet.GreetRequest, greet.GreetResponse] { return c.bidirectionalGreet.CallClientStream(ctx) } // GreetServiceHandler is an implementation of the greet.v1.GreetService service. type GreetServiceHandler interface { - Greet(context.Context, *connect_go.Request[greet.GreetRequest]) (*connect_go.Response[greet.GreetResponse], error) - ServerStreamGreet(context.Context, *connect_go.Request[greet.GreetRequest], *connect_go.ServerStream[greet.GreetResponse]) error - ClientStreamGreet(context.Context, *connect_go.ClientStream[greet.GreetRequest]) (*connect_go.Response[greet.GreetResponse], error) - BidirectionalGreet(context.Context, *connect_go.ClientStream[greet.GreetRequest]) (*connect_go.Response[greet.GreetResponse], error) + Greet(context.Context, *connect.Request[greet.GreetRequest]) (*connect.Response[greet.GreetResponse], error) + ServerStreamGreet(context.Context, *connect.Request[greet.GreetRequest], *connect.ServerStream[greet.GreetResponse]) error + ClientStreamGreet(context.Context, *connect.ClientStream[greet.GreetRequest]) (*connect.Response[greet.GreetResponse], error) + BidirectionalGreet(context.Context, *connect.ClientStream[greet.GreetRequest]) (*connect.Response[greet.GreetResponse], error) } // NewGreetServiceHandler builds an HTTP handler from the service implementation. It returns the @@ -107,46 +128,58 @@ type GreetServiceHandler interface { // // By default, handlers support the Connect, gRPC, and gRPC-Web protocols with the binary Protobuf // and JSON codecs. They also support gzip compression. -func NewGreetServiceHandler(svc GreetServiceHandler, opts ...connect_go.HandlerOption) (string, http.Handler) { - mux := http.NewServeMux() - mux.Handle("/greet.v1.GreetService/Greet", connect_go.NewUnaryHandler( - "/greet.v1.GreetService/Greet", +func NewGreetServiceHandler(svc GreetServiceHandler, opts ...connect.HandlerOption) (string, http.Handler) { + greetServiceGreetHandler := connect.NewUnaryHandler( + GreetServiceGreetProcedure, svc.Greet, opts..., - )) - mux.Handle("/greet.v1.GreetService/ServerStreamGreet", connect_go.NewServerStreamHandler( - "/greet.v1.GreetService/ServerStreamGreet", + ) + greetServiceServerStreamGreetHandler := connect.NewServerStreamHandler( + GreetServiceServerStreamGreetProcedure, svc.ServerStreamGreet, opts..., - )) - mux.Handle("/greet.v1.GreetService/ClientStreamGreet", connect_go.NewClientStreamHandler( - "/greet.v1.GreetService/ClientStreamGreet", + ) + greetServiceClientStreamGreetHandler := connect.NewClientStreamHandler( + GreetServiceClientStreamGreetProcedure, svc.ClientStreamGreet, opts..., - )) - mux.Handle("/greet.v1.GreetService/BidirectionalGreet", connect_go.NewClientStreamHandler( - "/greet.v1.GreetService/BidirectionalGreet", + ) + greetServiceBidirectionalGreetHandler := connect.NewClientStreamHandler( + GreetServiceBidirectionalGreetProcedure, svc.BidirectionalGreet, opts..., - )) - return "/greet.v1.GreetService/", mux + ) + return "/greet.v1.GreetService/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case GreetServiceGreetProcedure: + greetServiceGreetHandler.ServeHTTP(w, r) + case GreetServiceServerStreamGreetProcedure: + greetServiceServerStreamGreetHandler.ServeHTTP(w, r) + case GreetServiceClientStreamGreetProcedure: + greetServiceClientStreamGreetHandler.ServeHTTP(w, r) + case GreetServiceBidirectionalGreetProcedure: + greetServiceBidirectionalGreetHandler.ServeHTTP(w, r) + default: + http.NotFound(w, r) + } + }) } // UnimplementedGreetServiceHandler returns CodeUnimplemented from all methods. type UnimplementedGreetServiceHandler struct{} -func (UnimplementedGreetServiceHandler) Greet(context.Context, *connect_go.Request[greet.GreetRequest]) (*connect_go.Response[greet.GreetResponse], error) { - return nil, connect_go.NewError(connect_go.CodeUnimplemented, errors.New("greet.v1.GreetService.Greet is not implemented")) +func (UnimplementedGreetServiceHandler) Greet(context.Context, *connect.Request[greet.GreetRequest]) (*connect.Response[greet.GreetResponse], error) { + return nil, connect.NewError(connect.CodeUnimplemented, errors.New("greet.v1.GreetService.Greet is not implemented")) } -func (UnimplementedGreetServiceHandler) ServerStreamGreet(context.Context, *connect_go.Request[greet.GreetRequest], *connect_go.ServerStream[greet.GreetResponse]) error { - return connect_go.NewError(connect_go.CodeUnimplemented, errors.New("greet.v1.GreetService.ServerStreamGreet is not implemented")) +func (UnimplementedGreetServiceHandler) ServerStreamGreet(context.Context, *connect.Request[greet.GreetRequest], *connect.ServerStream[greet.GreetResponse]) error { + return connect.NewError(connect.CodeUnimplemented, errors.New("greet.v1.GreetService.ServerStreamGreet is not implemented")) } -func (UnimplementedGreetServiceHandler) ClientStreamGreet(context.Context, *connect_go.ClientStream[greet.GreetRequest]) (*connect_go.Response[greet.GreetResponse], error) { - return nil, connect_go.NewError(connect_go.CodeUnimplemented, errors.New("greet.v1.GreetService.ClientStreamGreet is not implemented")) +func (UnimplementedGreetServiceHandler) ClientStreamGreet(context.Context, *connect.ClientStream[greet.GreetRequest]) (*connect.Response[greet.GreetResponse], error) { + return nil, connect.NewError(connect.CodeUnimplemented, errors.New("greet.v1.GreetService.ClientStreamGreet is not implemented")) } -func (UnimplementedGreetServiceHandler) BidirectionalGreet(context.Context, *connect_go.ClientStream[greet.GreetRequest]) (*connect_go.Response[greet.GreetResponse], error) { - return nil, connect_go.NewError(connect_go.CodeUnimplemented, errors.New("greet.v1.GreetService.BidirectionalGreet is not implemented")) +func (UnimplementedGreetServiceHandler) BidirectionalGreet(context.Context, *connect.ClientStream[greet.GreetRequest]) (*connect.Response[greet.GreetResponse], error) { + return nil, connect.NewError(connect.CodeUnimplemented, errors.New("greet.v1.GreetService.BidirectionalGreet is not implemented")) } diff --git a/go.mod b/go.mod index 1ba5311..1d3bc05 100644 --- a/go.mod +++ b/go.mod @@ -6,10 +6,11 @@ require ( github.com/bufbuild/connect-go v1.0.0 github.com/prometheus/client_golang v1.13.0 github.com/stretchr/testify v1.4.0 - google.golang.org/protobuf v1.28.1 + google.golang.org/protobuf v1.31.0 ) require ( + connectrpc.com/connect v1.12.0 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.1.2 // indirect github.com/davecgh/go-spew v1.1.1 // indirect diff --git a/go.sum b/go.sum index 4f65b74..bf215e4 100644 --- a/go.sum +++ b/go.sum @@ -30,6 +30,8 @@ cloud.google.com/go/storage v1.5.0/go.mod h1:tpKbwo567HUNpVclU5sGELwQWBDZ8gh0Zeo cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohlUTyfDhBk= cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs= cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0= +connectrpc.com/connect v1.12.0 h1:HwKdOY0lGhhoHdsza+hW55aqHEC64pYpObRNoAgn70g= +connectrpc.com/connect v1.12.0/go.mod h1:3AGaO6RRGMx5IKFfqbe3hvK1NqLosFNP2BxDYTPmNPo= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= @@ -460,6 +462,8 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0 google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= +google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/interceptor.go b/interceptor.go index bd8285c..e41696b 100644 --- a/interceptor.go +++ b/interceptor.go @@ -6,7 +6,7 @@ import ( "strings" "time" - "github.com/bufbuild/connect-go" + "connectrpc.com/connect" ) const ( diff --git a/interceptor_test.go b/interceptor_test.go index 23cc462..c12f6c3 100644 --- a/interceptor_test.go +++ b/interceptor_test.go @@ -6,7 +6,7 @@ import ( "net/http/httptest" "testing" - "github.com/bufbuild/connect-go" + "connectrpc.com/connect" "github.com/easyCZ/connect-go-prometheus/gen/greet" "github.com/easyCZ/connect-go-prometheus/gen/greet/greetconnect" prom "github.com/prometheus/client_golang/prometheus" diff --git a/metrics_test.go b/metrics_test.go index 42ab4b7..3fd4d28 100644 --- a/metrics_test.go +++ b/metrics_test.go @@ -4,7 +4,7 @@ import ( "strings" "testing" - "github.com/bufbuild/connect-go" + "connectrpc.com/connect" "github.com/easyCZ/connect-go-prometheus/gen/greet/greetconnect" prom "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/testutil"