From 25de51fc024f642d9ac888257b1485939a1d4a14 Mon Sep 17 00:00:00 2001 From: Can Guler Date: Fri, 21 Dec 2018 16:55:49 -0800 Subject: [PATCH] example: errors (#2534) * Adds readme. * Fills readme. * Adds readme. * Moves error examples. * Adds port flag. * Changes the flag for clients. * Adds package comments. --- examples/features/deadline/client/main.go | 6 ++--- examples/features/errors/README.md | 26 ++++++++++++++++++- .../errors}/client/main.go | 8 +++++- .../errors}/server/main.go | 13 ++++++---- examples/rpc_errors/README.md | 25 ------------------ 5 files changed, 43 insertions(+), 35 deletions(-) rename examples/{rpc_errors => features/errors}/client/main.go (88%) rename examples/{rpc_errors => features/errors}/server/main.go (91%) delete mode 100644 examples/rpc_errors/README.md diff --git a/examples/features/deadline/client/main.go b/examples/features/deadline/client/main.go index d19178cc..0e262613 100644 --- a/examples/features/deadline/client/main.go +++ b/examples/features/deadline/client/main.go @@ -32,6 +32,8 @@ import ( "google.golang.org/grpc/status" ) +var addr = flag.String("addr", "localhost:50052", "the address to connect to") + func unaryCall(c pb.EchoClient, requestID int, message string, want codes.Code) { // Creates a context with a one second deadline for the RPC. ctx, cancel := context.WithTimeout(context.Background(), time.Second) @@ -68,11 +70,9 @@ func streamingCall(c pb.EchoClient, requestID int, message string, want codes.Co } func main() { - port := flag.Int("port", 50052, "port number") flag.Parse() - target := fmt.Sprintf("localhost:%v", *port) - conn, err := grpc.Dial(target, grpc.WithInsecure()) + conn, err := grpc.Dial(*addr, grpc.WithInsecure()) if err != nil { log.Fatalf("did not connect: %v", err) } diff --git a/examples/features/errors/README.md b/examples/features/errors/README.md index b45b30e1..62eee83d 100644 --- a/examples/features/errors/README.md +++ b/examples/features/errors/README.md @@ -1 +1,25 @@ -Please see [examples/rpc_errors](../../rpc_errors). +# Description + +This example demonstrates the use of status details in grpc errors. + +# Run the sample code + +Run the server: + +```sh +$ go run ./server/main.go +``` +Then run the client in another terminal: + +```sh +$ go run ./client/main.go +``` + +It should succeed and print the greeting it received from the server. +Then run the client again: + +```sh +$ go run ./client/main.go +``` + +This time, it should fail by printing error status details that it received from the server. diff --git a/examples/rpc_errors/client/main.go b/examples/features/errors/client/main.go similarity index 88% rename from examples/rpc_errors/client/main.go rename to examples/features/errors/client/main.go index daccb1ef..4bacff5f 100644 --- a/examples/rpc_errors/client/main.go +++ b/examples/features/errors/client/main.go @@ -16,10 +16,12 @@ * */ +// Binary client is an example client. package main import ( "context" + "flag" "log" "os" "time" @@ -30,9 +32,13 @@ import ( "google.golang.org/grpc/status" ) +var addr = flag.String("addr", "localhost:50052", "the address to connect to") + func main() { + flag.Parse() + // Set up a connection to the server. - conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure()) + conn, err := grpc.Dial(*addr, grpc.WithInsecure()) if err != nil { log.Fatalf("did not connect: %v", err) } diff --git a/examples/rpc_errors/server/main.go b/examples/features/errors/server/main.go similarity index 91% rename from examples/rpc_errors/server/main.go rename to examples/features/errors/server/main.go index 7593aabd..d306c4eb 100644 --- a/examples/rpc_errors/server/main.go +++ b/examples/features/errors/server/main.go @@ -16,10 +16,12 @@ * */ +// Binary server is an example server. package main import ( "context" + "flag" "fmt" "log" "net" @@ -32,9 +34,7 @@ import ( "google.golang.org/grpc/status" ) -const ( - port = ":50051" -) +var port = flag.Int("port", 50052, "port number") // server is used to implement helloworld.GreeterServer. type server struct { @@ -67,11 +67,14 @@ func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloRe } func main() { - log.Printf("server starting on port %s...", port) - lis, err := net.Listen("tcp", port) + flag.Parse() + + address := fmt.Sprintf(":%v", *port) + lis, err := net.Listen("tcp", address) if err != nil { log.Fatalf("failed to listen: %v", err) } + s := grpc.NewServer() pb.RegisterGreeterServer(s, &server{count: make(map[string]int)}) if err := s.Serve(lis); err != nil { diff --git a/examples/rpc_errors/README.md b/examples/rpc_errors/README.md deleted file mode 100644 index 62eee83d..00000000 --- a/examples/rpc_errors/README.md +++ /dev/null @@ -1,25 +0,0 @@ -# Description - -This example demonstrates the use of status details in grpc errors. - -# Run the sample code - -Run the server: - -```sh -$ go run ./server/main.go -``` -Then run the client in another terminal: - -```sh -$ go run ./client/main.go -``` - -It should succeed and print the greeting it received from the server. -Then run the client again: - -```sh -$ go run ./client/main.go -``` - -This time, it should fail by printing error status details that it received from the server.