Skip to content

Commit

Permalink
use withcancel for streaming RPCs
Browse files Browse the repository at this point in the history
  • Loading branch information
menghanl committed Feb 21, 2018
1 parent ef56e49 commit de08540
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
8 changes: 4 additions & 4 deletions examples/route_guide/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var (
// printFeature gets the feature for the given point.
func printFeature(client pb.RouteGuideClient, point *pb.Point) {
log.Printf("Getting feature for point (%d, %d)", point.Latitude, point.Longitude)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
feature, err := client.GetFeature(ctx, point)
if err != nil {
Expand All @@ -58,7 +58,7 @@ func printFeature(client pb.RouteGuideClient, point *pb.Point) {
// printFeatures lists all the features within the given bounding Rectangle.
func printFeatures(client pb.RouteGuideClient, rect *pb.Rectangle) {
log.Printf("Looking for features within %v", rect)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
stream, err := client.ListFeatures(ctx, rect)
if err != nil {
Expand Down Expand Up @@ -86,7 +86,7 @@ func runRecordRoute(client pb.RouteGuideClient) {
points = append(points, randomPoint(r))
}
log.Printf("Traversing %d points.", len(points))
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
stream, err := client.RecordRoute(ctx)
if err != nil {
Expand Down Expand Up @@ -114,7 +114,7 @@ func runRouteChat(client pb.RouteGuideClient) {
{Location: &pb.Point{Latitude: 0, Longitude: 2}, Message: "Fifth message"},
{Location: &pb.Point{Latitude: 0, Longitude: 3}, Message: "Sixth message"},
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
stream, err := client.RouteChat(ctx)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions examples/route_guide/mock_routeguide/rg_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package mock_routeguide_test
import (
"fmt"
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/golang/protobuf/proto"
Expand Down Expand Up @@ -60,7 +59,7 @@ func TestRouteChat(t *testing.T) {
}

func testRouteChat(client rgpb.RouteGuideClient) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
stream, err := client.RouteChat(ctx)
if err != nil {
Expand Down

0 comments on commit de08540

Please sign in to comment.