diff --git a/.gitmodules b/.gitmodules index 3acf310..08e7a81 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,4 @@ -[submodule "grpc/golang/poc/proto"] - path = grpc/golang/poc/proto - url = https://github.com/GuillaumeFalourd/poc-proto.git + [submodule "grpc/golang/poc/protos"] path = grpc/golang/poc/protos url = https://github.com/GuillaumeFalourd/poc-proto.git diff --git a/grpc/golang/poc/README.md b/grpc/golang/poc/README.md index 12f7b61..1a4d24c 100755 --- a/grpc/golang/poc/README.md +++ b/grpc/golang/poc/README.md @@ -25,14 +25,32 @@ rit grpc golang poc ![Formula](https://user-images.githubusercontent.com/22433243/128401370-a2ba5cd3-d92f-4c44-9950-c67baac24ff5.png) +* * * + ## How does it work? Reference: [Go generated code](https://developers.google.com/protocol-buffers/docs/reference/go-generated#package) 1 - The `protos` submodules as been added at the formula `root` repository based on [this Github repository](https://github.com/GuillaumeFalourd/poc-proto). +- To add a submodule: _e.g `git submodule add https://github.com/GuillaumeFalourd/poc-proto.git protos`_ +- To update a submodule: _e.g `git submodule update --remote --recursive`_ +- To init a submodule after a clone: _e.g `git submodule init` then `git submodule update`_ + 2 - To generate files from the `protos/user/user.proto` file, use the `make gen-go-proto` at the formula root directory (here `grpc/golang/poc`). +_Add a method like this to the Makefile:_ + +```shell +gen-go-proto: + mkdir -p src/pkg/formula + protoc --go_out=src/pkg/formula \ + --go_opt=paths=source_relative \ + --go-grpc_out=src/pkg/formula \ + --go-grpc_opt=paths=source_relative \ + protos/user/user.proto +``` + _Note that the proto file currently needs to have a `go_package` informed to work._ 3 - Files will be generated inside the `./poc/src/pkg/formula` folder on a new `protos/user` directory. @@ -41,6 +59,8 @@ _Note that the proto file currently needs to have a `go_package` informed to wor 5 - Then, implement the formula to call the gRPC service you wish. +* * * + _**Note**: If the `user.proto` is updated from the remote repository. You can update it on the formula using the followigin command_ ```bash diff --git a/grpc/golang/poc/src/pkg/formula/formula.go b/grpc/golang/poc/src/pkg/formula/formula.go index a97ccac..dfff02a 100755 --- a/grpc/golang/poc/src/pkg/formula/formula.go +++ b/grpc/golang/poc/src/pkg/formula/formula.go @@ -24,18 +24,17 @@ type Formula struct { } func (f Formula) Run() { - // Set up a connection to the server. - conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock()) + ctx, cancel := context.WithTimeout(context.Background(), 5000*time.Millisecond) // 5000 Millisecond is the timeout allowed + conn, err := grpc.DialContext(ctx, address, grpc.WithInsecure(), grpc.WithBlock()) if err != nil { - log.Fatalf("did not connect: %v", err) + log.Fatalf("Couldn't connect: %v", err) } defer conn.Close() c := pb.NewUserClient(conn) + defer cancel() // Call server Login method through gRPC. - ctx, cancel := context.WithTimeout(context.Background(), time.Second) - defer cancel() r, err := c.Login(ctx, &pb.LoginRequest{Username: f.Username, Password: f.Password}) if err != nil { log.Fatalf("Unexpected Error: %v", err)