Skip to content

Commit e8e3553

Browse files
authored
refactor(checkoutservice): reuse grpc service client (#1387)
1 parent 9d77fdf commit e8e3553

File tree

1 file changed

+46
-51
lines changed

1 file changed

+46
-51
lines changed

src/checkoutservice/main.go

+46-51
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,13 @@ type checkoutService struct {
124124
paymentSvcAddr string
125125
kafkaBrokerSvcAddr string
126126
pb.UnimplementedCheckoutServiceServer
127-
KafkaProducerClient sarama.AsyncProducer
127+
KafkaProducerClient sarama.AsyncProducer
128+
shippingSvcClient pb.ShippingServiceClient
129+
productCatalogSvcClient pb.ProductCatalogServiceClient
130+
cartSvcClient pb.CartServiceClient
131+
currencySvcClient pb.CurrencyServiceClient
132+
emailSvcClient pb.EmailServiceClient
133+
paymentSvcClient pb.PaymentServiceClient
128134
}
129135

130136
func main() {
@@ -153,12 +159,37 @@ func main() {
153159
tracer = tp.Tracer("checkoutservice")
154160

155161
svc := new(checkoutService)
162+
156163
mustMapEnv(&svc.shippingSvcAddr, "SHIPPING_SERVICE_ADDR")
164+
c := mustCreateClient(context.Background(), svc.shippingSvcAddr)
165+
svc.shippingSvcClient = pb.NewShippingServiceClient(c)
166+
defer c.Close()
167+
157168
mustMapEnv(&svc.productCatalogSvcAddr, "PRODUCT_CATALOG_SERVICE_ADDR")
169+
c = mustCreateClient(context.Background(), svc.productCatalogSvcAddr)
170+
svc.productCatalogSvcClient = pb.NewProductCatalogServiceClient(c)
171+
defer c.Close()
172+
158173
mustMapEnv(&svc.cartSvcAddr, "CART_SERVICE_ADDR")
174+
c = mustCreateClient(context.Background(), svc.cartSvcAddr)
175+
svc.cartSvcClient = pb.NewCartServiceClient(c)
176+
defer c.Close()
177+
159178
mustMapEnv(&svc.currencySvcAddr, "CURRENCY_SERVICE_ADDR")
179+
c = mustCreateClient(context.Background(), svc.currencySvcAddr)
180+
svc.currencySvcClient = pb.NewCurrencyServiceClient(c)
181+
defer c.Close()
182+
160183
mustMapEnv(&svc.emailSvcAddr, "EMAIL_SERVICE_ADDR")
184+
c = mustCreateClient(context.Background(), svc.emailSvcAddr)
185+
svc.emailSvcClient = pb.NewEmailServiceClient(c)
186+
defer c.Close()
187+
161188
mustMapEnv(&svc.paymentSvcAddr, "PAYMENT_SERVICE_ADDR")
189+
c = mustCreateClient(context.Background(), svc.paymentSvcAddr)
190+
svc.paymentSvcClient = pb.NewPaymentServiceClient(c)
191+
defer c.Close()
192+
162193
svc.kafkaBrokerSvcAddr = os.Getenv("KAFKA_SERVICE_ADDR")
163194

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

337-
func createClient(ctx context.Context, svcAddr string) (*grpc.ClientConn, error) {
338-
return grpc.DialContext(ctx, svcAddr,
368+
func mustCreateClient(ctx context.Context, svcAddr string) *grpc.ClientConn {
369+
c, err := grpc.DialContext(ctx, svcAddr,
339370
grpc.WithTransportCredentials(insecure.NewCredentials()),
340371
grpc.WithStatsHandler(otelgrpc.NewClientHandler()),
341372
)
342-
}
343-
344-
func (cs *checkoutService) quoteShipping(ctx context.Context, address *pb.Address, items []*pb.CartItem) (*pb.Money, error) {
345-
conn, err := createClient(ctx, cs.shippingSvcAddr)
346373
if err != nil {
347-
return nil, fmt.Errorf("could not connect shipping service: %+v", err)
374+
log.Fatalf("could not connect to %s service, err: %+v", svcAddr, err)
348375
}
349-
defer conn.Close()
350376

351-
shippingQuote, err := pb.NewShippingServiceClient(conn).
377+
return c
378+
}
379+
380+
func (cs *checkoutService) quoteShipping(ctx context.Context, address *pb.Address, items []*pb.CartItem) (*pb.Money, error) {
381+
shippingQuote, err := cs.shippingSvcClient.
352382
GetQuote(ctx, &pb.GetQuoteRequest{
353383
Address: address,
354384
Items: items})
@@ -359,27 +389,15 @@ func (cs *checkoutService) quoteShipping(ctx context.Context, address *pb.Addres
359389
}
360390

361391
func (cs *checkoutService) getUserCart(ctx context.Context, userID string) ([]*pb.CartItem, error) {
362-
conn, err := createClient(ctx, cs.cartSvcAddr)
363-
if err != nil {
364-
return nil, fmt.Errorf("could not connect cart service: %+v", err)
365-
}
366-
defer conn.Close()
367-
368-
cart, err := pb.NewCartServiceClient(conn).GetCart(ctx, &pb.GetCartRequest{UserId: userID})
392+
cart, err := cs.cartSvcClient.GetCart(ctx, &pb.GetCartRequest{UserId: userID})
369393
if err != nil {
370394
return nil, fmt.Errorf("failed to get user cart during checkout: %+v", err)
371395
}
372396
return cart.GetItems(), nil
373397
}
374398

375399
func (cs *checkoutService) emptyUserCart(ctx context.Context, userID string) error {
376-
conn, err := createClient(ctx, cs.cartSvcAddr)
377-
if err != nil {
378-
return fmt.Errorf("could not connect cart service: %+v", err)
379-
}
380-
defer conn.Close()
381-
382-
if _, err = pb.NewCartServiceClient(conn).EmptyCart(ctx, &pb.EmptyCartRequest{UserId: userID}); err != nil {
400+
if _, err := cs.cartSvcClient.EmptyCart(ctx, &pb.EmptyCartRequest{UserId: userID}); err != nil {
383401
return fmt.Errorf("failed to empty user cart during checkout: %+v", err)
384402
}
385403
return nil
@@ -388,15 +406,8 @@ func (cs *checkoutService) emptyUserCart(ctx context.Context, userID string) err
388406
func (cs *checkoutService) prepOrderItems(ctx context.Context, items []*pb.CartItem, userCurrency string) ([]*pb.OrderItem, error) {
389407
out := make([]*pb.OrderItem, len(items))
390408

391-
conn, err := createClient(ctx, cs.productCatalogSvcAddr)
392-
if err != nil {
393-
return nil, fmt.Errorf("could not connect product catalog service: %+v", err)
394-
}
395-
defer conn.Close()
396-
cl := pb.NewProductCatalogServiceClient(conn)
397-
398409
for i, item := range items {
399-
product, err := cl.GetProduct(ctx, &pb.GetProductRequest{Id: item.GetProductId()})
410+
product, err := cs.productCatalogSvcClient.GetProduct(ctx, &pb.GetProductRequest{Id: item.GetProductId()})
400411
if err != nil {
401412
return nil, fmt.Errorf("failed to get product #%q", item.GetProductId())
402413
}
@@ -412,12 +423,7 @@ func (cs *checkoutService) prepOrderItems(ctx context.Context, items []*pb.CartI
412423
}
413424

414425
func (cs *checkoutService) convertCurrency(ctx context.Context, from *pb.Money, toCurrency string) (*pb.Money, error) {
415-
conn, err := createClient(ctx, cs.currencySvcAddr)
416-
if err != nil {
417-
return nil, fmt.Errorf("could not connect currency service: %+v", err)
418-
}
419-
defer conn.Close()
420-
result, err := pb.NewCurrencyServiceClient(conn).Convert(ctx, &pb.CurrencyConversionRequest{
426+
result, err := cs.currencySvcClient.Convert(ctx, &pb.CurrencyConversionRequest{
421427
From: from,
422428
ToCode: toCurrency})
423429
if err != nil {
@@ -427,13 +433,7 @@ func (cs *checkoutService) convertCurrency(ctx context.Context, from *pb.Money,
427433
}
428434

429435
func (cs *checkoutService) chargeCard(ctx context.Context, amount *pb.Money, paymentInfo *pb.CreditCardInfo) (string, error) {
430-
conn, err := createClient(ctx, cs.paymentSvcAddr)
431-
if err != nil {
432-
return "", fmt.Errorf("failed to connect payment service: %+v", err)
433-
}
434-
defer conn.Close()
435-
436-
paymentResp, err := pb.NewPaymentServiceClient(conn).Charge(ctx, &pb.ChargeRequest{
436+
paymentResp, err := cs.paymentSvcClient.Charge(ctx, &pb.ChargeRequest{
437437
Amount: amount,
438438
CreditCard: paymentInfo})
439439
if err != nil {
@@ -465,12 +465,7 @@ func (cs *checkoutService) sendOrderConfirmation(ctx context.Context, email stri
465465
}
466466

467467
func (cs *checkoutService) shipOrder(ctx context.Context, address *pb.Address, items []*pb.CartItem) (string, error) {
468-
conn, err := createClient(ctx, cs.shippingSvcAddr)
469-
if err != nil {
470-
return "", fmt.Errorf("failed to connect email service: %+v", err)
471-
}
472-
defer conn.Close()
473-
resp, err := pb.NewShippingServiceClient(conn).ShipOrder(ctx, &pb.ShipOrderRequest{
468+
resp, err := cs.shippingSvcClient.ShipOrder(ctx, &pb.ShipOrderRequest{
474469
Address: address,
475470
Items: items})
476471
if err != nil {

0 commit comments

Comments
 (0)