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 5 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
55 changes: 55 additions & 0 deletions examples/deadline/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
*
* 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 client
canguler marked this conversation as resolved.
Show resolved Hide resolved

import (
"context"
"log"
"time"

"google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
)

// ConnectAndRequest sets up a connection to the server,
// makes a request and returns, if successful, the reply.
func ConnectAndRequest(name string) (*pb.HelloReply, error) {
conn, err := grpc.Dial("localhost:50052", grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()

c := pb.NewGreeterClient(conn)

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
canguler marked this conversation as resolved.
Show resolved Hide resolved
defer cancel()

req := &pb.HelloRequest{Name: name}

log.Println("# NEW REQUEST #")
log.Printf("Request: { %+v}", req)
rep, err := c.SayHello(ctx, req)
if err != nil {
log.Printf("No reply: %v", err)
return nil, err
}
log.Printf("Reply: { %+v}", rep)
return rep, nil
}
canguler marked this conversation as resolved.
Show resolved Hide resolved
34 changes: 34 additions & 0 deletions examples/deadline/clientmain/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
*
* 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

import (
"google.golang.org/grpc/examples/deadline/client"
)

func main() {
// A successful request
client.ConnectAndRequest("world")
// Exceeds deadline
client.ConnectAndRequest("delay")
// A successful request with propagated deadline
client.ConnectAndRequest("[propagate me]world")
// Exceeds propagated deadline
client.ConnectAndRequest("[propagate me][propagate me]world")
}
62 changes: 62 additions & 0 deletions examples/deadline/servermain/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
*
* 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

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

"google.golang.org/grpc/examples/deadline/client"

"google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
)

type server struct{}

// SayHello implements helloworld.GreeterServer
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
name := in.Name
if strings.HasPrefix(name, "[propagate me]") {
time.Sleep(800 * time.Millisecond)
name = strings.TrimPrefix(name, "[propagate me]")
return client.ConnectAndRequest(name)
canguler marked this conversation as resolved.
Show resolved Hide resolved
}

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

return &pb.HelloReply{Message: "Hello " + name}, nil
}

func main() {
lis, err := net.Listen("tcp", ":50052")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{})
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}