Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example: deadline #2494

Merged
merged 11 commits into from
Dec 14, 2018
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions examples/features/deadline/client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
*
* Copyright 2018 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package main
canguler marked this conversation as resolved.
Show resolved Hide resolved

import (
"context"
"log"
"time"

"google.golang.org/grpc/status"

canguler marked this conversation as resolved.
Show resolved Hide resolved
"google.golang.org/grpc/codes"
pb "google.golang.org/grpc/examples/features/proto/echo"

"google.golang.org/grpc"
)

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)
defer cancel()

req := &pb.EchoRequest{Message: message}

_, err := c.UnaryEcho(ctx, req)
got := status.Code(err)
if got != want {
log.Fatalf("[%v] wanted = %v, got = %v (error code)", requestID, want, got)
}
}

func streamingCall(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)
defer cancel()

stream, err := c.BidirectionalStreamingEcho(ctx)
if err != nil {
log.Printf("Stream err: %v", err)
return
}

err = stream.Send(&pb.EchoRequest{Message: message})
if err != nil {
log.Printf("Send error: %v", err)
return
}

_, err = stream.Recv()

got := status.Code(err)
if got != want {
log.Fatalf("[%v] wanted = %v, got = %v (error code)", requestID, want, got)
}
}

func main() {
conn, err := grpc.Dial("localhost:50052", grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()

c := pb.NewEchoClient(conn)

// A successful request
unaryCall(c, 1, "world", codes.OK)
// Exceeds deadline
unaryCall(c, 2, "delay", codes.DeadlineExceeded)
// A successful request with propagated deadline
unaryCall(c, 3, "[propagate me]world", codes.OK)
// Exceeds propagated deadline
unaryCall(c, 4, "[propagate me][propagate me]world", codes.DeadlineExceeded)
// Receives a response from the stream successfully.
streamingCall(c, 5, "[propagate me]world", codes.OK)
// Exceeds propagated deadline before receiving a response
streamingCall(c, 6, "[propagate me][propagate me]world", codes.DeadlineExceeded)
}
123 changes: 123 additions & 0 deletions examples/features/deadline/server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
*
* Copyright 2018 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package main
canguler marked this conversation as resolved.
Show resolved Hide resolved

import (
"context"
"io"
"log"
"net"
"strings"
"time"

"google.golang.org/grpc"
pb "google.golang.org/grpc/examples/features/proto/echo"
)

var c pb.EchoClient
canguler marked this conversation as resolved.
Show resolved Hide resolved

// server is used to implement EchoServer.
type server struct{}
canguler marked this conversation as resolved.
Show resolved Hide resolved

func (s *server) UnaryEcho(ctx context.Context, req *pb.EchoRequest) (*pb.EchoResponse, error) {
message := req.Message
if strings.HasPrefix(message, "[propagate me]") {
time.Sleep(800 * time.Millisecond)
message = strings.TrimPrefix(message, "[propagate me]")
return c.UnaryEcho(ctx, &pb.EchoRequest{Message: message})
}

if message == "delay" {
time.Sleep(1500 * time.Millisecond)
}

return &pb.EchoResponse{Message: req.Message}, nil
}

func (s *server) ServerStreamingEcho(req *pb.EchoRequest, stream pb.Echo_ServerStreamingEchoServer) error {
for i := 0; i < 10; i++ {
canguler marked this conversation as resolved.
Show resolved Hide resolved
if err := stream.Send(&pb.EchoResponse{Message: req.Message}); err != nil {
return err
}
}
return nil
}

func (s *server) ClientStreamingEcho(stream pb.Echo_ClientStreamingEchoServer) error {
var lastMessage string
canguler marked this conversation as resolved.
Show resolved Hide resolved
for {
req, err := stream.Recv()
if err == io.EOF {
return stream.SendAndClose(&pb.EchoResponse{Message: lastMessage})
}
if err != nil {
return err
}
lastMessage = req.Message
}
}

func (s *server) BidirectionalStreamingEcho(stream pb.Echo_BidirectionalStreamingEchoServer) error {
for {
req, err := stream.Recv()
if err == io.EOF {
return nil
canguler marked this conversation as resolved.
Show resolved Hide resolved
}
if err != nil {
return err
}

message := req.Message
if strings.HasPrefix(message, "[propagate me]") {
time.Sleep(800 * time.Millisecond)
message = strings.TrimPrefix(message, "[propagate me]")
res, err := c.UnaryEcho(stream.Context(), &pb.EchoRequest{Message: message})
if err != nil {
return err
}
stream.Send(res)
}

if message == "delay" {
time.Sleep(1500 * time.Millisecond)
}
stream.Send(&pb.EchoResponse{Message: message})
}
}

func main() {
lis, err := net.Listen("tcp", ":50052")
canguler marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterEchoServer(s, &server{})
canguler marked this conversation as resolved.
Show resolved Hide resolved

conn, err := grpc.Dial("localhost:50052", grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()

c = pb.NewEchoClient(conn)

if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}