@@ -124,7 +124,13 @@ type checkoutService struct {
124
124
paymentSvcAddr string
125
125
kafkaBrokerSvcAddr string
126
126
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
128
134
}
129
135
130
136
func main () {
@@ -153,12 +159,37 @@ func main() {
153
159
tracer = tp .Tracer ("checkoutservice" )
154
160
155
161
svc := new (checkoutService )
162
+
156
163
mustMapEnv (& svc .shippingSvcAddr , "SHIPPING_SERVICE_ADDR" )
164
+ c := mustCreateClient (context .Background (), svc .shippingSvcAddr )
165
+ svc .shippingSvcClient = pb .NewShippingServiceClient (c )
166
+ defer c .Close ()
167
+
157
168
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
+
158
173
mustMapEnv (& svc .cartSvcAddr , "CART_SERVICE_ADDR" )
174
+ c = mustCreateClient (context .Background (), svc .cartSvcAddr )
175
+ svc .cartSvcClient = pb .NewCartServiceClient (c )
176
+ defer c .Close ()
177
+
159
178
mustMapEnv (& svc .currencySvcAddr , "CURRENCY_SERVICE_ADDR" )
179
+ c = mustCreateClient (context .Background (), svc .currencySvcAddr )
180
+ svc .currencySvcClient = pb .NewCurrencyServiceClient (c )
181
+ defer c .Close ()
182
+
160
183
mustMapEnv (& svc .emailSvcAddr , "EMAIL_SERVICE_ADDR" )
184
+ c = mustCreateClient (context .Background (), svc .emailSvcAddr )
185
+ svc .emailSvcClient = pb .NewEmailServiceClient (c )
186
+ defer c .Close ()
187
+
161
188
mustMapEnv (& svc .paymentSvcAddr , "PAYMENT_SERVICE_ADDR" )
189
+ c = mustCreateClient (context .Background (), svc .paymentSvcAddr )
190
+ svc .paymentSvcClient = pb .NewPaymentServiceClient (c )
191
+ defer c .Close ()
192
+
162
193
svc .kafkaBrokerSvcAddr = os .Getenv ("KAFKA_SERVICE_ADDR" )
163
194
164
195
if svc .kafkaBrokerSvcAddr != "" {
@@ -334,21 +365,20 @@ func (cs *checkoutService) prepareOrderItemsAndShippingQuoteFromCart(ctx context
334
365
return out , nil
335
366
}
336
367
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 ,
339
370
grpc .WithTransportCredentials (insecure .NewCredentials ()),
340
371
grpc .WithStatsHandler (otelgrpc .NewClientHandler ()),
341
372
)
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 )
346
373
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 )
348
375
}
349
- defer conn .Close ()
350
376
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 .
352
382
GetQuote (ctx , & pb.GetQuoteRequest {
353
383
Address : address ,
354
384
Items : items })
@@ -359,27 +389,15 @@ func (cs *checkoutService) quoteShipping(ctx context.Context, address *pb.Addres
359
389
}
360
390
361
391
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 })
369
393
if err != nil {
370
394
return nil , fmt .Errorf ("failed to get user cart during checkout: %+v" , err )
371
395
}
372
396
return cart .GetItems (), nil
373
397
}
374
398
375
399
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 {
383
401
return fmt .Errorf ("failed to empty user cart during checkout: %+v" , err )
384
402
}
385
403
return nil
@@ -388,15 +406,8 @@ func (cs *checkoutService) emptyUserCart(ctx context.Context, userID string) err
388
406
func (cs * checkoutService ) prepOrderItems (ctx context.Context , items []* pb.CartItem , userCurrency string ) ([]* pb.OrderItem , error ) {
389
407
out := make ([]* pb.OrderItem , len (items ))
390
408
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
-
398
409
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 ()})
400
411
if err != nil {
401
412
return nil , fmt .Errorf ("failed to get product #%q" , item .GetProductId ())
402
413
}
@@ -412,12 +423,7 @@ func (cs *checkoutService) prepOrderItems(ctx context.Context, items []*pb.CartI
412
423
}
413
424
414
425
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 {
421
427
From : from ,
422
428
ToCode : toCurrency })
423
429
if err != nil {
@@ -427,13 +433,7 @@ func (cs *checkoutService) convertCurrency(ctx context.Context, from *pb.Money,
427
433
}
428
434
429
435
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 {
437
437
Amount : amount ,
438
438
CreditCard : paymentInfo })
439
439
if err != nil {
@@ -465,12 +465,7 @@ func (cs *checkoutService) sendOrderConfirmation(ctx context.Context, email stri
465
465
}
466
466
467
467
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 {
474
469
Address : address ,
475
470
Items : items })
476
471
if err != nil {
0 commit comments