コード例 #1
0
ファイル: client.go プロジェクト: dylanpoe/golang.org
// search issues a search for query and prints the result.
func search(client pb.GoogleClient, query string) {
	ctx, cancel := context.WithTimeout(context.Background(), 80*time.Millisecond) // HL
	defer cancel()
	req := &pb.Request{Query: query}    // HL
	res, err := client.Search(ctx, req) // HL
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(res) // HL
}
コード例 #2
0
ファイル: client.go プロジェクト: dylanpoe/golang.org
// watch runs a Watch RPC and prints the result stream.
func watch(client pb.GoogleClient, query string) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	req := &pb.Request{Query: query}      // HL
	stream, err := client.Watch(ctx, req) // HL
	if err != nil {
		log.Fatal(err)
	}
	for {
		res, err := stream.Recv() // HL
		if err == io.EOF {        // HL
			fmt.Println("and now your watch is ended")
			return
		}
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println(res) // HL
	}
}
コード例 #3
0
ファイル: frontend.go プロジェクト: dylanpoe/golang.org
// watchBackend runs Watch on a single backend and sends results on c.
// watchBackend returns when ctx.Done is closed or stream.Recv fails.
func watchBackend(ctx context.Context, backend pb.GoogleClient, req *pb.Request, c chan<- result) {
	stream, err := backend.Watch(ctx, req) // HL
	if err != nil {
		select {
		case c <- result{err: err}: // HL
		case <-ctx.Done():
		}
		return
	}
	for {
		res, err := stream.Recv() // HL
		select {
		case c <- result{res, err}: // HL
			if err != nil {
				return
			}
		case <-ctx.Done():
			return
		}
	}
}