Skip to content

Commit

Permalink
Added ClientStreaming Operation
Browse files Browse the repository at this point in the history
  • Loading branch information
nik-hil committed Aug 3, 2022
1 parent c55b048 commit a9a650f
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 17 deletions.
1 change: 1 addition & 0 deletions .vscode/dryrun.log
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
make --dry-run --always-make --keep-going --print-directory
make: Entering directory `/Users/nikhar/workspace/go-grpc-course'
grep -E '^[a-zA-Z_-]+:.*?## .*$' Makefile | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $1, $2}'
make: Leaving directory `/Users/nikhar/workspace/go-grpc-course'
Expand Down
14 changes: 7 additions & 7 deletions .vscode/targets.log
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ make all --print-data-base --no-builtin-variables --no-builtin-rules --question
# This program built for i386-apple-darwin11.3.0


# Make data base, printed on Mon Aug 1 14:11:45 2022
# Make data base, printed on Wed Aug 3 09:31:32 2022


# Variables

Expand Down Expand Up @@ -79,7 +80,6 @@ ORIGINAL_XDG_CURRENT_DESKTOP = undefined
# environment
GOMODCACHE = /Users/nikhar/go/pkg/mod
# environment

HOME = /Users/nikhar
# default
MAKEFILEPATH = $(shell /usr/bin/xcode-select -print-path 2>/dev/null || echo /Developer)/Makefiles
Expand Down Expand Up @@ -188,9 +188,9 @@ VSCODE_PID = 472

# Directories

# . (device 16777225, inode 22309043): 6 files, no impossibilities.
# . (device 16777225, inode 22349615): 9 files, no impossibilities.

# 6 files, no impossibilities in 1 directories.
# 9 files, no impossibilities in 1 directories.

# Implicit Rules

Expand Down Expand Up @@ -229,7 +229,7 @@ clean_greet:
# Not a target:
Makefile:
# Implicit rule search has been done.
# Last modified 2022-08-01 14:11:45
# Last modified 2022-08-01 16:56:36
# File has been updated.
# Successfully updated.
# variable set hash-table stats:
Expand Down Expand Up @@ -305,6 +305,7 @@ about:
@echo "Protoc version: $(shell protoc --version)"
@echo "Go version: $(shell go version)"
@echo "Go package: ${PACKAGE}"

@echo "Openssl version: $(shell openssl version)"


Expand Down Expand Up @@ -353,7 +354,6 @@ blog:
# Implicit rule search has not been done.
# File does not exist.
# File has not been updated.

# commands to execute (from `Makefile', line 50):
@${CHECK_DIR_CMD}
protoc -I$@/${PROTO_DIR} --go_opt=module=${PACKAGE} --go_out=. --go-grpc_opt=module=${PACKAGE} --go-grpc_out=. $@/${PROTO_DIR}/*.proto
Expand All @@ -374,6 +374,6 @@ blog:
# strcache size: total = 4096 / max = 4096 / min = 4096 / avg = 4096
# strcache free: total = 4087 / max = 4087 / min = 4087 / avg = 4087

# Finished Make data base on Mon Aug 1 14:11:45 2022
# Finished Make data base on Wed Aug 3 09:31:32 2022


Binary file modified bin/greet/client
Binary file not shown.
Binary file modified bin/greet/server
Binary file not shown.
28 changes: 28 additions & 0 deletions greet/client/greet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"io"
"log"
"time"

pb "github.com/nik-hil/go-grpc-course/greet/proto"
)
Expand Down Expand Up @@ -43,3 +44,30 @@ func doGreetManyTimes(c pb.GreetServiceClient) {
}

}

func doLongGreet(c pb.GreetServiceClient) {
log.Printf("doLongGreet was invoked")

reqs := []*pb.GreetRequest{
{FirstName: "Nikhil"},
{FirstName: "Test1"},
{FirstName: "Test2"},
}
stream, err := c.LongGreet(context.Background())

if err != nil {
log.Fatalf("Error while calling stream in LongGreet %v\n", err)
}

for _, req := range reqs {
log.Printf("Sending req: %v\n", req)
stream.Send(req)
time.Sleep(1 * time.Second)
}

res, err := stream.CloseAndRecv()
if err != nil {
log.Fatal("Error in closing LongGreet: %v\n", err)
}
log.Printf("LongGreet: %s\n", res.Result)
}
3 changes: 2 additions & 1 deletion greet/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ func main() {
defer conn.Close()
c := pb.NewGreetServiceClient(conn)
// doGreet(c)
doGreetManyTimes(c)
// doGreetManyTimes(c)
doLongGreet(c)
}
24 changes: 15 additions & 9 deletions greet/proto/greet.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions greet/proto/greet.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ message GreetResponse{
service GreetService{
rpc Greet (GreetRequest) returns (GreetResponse);
rpc GreetManyTimes (GreetRequest) returns (stream GreetResponse);
rpc LongGreet (stream GreetRequest) returns (GreetResponse);
}
70 changes: 70 additions & 0 deletions greet/proto/greet_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions greet/server/greet.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"io"
"log"

pb "github.com/nik-hil/go-grpc-course/greet/proto"
Expand All @@ -26,3 +27,23 @@ func (s *Server) GreetManyTimes(in *pb.GreetRequest, stream pb.GreetService_Gree
}
return nil
}

func (s *Server) LongGreet(stream pb.GreetService_LongGreetServer) error {
log.Printf("LongGreet function was invoked")
i := 0
res := ""
for {
req, err := stream.Recv()
if err == io.EOF {
return stream.SendAndClose(&pb.GreetResponse{
Result: res,
})
}
if err != nil {
log.Fatalf("Error while reading the stream: %v\n", err)
}
log.Printf("LongGreet %s count %d\n", req.FirstName, i)
i = i + 1
res += fmt.Sprintf("Hello %s! \t", req.FirstName)
}
}

0 comments on commit a9a650f

Please sign in to comment.