-
Notifications
You must be signed in to change notification settings - Fork 5
/
server_multiplex.go
48 lines (38 loc) · 1.1 KB
/
server_multiplex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"context"
"fmt"
"net"
"strings"
"github.com/gunsluo/go-example/grpc/helloworld/pb"
"github.com/sirupsen/logrus"
"github.com/soheilhy/cmux"
"google.golang.org/grpc"
)
const (
sAddress = "0.0.0.0:19000"
)
type Service struct {
}
func (s *Service) SayHello(ctx context.Context, req *pb.HelloRequest) (*pb.HelloReply, error) {
fmt.Println("==>", req.Name)
return &pb.HelloReply{
Message: "hello, " + req.Name,
}, nil
}
func main() {
server := grpc.NewServer()
pb.RegisterGreeterServer(server, &Service{})
listener, err := net.Listen("tcp", sAddress)
if err != nil {
logrus.WithError(err).Fatalf("Can't connect serve")
}
m := cmux.New(listener)
//grpcl := m.Match(cmux.HTTP2HeaderFieldPrefix("content-type", "application/grpc"))
grpcl := m.MatchWithWriters(cmux.HTTP2MatchHeaderFieldSendSettings("content-type", "application/grpc"))
logrus.WithField("addr", sAddress).Println("Starting server")
go server.Serve(grpcl)
if err := m.Serve(); !strings.Contains(err.Error(), "use of closed network connection") {
logrus.WithError(err).Fatalf("multiplex port serve")
}
}