1/*
2 *
3 * Copyright 2018 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19// Binary client is an example client.
20package main
21
22import (
23	"context"
24	"fmt"
25	"log"
26	"time"
27
28	"google.golang.org/grpc"
29	ecpb "google.golang.org/grpc/examples/features/proto/echo"
30	"google.golang.org/grpc/resolver"
31)
32
33const (
34	exampleScheme      = "example"
35	exampleServiceName = "lb.example.grpc.io"
36)
37
38var addrs = []string{"localhost:50051", "localhost:50052"}
39
40func callUnaryEcho(c ecpb.EchoClient, message string) {
41	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
42	defer cancel()
43	r, err := c.UnaryEcho(ctx, &ecpb.EchoRequest{Message: message})
44	if err != nil {
45		log.Fatalf("could not greet: %v", err)
46	}
47	fmt.Println(r.Message)
48}
49
50func makeRPCs(cc *grpc.ClientConn, n int) {
51	hwc := ecpb.NewEchoClient(cc)
52	for i := 0; i < n; i++ {
53		callUnaryEcho(hwc, "this is examples/load_balancing")
54	}
55}
56
57func main() {
58	pickfirstConn, err := grpc.Dial(
59		fmt.Sprintf("%s:///%s", exampleScheme, exampleServiceName),
60		// grpc.WithBalancerName("pick_first"), // "pick_first" is the default, so this DialOption is not necessary.
61		grpc.WithInsecure(),
62		grpc.WithBlock(),
63	)
64	if err != nil {
65		log.Fatalf("did not connect: %v", err)
66	}
67	defer pickfirstConn.Close()
68
69	fmt.Println("--- calling helloworld.Greeter/SayHello with pick_first ---")
70	makeRPCs(pickfirstConn, 10)
71
72	fmt.Println()
73
74	// Make another ClientConn with round_robin policy.
75	roundrobinConn, err := grpc.Dial(
76		fmt.Sprintf("%s:///%s", exampleScheme, exampleServiceName),
77		grpc.WithBalancerName("round_robin"), // This sets the initial balancing policy.
78		grpc.WithInsecure(),
79		grpc.WithBlock(),
80	)
81	if err != nil {
82		log.Fatalf("did not connect: %v", err)
83	}
84	defer roundrobinConn.Close()
85
86	fmt.Println("--- calling helloworld.Greeter/SayHello with round_robin ---")
87	makeRPCs(roundrobinConn, 10)
88}
89
90// Following is an example name resolver implementation. Read the name
91// resolution example to learn more about it.
92
93type exampleResolverBuilder struct{}
94
95func (*exampleResolverBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) {
96	r := &exampleResolver{
97		target: target,
98		cc:     cc,
99		addrsStore: map[string][]string{
100			exampleServiceName: addrs,
101		},
102	}
103	r.start()
104	return r, nil
105}
106func (*exampleResolverBuilder) Scheme() string { return exampleScheme }
107
108type exampleResolver struct {
109	target     resolver.Target
110	cc         resolver.ClientConn
111	addrsStore map[string][]string
112}
113
114func (r *exampleResolver) start() {
115	addrStrs := r.addrsStore[r.target.Endpoint]
116	addrs := make([]resolver.Address, len(addrStrs))
117	for i, s := range addrStrs {
118		addrs[i] = resolver.Address{Addr: s}
119	}
120	r.cc.UpdateState(resolver.State{Addresses: addrs})
121}
122func (*exampleResolver) ResolveNow(o resolver.ResolveNowOptions) {}
123func (*exampleResolver) Close()                                  {}
124
125func init() {
126	resolver.Register(&exampleResolverBuilder{})
127}
128