diff --git a/internal/helloworld/greeter_server.go b/internal/helloworld/greeter_server.go index 47c76165..9a4a175d 100644 --- a/internal/helloworld/greeter_server.go +++ b/internal/helloworld/greeter_server.go @@ -3,7 +3,9 @@ package helloworld import ( "fmt" "io" + "math/rand" "sync" + "time" context "golang.org/x/net/context" "google.golang.org/grpc/stats" @@ -35,12 +37,19 @@ type Greeter struct { callCounts map[CallType]int } +func RandomSleep() { + msCount := rand.Intn(4) + 1 + time.Sleep(time.Millisecond * time.Duration(msCount)) +} + // SayHello implements helloworld.GreeterServer func (s *Greeter) SayHello(ctx context.Context, in *HelloRequest) (*HelloReply, error) { s.mutex.Lock() s.callCounts[Unary]++ s.mutex.Unlock() + RandomSleep() + return &HelloReply{Message: "Hello " + in.Name}, nil } @@ -50,6 +59,8 @@ func (s *Greeter) SayHellos(req *HelloRequest, stream Greeter_SayHellosServer) e s.callCounts[ServerStream]++ s.mutex.Unlock() + RandomSleep() + for _, msg := range s.streamData { if err := stream.Send(msg); err != nil { return err @@ -65,6 +76,8 @@ func (s *Greeter) SayHelloCS(stream Greeter_SayHelloCSServer) error { s.callCounts[ClientStream]++ s.mutex.Unlock() + RandomSleep() + msgCount := 0 for { @@ -86,6 +99,8 @@ func (s *Greeter) SayHelloBidi(stream Greeter_SayHelloBidiServer) error { s.callCounts[Bidi]++ s.mutex.Unlock() + RandomSleep() + for { in, err := stream.Recv() if err == io.EOF { @@ -197,4 +212,4 @@ func (c *HWStatsHandler) HandleRPC(ctx context.Context, rs stats.RPCStats) { // TagRPC implements per-RPC context management. func (c *HWStatsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context { return ctx -} +} \ No newline at end of file