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

refactor(checkoutservice): reuse grpc service client #1387

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 46 additions & 51 deletions src/checkoutservice/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,13 @@ type checkoutService struct {
paymentSvcAddr string
kafkaBrokerSvcAddr string
pb.UnimplementedCheckoutServiceServer
KafkaProducerClient sarama.AsyncProducer
KafkaProducerClient sarama.AsyncProducer
shippingSvcClient pb.ShippingServiceClient
productCatalogSvcClient pb.ProductCatalogServiceClient
cartSvcClient pb.CartServiceClient
currencySvcClient pb.CurrencyServiceClient
emailSvcClient pb.EmailServiceClient
paymentSvcClient pb.PaymentServiceClient
}

func main() {
Expand Down Expand Up @@ -153,12 +159,37 @@ func main() {
tracer = tp.Tracer("checkoutservice")

svc := new(checkoutService)

mustMapEnv(&svc.shippingSvcAddr, "SHIPPING_SERVICE_ADDR")
c := mustCreateClient(context.Background(), svc.shippingSvcAddr)
svc.shippingSvcClient = pb.NewShippingServiceClient(c)
defer c.Close()

mustMapEnv(&svc.productCatalogSvcAddr, "PRODUCT_CATALOG_SERVICE_ADDR")
c = mustCreateClient(context.Background(), svc.productCatalogSvcAddr)
svc.productCatalogSvcClient = pb.NewProductCatalogServiceClient(c)
defer c.Close()

mustMapEnv(&svc.cartSvcAddr, "CART_SERVICE_ADDR")
c = mustCreateClient(context.Background(), svc.cartSvcAddr)
svc.cartSvcClient = pb.NewCartServiceClient(c)
defer c.Close()

mustMapEnv(&svc.currencySvcAddr, "CURRENCY_SERVICE_ADDR")
c = mustCreateClient(context.Background(), svc.currencySvcAddr)
svc.currencySvcClient = pb.NewCurrencyServiceClient(c)
defer c.Close()

mustMapEnv(&svc.emailSvcAddr, "EMAIL_SERVICE_ADDR")
c = mustCreateClient(context.Background(), svc.emailSvcAddr)
svc.emailSvcClient = pb.NewEmailServiceClient(c)
defer c.Close()

mustMapEnv(&svc.paymentSvcAddr, "PAYMENT_SERVICE_ADDR")
c = mustCreateClient(context.Background(), svc.paymentSvcAddr)
svc.paymentSvcClient = pb.NewPaymentServiceClient(c)
defer c.Close()

svc.kafkaBrokerSvcAddr = os.Getenv("KAFKA_SERVICE_ADDR")

if svc.kafkaBrokerSvcAddr != "" {
Expand Down Expand Up @@ -334,21 +365,20 @@ func (cs *checkoutService) prepareOrderItemsAndShippingQuoteFromCart(ctx context
return out, nil
}

func createClient(ctx context.Context, svcAddr string) (*grpc.ClientConn, error) {
return grpc.DialContext(ctx, svcAddr,
func mustCreateClient(ctx context.Context, svcAddr string) *grpc.ClientConn {
c, err := grpc.DialContext(ctx, svcAddr,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithStatsHandler(otelgrpc.NewClientHandler()),
)
}

func (cs *checkoutService) quoteShipping(ctx context.Context, address *pb.Address, items []*pb.CartItem) (*pb.Money, error) {
conn, err := createClient(ctx, cs.shippingSvcAddr)
if err != nil {
return nil, fmt.Errorf("could not connect shipping service: %+v", err)
log.Fatalf("could not connect to %s service, err: %+v", svcAddr, err)
}
defer conn.Close()

shippingQuote, err := pb.NewShippingServiceClient(conn).
return c
}

func (cs *checkoutService) quoteShipping(ctx context.Context, address *pb.Address, items []*pb.CartItem) (*pb.Money, error) {
shippingQuote, err := cs.shippingSvcClient.
GetQuote(ctx, &pb.GetQuoteRequest{
Address: address,
Items: items})
Expand All @@ -359,27 +389,15 @@ func (cs *checkoutService) quoteShipping(ctx context.Context, address *pb.Addres
}

func (cs *checkoutService) getUserCart(ctx context.Context, userID string) ([]*pb.CartItem, error) {
conn, err := createClient(ctx, cs.cartSvcAddr)
if err != nil {
return nil, fmt.Errorf("could not connect cart service: %+v", err)
}
defer conn.Close()

cart, err := pb.NewCartServiceClient(conn).GetCart(ctx, &pb.GetCartRequest{UserId: userID})
cart, err := cs.cartSvcClient.GetCart(ctx, &pb.GetCartRequest{UserId: userID})
if err != nil {
return nil, fmt.Errorf("failed to get user cart during checkout: %+v", err)
}
return cart.GetItems(), nil
}

func (cs *checkoutService) emptyUserCart(ctx context.Context, userID string) error {
conn, err := createClient(ctx, cs.cartSvcAddr)
if err != nil {
return fmt.Errorf("could not connect cart service: %+v", err)
}
defer conn.Close()

if _, err = pb.NewCartServiceClient(conn).EmptyCart(ctx, &pb.EmptyCartRequest{UserId: userID}); err != nil {
if _, err := cs.cartSvcClient.EmptyCart(ctx, &pb.EmptyCartRequest{UserId: userID}); err != nil {
return fmt.Errorf("failed to empty user cart during checkout: %+v", err)
}
return nil
Expand All @@ -388,15 +406,8 @@ func (cs *checkoutService) emptyUserCart(ctx context.Context, userID string) err
func (cs *checkoutService) prepOrderItems(ctx context.Context, items []*pb.CartItem, userCurrency string) ([]*pb.OrderItem, error) {
out := make([]*pb.OrderItem, len(items))

conn, err := createClient(ctx, cs.productCatalogSvcAddr)
if err != nil {
return nil, fmt.Errorf("could not connect product catalog service: %+v", err)
}
defer conn.Close()
cl := pb.NewProductCatalogServiceClient(conn)

for i, item := range items {
product, err := cl.GetProduct(ctx, &pb.GetProductRequest{Id: item.GetProductId()})
product, err := cs.productCatalogSvcClient.GetProduct(ctx, &pb.GetProductRequest{Id: item.GetProductId()})
if err != nil {
return nil, fmt.Errorf("failed to get product #%q", item.GetProductId())
}
Expand All @@ -412,12 +423,7 @@ func (cs *checkoutService) prepOrderItems(ctx context.Context, items []*pb.CartI
}

func (cs *checkoutService) convertCurrency(ctx context.Context, from *pb.Money, toCurrency string) (*pb.Money, error) {
conn, err := createClient(ctx, cs.currencySvcAddr)
if err != nil {
return nil, fmt.Errorf("could not connect currency service: %+v", err)
}
defer conn.Close()
result, err := pb.NewCurrencyServiceClient(conn).Convert(ctx, &pb.CurrencyConversionRequest{
result, err := cs.currencySvcClient.Convert(ctx, &pb.CurrencyConversionRequest{
From: from,
ToCode: toCurrency})
if err != nil {
Expand All @@ -427,13 +433,7 @@ func (cs *checkoutService) convertCurrency(ctx context.Context, from *pb.Money,
}

func (cs *checkoutService) chargeCard(ctx context.Context, amount *pb.Money, paymentInfo *pb.CreditCardInfo) (string, error) {
conn, err := createClient(ctx, cs.paymentSvcAddr)
if err != nil {
return "", fmt.Errorf("failed to connect payment service: %+v", err)
}
defer conn.Close()

paymentResp, err := pb.NewPaymentServiceClient(conn).Charge(ctx, &pb.ChargeRequest{
paymentResp, err := cs.paymentSvcClient.Charge(ctx, &pb.ChargeRequest{
Amount: amount,
CreditCard: paymentInfo})
if err != nil {
Expand Down Expand Up @@ -465,12 +465,7 @@ func (cs *checkoutService) sendOrderConfirmation(ctx context.Context, email stri
}

func (cs *checkoutService) shipOrder(ctx context.Context, address *pb.Address, items []*pb.CartItem) (string, error) {
conn, err := createClient(ctx, cs.shippingSvcAddr)
if err != nil {
return "", fmt.Errorf("failed to connect email service: %+v", err)
}
defer conn.Close()
resp, err := pb.NewShippingServiceClient(conn).ShipOrder(ctx, &pb.ShipOrderRequest{
resp, err := cs.shippingSvcClient.ShipOrder(ctx, &pb.ShipOrderRequest{
Address: address,
Items: items})
if err != nil {
Expand Down
Loading