diff --git a/src/frontend/go.mod b/src/frontend/go.mod index 09c07a8066f..5735b13e0ff 100644 --- a/src/frontend/go.mod +++ b/src/frontend/go.mod @@ -31,6 +31,7 @@ require ( github.com/google/pprof v0.0.0-20221103000818-d260c55eee4c // indirect github.com/googleapis/enterprise-certificate-proxy v0.2.0 // indirect github.com/googleapis/gax-go/v2 v2.7.0 // indirect + github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.0-rc.5 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect go.opencensus.io v0.24.0 // indirect go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.14.0 // indirect @@ -44,5 +45,5 @@ require ( google.golang.org/api v0.103.0 // indirect google.golang.org/appengine v1.6.7 // indirect google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect - google.golang.org/protobuf v1.28.1 // indirect + google.golang.org/protobuf v1.30.0 // indirect ) diff --git a/src/frontend/go.sum b/src/frontend/go.sum index 797b58f4777..821f3e8f2b1 100644 --- a/src/frontend/go.sum +++ b/src/frontend/go.sum @@ -154,6 +154,8 @@ github.com/googleapis/gax-go/v2 v2.7.0 h1:IcsPKeInNvYi7eqSaDjiZqDDKu5rsmunY0Y1Yu github.com/googleapis/gax-go/v2 v2.7.0/go.mod h1:TEop28CZZQ2y+c0VxMUmu1lV+fQx57QpBWsYpwqHJx8= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.0-rc.5 h1:3IZOAnD058zZllQTZNBioTlrzrBG/IjpiZ133IEtusM= +github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.0-rc.5/go.mod h1:xbKERva94Pw2cPen0s79J3uXmGzbbpDYFBFDlZ4mV/w= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 h1:BZHcxBETFHIdVyhyEfOvn/RdU/QGdLI4y34qQGjGWO0= github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0/go.mod h1:hgWBS7lorOAVIJEQMi4ZsPv9hVvWI6+ch50m39Pf2Ks= @@ -481,6 +483,8 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ google.golang.org/protobuf v1.27.1/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.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= +google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= 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= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= diff --git a/src/frontend/main.go b/src/frontend/main.go index 068a467a5ef..4436011a004 100644 --- a/src/frontend/main.go +++ b/src/frontend/main.go @@ -32,6 +32,8 @@ import ( "go.opentelemetry.io/otel/propagation" sdktrace "go.opentelemetry.io/otel/sdk/trace" "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/retry" ) const ( @@ -210,14 +212,21 @@ func mustMapEnv(target *string, envKey string) { *target = v } + func mustConnGRPC(ctx context.Context, conn **grpc.ClientConn, addr string) { var err error + opts := []retry.CallOption{ + retry.WithBackoff(retry.BackoffLinear(250 * time.Millisecond)), + retry.WithCodes(codes.Aborted), + } ctx, cancel := context.WithTimeout(ctx, time.Second*3) defer cancel() *conn, err = grpc.DialContext(ctx, addr, grpc.WithInsecure(), grpc.WithUnaryInterceptor(otelgrpc.UnaryClientInterceptor()), - grpc.WithStreamInterceptor(otelgrpc.StreamClientInterceptor())) + grpc.WithStreamInterceptor(otelgrpc.StreamClientInterceptor()), + grpc.WithUnaryInterceptor(retry.UnaryClientInterceptor(opts...)), + ) if err != nil { panic(errors.Wrapf(err, "grpc: failed to connect %s", addr)) } diff --git a/src/frontend/rpc.go b/src/frontend/rpc.go index a1dd313847d..81b78e85992 100644 --- a/src/frontend/rpc.go +++ b/src/frontend/rpc.go @@ -21,6 +21,8 @@ import ( pb "github.com/GoogleCloudPlatform/microservices-demo/src/frontend/genproto" "github.com/pkg/errors" + + "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/retry" ) const ( @@ -50,7 +52,7 @@ func (fe *frontendServer) getProducts(ctx context.Context) ([]*pb.Product, error func (fe *frontendServer) getProduct(ctx context.Context, id string) (*pb.Product, error) { resp, err := pb.NewProductCatalogServiceClient(fe.productCatalogSvcConn). - GetProduct(ctx, &pb.GetProductRequest{Id: id}) + GetProduct(ctx, &pb.GetProductRequest{Id: id}, retry.WithMax(3)) return resp, err }