Skip to content

Commit

Permalink
comments and things
Browse files Browse the repository at this point in the history
  • Loading branch information
menghanl committed Dec 21, 2018
1 parent c09e101 commit a37738a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
27 changes: 20 additions & 7 deletions examples/features/name_resolving/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*
*/

// Binary client is an example client.
package main

import (
Expand All @@ -36,8 +37,6 @@ const (
backendAddr = "localhost:50051"
)

var addrs = []string{backendAddr}

func callUnaryEcho(c ecpb.EchoClient, message string) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
Expand Down Expand Up @@ -65,7 +64,7 @@ func main() {
}
defer passthroughConn.Close()

fmt.Println("--- calling helloworld.Greeter/SayHello with pick_first ---")
fmt.Printf("--- calling helloworld.Greeter/SayHello to \"passthrough:///%s\"\n", backendAddr)
makeRPCs(passthroughConn, 10)

fmt.Println()
Expand All @@ -79,27 +78,39 @@ func main() {
}
defer exampleConn.Close()

fmt.Println("--- calling helloworld.Greeter/SayHello with pick_first ---")
fmt.Printf("--- calling helloworld.Greeter/SayHello to \"%s:///%s\"\n", exampleScheme, exampleServiceName)
makeRPCs(exampleConn, 10)
}

// Following is an example name resolver implementation.

// Following is an example name resolver. It includes a
// ResolverBuilder(https://godoc.org/google.golang.org/grpc/resolver#Builder)
// and a Resolver(https://godoc.org/google.golang.org/grpc/resolver#Resolver).
//
// A ResolverBuilder is registered for a scheme (in this example, "example" is
// the scheme). When a ClientConn is created for this scheme, the
// ResolverBuilder will be picked to build a Resolver. Note that a new Resolver
// is built for each ClientConn. The Resolver will watch the updates for the
// target, and send updates to the ClientConn.

// exampleResolverBuilder is a
// ResolverBuilder(https://godoc.org/google.golang.org/grpc/resolver#Builder).
type exampleResolverBuilder struct{}

func (*exampleResolverBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOption) (resolver.Resolver, error) {
r := &exampleResolver{
target: target,
cc: cc,
addrsStore: map[string][]string{
exampleServiceName: addrs,
exampleServiceName: []string{backendAddr},
},
}
r.start()
return r, nil
}
func (*exampleResolverBuilder) Scheme() string { return exampleScheme }

// exampleResolver is a
// Resolver(https://godoc.org/google.golang.org/grpc/resolver#Resolver).
type exampleResolver struct {
target resolver.Target
cc resolver.ClientConn
Expand All @@ -118,5 +129,7 @@ func (*exampleResolver) ResolveNow(o resolver.ResolveNowOption) {}
func (*exampleResolver) Close() {}

func init() {
// Register the example ResolverBuilder. This is usually done in a package's
// init() function.
resolver.Register(&exampleResolverBuilder{})
}
21 changes: 5 additions & 16 deletions examples/features/name_resolving/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,22 @@
*
*/

// Binary server is an example server.
package main

import (
"context"
"fmt"
"log"
"net"
"sync"

"google.golang.org/grpc"
"google.golang.org/grpc/codes"
ecpb "google.golang.org/grpc/examples/features/proto/echo"
"google.golang.org/grpc/status"
)

var (
addrs = []string{":50051"}
)
const addr = "localhost:50051"

type ecServer struct {
addr string
Expand All @@ -53,6 +51,9 @@ func (s *ecServer) BidirectionalStreamingEcho(ecpb.Echo_BidirectionalStreamingEc
}

func startServer(addr string) {
}

func main() {
lis, err := net.Listen("tcp", addr)
if err != nil {
log.Fatalf("failed to listen: %v", err)
Expand All @@ -64,15 +65,3 @@ func startServer(addr string) {
log.Fatalf("failed to serve: %v", err)
}
}

func main() {
var wg sync.WaitGroup
for _, addr := range addrs {
wg.Add(1)
go func(addr string) {
defer wg.Done()
startServer(addr)
}(addr)
}
wg.Wait()
}

0 comments on commit a37738a

Please sign in to comment.