From efc9be3e4922eb355f033dbd50548ea496ac53a0 Mon Sep 17 00:00:00 2001 From: tkms0106 <23391543+tkms0106@users.noreply.github.com> Date: Wed, 12 Aug 2020 11:57:01 +0900 Subject: [PATCH 1/5] Uncomment --- ch03/order-service/go/client/main.go | 190 ++++++++++++++------------- 1 file changed, 96 insertions(+), 94 deletions(-) diff --git a/ch03/order-service/go/client/main.go b/ch03/order-service/go/client/main.go index 1f73437..9e82ebc 100644 --- a/ch03/order-service/go/client/main.go +++ b/ch03/order-service/go/client/main.go @@ -2,10 +2,12 @@ package main import ( "context" - "google.golang.org/grpc" + "io" "log" pb "ordermgt/client/ecommerce" "time" + + "google.golang.org/grpc" ) const ( @@ -19,8 +21,8 @@ func main() { log.Fatalf("did not connect: %v", err) } defer conn.Close() - client := pb.NewOrderManagementClient(conn) - ctx , cancel := context.WithTimeout(context.Background(), time.Second*5) + client := pb.NewOrderManagementClient(conn) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) defer cancel() // Add Order @@ -30,96 +32,96 @@ func main() { log.Print("AddOrder Response -> ", res.Value) } - // // Get Order - // retrievedOrder , err := client.GetOrder(ctx, &wrapper.StringValue{Value: "106"}) - // log.Print("GetOrder Response -> : ", retrievedOrder) - - // // Search Order : Server streaming scenario - // searchStream, _ := client.SearchOrders(ctx, &wrapper.StringValue{Value: "Google"}) - // for { - // searchOrder, err := searchStream.Recv() - // if err == io.EOF { - // log.Print("EOF") - // break - // } - - // if err == nil { - // log.Print("Search Result : ", searchOrder) - // } - // } - - // // ========================================= - // // Update Orders : Client streaming scenario - // updOrder1 := pb.Order{Id: "102", Items:[]string{"Google Pixel 3A", "Google Pixel Book"}, Destination:"Mountain View, CA", Price:1100.00} - // updOrder2 := pb.Order{Id: "103", Items:[]string{"Apple Watch S4", "Mac Book Pro", "iPad Pro"}, Destination:"San Jose, CA", Price:2800.00} - // updOrder3 := pb.Order{Id: "104", Items:[]string{"Google Home Mini", "Google Nest Hub", "iPad Mini"}, Destination:"Mountain View, CA", Price:2200.00} - - // updateStream, err := client.UpdateOrders(ctx) - - // if err != nil { - // log.Fatalf("%v.UpdateOrders(_) = _, %v", client, err) - // } - - // // Updating order 1 - // if err := updateStream.Send(&updOrder1); err != nil { - // log.Fatalf("%v.Send(%v) = %v", updateStream, updOrder1, err) - // } - - // // Updating order 2 - // if err := updateStream.Send(&updOrder2); err != nil { - // log.Fatalf("%v.Send(%v) = %v", updateStream, updOrder2, err) - // } - - // // Updating order 3 - // if err := updateStream.Send(&updOrder3); err != nil { - // log.Fatalf("%v.Send(%v) = %v", updateStream, updOrder3, err) - // } - - // updateRes, err := updateStream.CloseAndRecv() - // if err != nil { - // log.Fatalf("%v.CloseAndRecv() got error %v, want %v", updateStream, err, nil) - // } - // log.Printf("Update Orders Res : %s", updateRes) - - // // ========================================= - // // Process Order : Bi-di streaming scenario - // streamProcOrder, err := client.ProcessOrders(ctx) - // if err != nil { - // log.Fatalf("%v.ProcessOrders(_) = _, %v", client, err) - // } - - // if err := streamProcOrder.Send(&wrapper.StringValue{Value:"102"}); err != nil { - // log.Fatalf("%v.Send(%v) = %v", client, "102", err) - // } - - // if err := streamProcOrder.Send(&wrapper.StringValue{Value:"103"}); err != nil { - // log.Fatalf("%v.Send(%v) = %v", client, "103", err) - // } - - // if err := streamProcOrder.Send(&wrapper.StringValue{Value:"104"}); err != nil { - // log.Fatalf("%v.Send(%v) = %v", client, "104", err) - // } - - // channel := make(chan struct{}) - // go asncClientBidirectionalRPC(streamProcOrder, channel) - // time.Sleep(time.Millisecond * 1000) - - // if err := streamProcOrder.Send(&wrapper.StringValue{Value:"101"}); err != nil { - // log.Fatalf("%v.Send(%v) = %v", client, "101", err) - // } - // if err := streamProcOrder.CloseSend(); err != nil { - // log.Fatal(err) - // } - // <- channel + // Get Order + retrievedOrder, err := client.GetOrder(ctx, &wrapper.StringValue{Value: "106"}) + log.Print("GetOrder Response -> : ", retrievedOrder) + + // Search Order : Server streaming scenario + searchStream, _ := client.SearchOrders(ctx, &wrapper.StringValue{Value: "Google"}) + for { + searchOrder, err := searchStream.Recv() + if err == io.EOF { + log.Print("EOF") + break + } + + if err == nil { + log.Print("Search Result : ", searchOrder) + } + } + + // ========================================= + // Update Orders : Client streaming scenario + updOrder1 := pb.Order{Id: "102", Items: []string{"Google Pixel 3A", "Google Pixel Book"}, Destination: "Mountain View, CA", Price: 1100.00} + updOrder2 := pb.Order{Id: "103", Items: []string{"Apple Watch S4", "Mac Book Pro", "iPad Pro"}, Destination: "San Jose, CA", Price: 2800.00} + updOrder3 := pb.Order{Id: "104", Items: []string{"Google Home Mini", "Google Nest Hub", "iPad Mini"}, Destination: "Mountain View, CA", Price: 2200.00} + + updateStream, err := client.UpdateOrders(ctx) + + if err != nil { + log.Fatalf("%v.UpdateOrders(_) = _, %v", client, err) + } + + // Updating order 1 + if err := updateStream.Send(&updOrder1); err != nil { + log.Fatalf("%v.Send(%v) = %v", updateStream, updOrder1, err) + } + + // Updating order 2 + if err := updateStream.Send(&updOrder2); err != nil { + log.Fatalf("%v.Send(%v) = %v", updateStream, updOrder2, err) + } + + // Updating order 3 + if err := updateStream.Send(&updOrder3); err != nil { + log.Fatalf("%v.Send(%v) = %v", updateStream, updOrder3, err) + } + + updateRes, err := updateStream.CloseAndRecv() + if err != nil { + log.Fatalf("%v.CloseAndRecv() got error %v, want %v", updateStream, err, nil) + } + log.Printf("Update Orders Res : %s", updateRes) + + // ========================================= + // Process Order : Bi-di streaming scenario + streamProcOrder, err := client.ProcessOrders(ctx) + if err != nil { + log.Fatalf("%v.ProcessOrders(_) = _, %v", client, err) + } + + if err := streamProcOrder.Send(&wrapper.StringValue{Value: "102"}); err != nil { + log.Fatalf("%v.Send(%v) = %v", client, "102", err) + } + + if err := streamProcOrder.Send(&wrapper.StringValue{Value: "103"}); err != nil { + log.Fatalf("%v.Send(%v) = %v", client, "103", err) + } + + if err := streamProcOrder.Send(&wrapper.StringValue{Value: "104"}); err != nil { + log.Fatalf("%v.Send(%v) = %v", client, "104", err) + } + + channel := make(chan struct{}) + go asncClientBidirectionalRPC(streamProcOrder, channel) + time.Sleep(time.Millisecond * 1000) + + if err := streamProcOrder.Send(&wrapper.StringValue{Value: "101"}); err != nil { + log.Fatalf("%v.Send(%v) = %v", client, "101", err) + } + if err := streamProcOrder.CloseSend(); err != nil { + log.Fatal(err) + } + <-channel } -//func asncClientBidirectionalRPC(streamProcOrder pb.OrderManagement_ProcessOrdersClient, c chan struct{}) { -// for { -// combinedShipment, errProcOrder := streamProcOrder.Recv() -// if errProcOrder == io.EOF { -// break -// } -// log.Printf("Combined shipment : ", combinedShipment.OrdersList) -// } -// <-c -//} +func asncClientBidirectionalRPC(streamProcOrder pb.OrderManagement_ProcessOrdersClient, c chan struct{}) { + for { + combinedShipment, errProcOrder := streamProcOrder.Recv() + if errProcOrder == io.EOF { + break + } + log.Printf("Combined shipment : ", combinedShipment.OrdersList) + } + <-c +} From 7ea86a50e0f66d1ac42ecc0a1dcf3bfb33ea03d1 Mon Sep 17 00:00:00 2001 From: tkms0106 <23391543+tkms0106@users.noreply.github.com> Date: Wed, 12 Aug 2020 11:57:48 +0900 Subject: [PATCH 2/5] Add import wrapper --- ch03/order-service/go/client/main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/ch03/order-service/go/client/main.go b/ch03/order-service/go/client/main.go index 9e82ebc..52724e1 100644 --- a/ch03/order-service/go/client/main.go +++ b/ch03/order-service/go/client/main.go @@ -7,6 +7,7 @@ import ( pb "ordermgt/client/ecommerce" "time" + wrapper "github.com/golang/protobuf/ptypes/wrappers" "google.golang.org/grpc" ) From cba8f775b09f61c0e23834e9fd38169709b1de72 Mon Sep 17 00:00:00 2001 From: tkms0106 <23391543+tkms0106@users.noreply.github.com> Date: Wed, 12 Aug 2020 12:00:17 +0900 Subject: [PATCH 3/5] Add error handling --- ch03/order-service/go/client/main.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ch03/order-service/go/client/main.go b/ch03/order-service/go/client/main.go index 52724e1..32dd2fa 100644 --- a/ch03/order-service/go/client/main.go +++ b/ch03/order-service/go/client/main.go @@ -35,6 +35,9 @@ func main() { // Get Order retrievedOrder, err := client.GetOrder(ctx, &wrapper.StringValue{Value: "106"}) + if err != nil { + log.Fatalf("%v.GetOrder(ctx, &wrapper.StringValue{Value: \"106\"}) = _, %v", client, err) + } log.Print("GetOrder Response -> : ", retrievedOrder) // Search Order : Server streaming scenario From 0a92bc14385567a123b3f087569d26095ab013d4 Mon Sep 17 00:00:00 2001 From: tkms0106 <23391543+tkms0106@users.noreply.github.com> Date: Wed, 12 Aug 2020 12:00:44 +0900 Subject: [PATCH 4/5] Add formatting directive --- ch03/order-service/go/client/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch03/order-service/go/client/main.go b/ch03/order-service/go/client/main.go index 32dd2fa..485592a 100644 --- a/ch03/order-service/go/client/main.go +++ b/ch03/order-service/go/client/main.go @@ -125,7 +125,7 @@ func asncClientBidirectionalRPC(streamProcOrder pb.OrderManagement_ProcessOrders if errProcOrder == io.EOF { break } - log.Printf("Combined shipment : ", combinedShipment.OrdersList) + log.Printf("Combined shipment : %v", combinedShipment.OrdersList) } <-c } From ad1f0e05837bd918f11339584357b51c78d1307c Mon Sep 17 00:00:00 2001 From: tkms0106 <23391543+tkms0106@users.noreply.github.com> Date: Wed, 12 Aug 2020 13:09:37 +0900 Subject: [PATCH 5/5] Fix that both channels read and block each other --- ch03/order-service/go/client/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch03/order-service/go/client/main.go b/ch03/order-service/go/client/main.go index 485592a..e4f2dad 100644 --- a/ch03/order-service/go/client/main.go +++ b/ch03/order-service/go/client/main.go @@ -127,5 +127,5 @@ func asncClientBidirectionalRPC(streamProcOrder pb.OrderManagement_ProcessOrders } log.Printf("Combined shipment : %v", combinedShipment.OrdersList) } - <-c + close(c) }