-
Notifications
You must be signed in to change notification settings - Fork 2
/
proxy.grpc.go
211 lines (194 loc) · 5.03 KB
/
proxy.grpc.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"context"
"errors"
"sync/atomic"
// "fmt"
"io"
"strconv"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/anypb"
)
func redirectGRPC(ss grpc.ServerStream, conn *grpc.ClientConn, custom map[string][]string) error {
fullMethodName, ok := grpc.MethodFromServerStream(ss)
if !ok {
return status.Errorf(codes.Internal, "failed to get method from server stream")
}
ssctx := ss.Context()
md, _ := metadata.FromIncomingContext(ssctx)
for name, h := range custom { // 注入自定义头部
md.Set(name, h...)
}
outCtx := metadata.NewOutgoingContext(ssctx, md.Copy())
redirectTo, err := grpc.NewClientStream(outCtx, &grpc.StreamDesc{
ServerStreams: true,
ClientStreams: true,
}, conn, fullMethodName)
if err != nil {
return status.Errorf(codes.Canceled, "the ClientConn may has been closed")
}
ErrChanA2B := redirectGRPC2server(ss, redirectTo) // req
ErrChanB2A := redirectGRPC2client(redirectTo, ss) // resp
for i := 0; i < 2; i++ {
L:
select {
case err = <-ErrChanA2B:
if err == io.EOF {
// success
_ = redirectTo.CloseSend()
break L
} else {
return status.Errorf(codes.Internal, "failed proxying s2c: %v", err)
}
case err = <-ErrChanB2A:
ss.SetTrailer(redirectTo.Trailer())
if err != io.EOF {
return err
}
return nil
}
}
return status.Errorf(codes.Internal, "should never reach this stage.")
}
func redirectGRPC2server(src grpc.ServerStream, dst grpc.ClientStream) chan error {
ret := make(chan error, 1)
go func() {
f := &anypb.Any{}
for i := 0; ; i++ {
if err := src.RecvMsg(f); err != nil {
ret <- err // this can be io.EOF which is happy case
break
}
if err := dst.SendMsg(f); err != nil {
ret <- err
break
}
}
}()
return ret
}
func redirectGRPC2client(src grpc.ClientStream, dst grpc.ServerStream) chan error {
ret := make(chan error, 1)
go func() {
f := &anypb.Any{}
if err := src.RecvMsg(f); err != nil {
ret <- err // this can be io.EOF which is happy case
return
}
md, err := src.Header()
if err != nil {
ret <- err
return
}
if err := dst.SendHeader(md); err != nil {
ret <- err
return
}
if err := dst.SendMsg(f); err != nil {
ret <- err
return
}
for {
if err := src.RecvMsg(f); err != nil {
ret <- err
break
}
if err := dst.SendMsg(f); err != nil {
ret <- err
break
}
}
}()
return ret
}
func handlerInGRPC(nodeActiveContext context.Context) func(_ interface{}, s grpc.ServerStream) (err error) {
return func(_ interface{}, s grpc.ServerStream) (err error) {
md, ok := metadata.FromIncomingContext(s.Context())
trace_id := "UNKNOWN"
from := "UNKNOWN"
if ok {
trace_ids := md.Get("trace_id")
if len(trace_ids) > 0 {
trace_id = trace_ids[0]
}
froms := md.Get("from")
if len(froms) > 0 {
from = froms[0]
}
}
srv_name, mth := fullServiceNameFrom(s)
log_f := logrus.Fields{"direction": "IN", "want_srv": srv_name, "want_mth": mth, "trace_id": trace_id, "from": from}
logger.WithFields(log_f).Info()
if _, ok := CONFIG.AllowService["*"]; !ok {
if _, ok := CONFIG.AllowService[from]; !ok {
logger.WithFields(log_f).Warn("not allowed service")
return errors.New("not allowed service")
}
}
conn, _, err := P.local.Conn(nodeActiveContext)
if err != nil {
logger.WithFields(log_f).Error(err)
return err
}
err = redirectGRPC(s, conn, nil)
if err != nil {
logger.WithFields(log_f).Error(err)
return err
}
return nil
}
}
var reqid int64 = 1
func makereqid() int {
if reqid > 1000 {
atomic.StoreInt64(&reqid, 1)
} else {
atomic.AddInt64(&reqid, 1)
}
return int(reqid)
}
func handlerOutGRPC(nodeActiveContext context.Context) func(_ interface{}, s grpc.ServerStream) (err error) {
return func(_ interface{}, s grpc.ServerStream) (err error) {
md, ok := metadata.FromIncomingContext(s.Context())
var trace string
reqid := int(makereqid())
if ok {
trace_ids := md.Get("trace_id")
if len(trace_ids) > 0 {
trace = trace_ids[0]
goto HaveTrace
}
}
trace = strconv.Itoa(reqid + int(node_id))
HaveTrace:
srv_name, mth := fullServiceNameFrom(s)
log_f := logrus.Fields{"direction": "OUT", "want_srv": srv_name, "want_mth": mth, "trace_id": trace}
logger.WithFields(log_f).Info()
endpoint := P.discoverer.direct(srv_name)
if endpoint == nil {
logger.WithFields(log_f).Error("couldnt find lived node")
return errors.New("couldnt find " + srv_name)
}
conn, delay, err := (*nodeGRPC)(endpoint).Conn(nodeActiveContext)
if err != nil {
logger.WithFields(log_f).Error("get redirect_to conn failed", err)
endpoint.rm()
return errors.New("get redirect_to conn failed")
}
err = redirectGRPC(s, conn, map[string][]string{
"trace_id": {trace},
"from": {string(name())},
})
if err != nil {
logger.WithFields(log_f).Error("redirect failed", err)
endpoint.rm()
return err
}
endpoint.SetDelay(delay)
return nil
}
}