Skip to content

Commit 187e357

Browse files
authored
examples: multiplex (#2477)
1 parent 49616eb commit 187e357

File tree

3 files changed

+167
-0
lines changed

3 files changed

+167
-0
lines changed

Diff for: examples/features/multiplex/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Multiplex
2+
3+
A `grpc.ClientConn` can be shared by two stubs and two services can share a
4+
`grpc.Server`. This example illustrates how to perform both types of sharing.
5+
6+
```
7+
go run server/main.go
8+
```
9+
10+
```
11+
go run client/main.go
12+
```

Diff for: examples/features/multiplex/client/main.go

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
package main
20+
21+
import (
22+
"context"
23+
"fmt"
24+
"log"
25+
"time"
26+
27+
"google.golang.org/grpc"
28+
ecpb "google.golang.org/grpc/examples/features/proto/echo"
29+
hwpb "google.golang.org/grpc/examples/helloworld/helloworld"
30+
)
31+
32+
const (
33+
address = "localhost:50051"
34+
)
35+
36+
// callSayHello calls SayHello on c with the given name, and prints the
37+
// response.
38+
func callSayHello(c hwpb.GreeterClient, name string) {
39+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
40+
defer cancel()
41+
r, err := c.SayHello(ctx, &hwpb.HelloRequest{Name: name})
42+
if err != nil {
43+
log.Fatalf("client.SayHello(_) = _, %v", err)
44+
}
45+
fmt.Println("Greeting: ", r.Message)
46+
}
47+
48+
func callUnaryEcho(client ecpb.EchoClient, message string) {
49+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
50+
defer cancel()
51+
resp, err := client.UnaryEcho(ctx, &ecpb.EchoRequest{Message: message})
52+
if err != nil {
53+
log.Fatalf("client.UnaryEcho(_) = _, %v: ", err)
54+
}
55+
fmt.Println("UnaryEcho: ", resp.Message)
56+
}
57+
58+
func main() {
59+
// Set up a connection to the server.
60+
conn, err := grpc.Dial(address, grpc.WithInsecure())
61+
if err != nil {
62+
log.Fatalf("did not connect: %v", err)
63+
}
64+
defer conn.Close()
65+
66+
fmt.Println("--- calling helloworld.Greeter/SayHello ---")
67+
// Make a greeter client and send an RPC.
68+
hwc := hwpb.NewGreeterClient(conn)
69+
callSayHello(hwc, "multiplex")
70+
71+
fmt.Println()
72+
fmt.Println("--- calling routeguide.RouteGuide/GetFeature ---")
73+
// Make a routeguild client with the same ClientConn.
74+
rgc := ecpb.NewEchoClient(conn)
75+
callUnaryEcho(rgc, "this is examples/multiplex")
76+
}

Diff for: examples/features/multiplex/server/main.go

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
package main
20+
21+
import (
22+
"context"
23+
"log"
24+
"net"
25+
26+
"google.golang.org/grpc"
27+
"google.golang.org/grpc/codes"
28+
ecpb "google.golang.org/grpc/examples/features/proto/echo"
29+
hwpb "google.golang.org/grpc/examples/helloworld/helloworld"
30+
"google.golang.org/grpc/status"
31+
)
32+
33+
const (
34+
port = ":50051"
35+
)
36+
37+
// hwServer is used to implement helloworld.GreeterServer.
38+
type hwServer struct{}
39+
40+
// SayHello implements helloworld.GreeterServer
41+
func (s *hwServer) SayHello(ctx context.Context, in *hwpb.HelloRequest) (*hwpb.HelloReply, error) {
42+
return &hwpb.HelloReply{Message: "Hello " + in.Name}, nil
43+
}
44+
45+
type ecServer struct{}
46+
47+
func (s *ecServer) UnaryEcho(ctx context.Context, req *ecpb.EchoRequest) (*ecpb.EchoResponse, error) {
48+
return &ecpb.EchoResponse{Message: req.Message}, nil
49+
}
50+
51+
func (s *ecServer) ServerStreamingEcho(*ecpb.EchoRequest, ecpb.Echo_ServerStreamingEchoServer) error {
52+
return status.Errorf(codes.Unimplemented, "not implemented")
53+
}
54+
55+
func (s *ecServer) ClientStreamingEcho(ecpb.Echo_ClientStreamingEchoServer) error {
56+
return status.Errorf(codes.Unimplemented, "not implemented")
57+
}
58+
59+
func (s *ecServer) BidirectionalStreamingEcho(ecpb.Echo_BidirectionalStreamingEchoServer) error {
60+
return status.Errorf(codes.Unimplemented, "not implemented")
61+
}
62+
63+
func main() {
64+
lis, err := net.Listen("tcp", port)
65+
if err != nil {
66+
log.Fatalf("failed to listen: %v", err)
67+
}
68+
s := grpc.NewServer()
69+
70+
// Register Greeter on the server.
71+
hwpb.RegisterGreeterServer(s, &hwServer{})
72+
73+
// Register RouteGuide on the same server.
74+
ecpb.RegisterEchoServer(s, &ecServer{})
75+
76+
if err := s.Serve(lis); err != nil {
77+
log.Fatalf("failed to serve: %v", err)
78+
}
79+
}

0 commit comments

Comments
 (0)