This repository has been archived by the owner on Oct 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
/
cmux.go
147 lines (119 loc) · 2.99 KB
/
cmux.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package cmux
import (
"context"
"net"
"net/http"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/soheilhy/cmux"
"github.com/why444216978/go-util/assert"
"google.golang.org/grpc"
"google.golang.org/protobuf/encoding/protojson"
"github.com/why444216978/gin-api/library/logger"
"github.com/why444216978/gin-api/server"
serverGRPC "github.com/why444216978/gin-api/server/grpc"
)
type (
RegisterHTTP func(ctx context.Context, mux *runtime.ServeMux, conn *grpc.ClientConn) error
)
type Option struct {
logger logger.Logger
registerHTTP RegisterHTTP
httpServer *http.Server
}
type OptionFunc func(*Option)
func WithHTTP(httpServer *http.Server, registerHTTP RegisterHTTP) OptionFunc {
return func(s *Option) {
s.httpServer = httpServer
s.registerHTTP = registerHTTP
}
}
type CMUXServer struct {
*Option
ctx context.Context
endpoint string
registers []serverGRPC.Register
tcpMux cmux.CMux
}
var _ server.Server = (*CMUXServer)(nil)
func NewCMUX(endpoint string, registers []serverGRPC.Register, opts ...OptionFunc) *CMUXServer {
if len(registers) < 1 {
panic("len(registers) < 1")
}
option := &Option{}
for _, o := range opts {
o(option)
}
s := &CMUXServer{
Option: option,
ctx: context.Background(),
registers: registers,
endpoint: endpoint,
}
return s
}
func (s *CMUXServer) Start() (err error) {
listener, err := net.Listen("tcp", s.endpoint)
if err != nil {
return
}
s.tcpMux = cmux.New(listener)
go s.startGRPC()
go s.startHTTP()
return s.tcpMux.Serve()
}
func (s *CMUXServer) startGRPC() {
grpcServer := grpc.NewServer(serverGRPC.NewServerOption(serverGRPC.ServerOptionLogger(s.logger))...)
for _, r := range s.registers {
if r.RegisterGRPC == nil {
panic("r.RegisterGRPC == nil")
}
r.RegisterGRPC(grpcServer)
}
serverGRPC.RegisterTools(grpcServer)
listener := s.tcpMux.MatchWithWriters(cmux.HTTP2MatchHeaderFieldPrefixSendSettings("content-type", "application/grpc"))
if err := grpcServer.Serve(listener); err != nil {
panic(err)
}
}
func (s *CMUXServer) startHTTP() (err error) {
defer func() {
if err != nil {
panic(err)
}
}()
if assert.IsNil(s.registerHTTP) {
return
}
if assert.IsNil(s.httpServer) {
panic("httpServer is nil")
}
grpcConn, err := grpc.DialContext(s.ctx, s.endpoint, serverGRPC.NewDialOption()...)
if err != nil {
return
}
mux := runtime.NewServeMux(
runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{
MarshalOptions: protojson.MarshalOptions{
UseProtoNames: true,
UseEnumNumbers: true,
},
}),
runtime.WithErrorHandler(runtime.DefaultHTTPErrorHandler),
)
if err = s.registerHTTP(s.ctx, mux, grpcConn); err != nil {
return
}
router := http.NewServeMux()
router.Handle("/", mux)
s.httpServer.Addr = s.endpoint
s.httpServer.Handler = router
listener := s.tcpMux.Match(cmux.HTTP1Fast())
if err = s.httpServer.Serve(listener); err != nil {
return
}
return
}
func (s *CMUXServer) Close() (err error) {
s.tcpMux.Close()
return
}