forked from spiral-modules/php-grpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
167 lines (138 loc) · 3.95 KB
/
proxy.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
package grpc
import (
"encoding/json"
"fmt"
"github.com/spiral/roadrunner"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status"
"strconv"
"strings"
)
// base interface for Proxy class
type proxyService interface {
// RegisterMethod registers new RPC method.
RegisterMethod(method string)
// ServiceDesc returns service description for the proxy.
ServiceDesc() *grpc.ServiceDesc
}
// carry details about service, method and RPC context to PHP process
type rpcContext struct {
Service string `json:"service"`
Method string `json:"method"`
Context map[string][]string `json:"context"`
}
// Proxy manages GRPC/RoadRunner bridge.
type Proxy struct {
rr *roadrunner.Server
name string
metadata string
methods []string
}
// NewProxy creates new service proxy object.
func NewProxy(name string, metadata string, rr *roadrunner.Server) *Proxy {
return &Proxy{
rr: rr,
name: name,
metadata: metadata,
methods: make([]string, 0),
}
}
// RegisterMethod registers new RPC method.
func (p *Proxy) RegisterMethod(method string) {
p.methods = append(p.methods, method)
}
// ServiceDesc returns service description for the proxy.
func (p *Proxy) ServiceDesc() *grpc.ServiceDesc {
desc := &grpc.ServiceDesc{
ServiceName: p.name,
Metadata: p.metadata,
HandlerType: (*proxyService)(nil),
Methods: []grpc.MethodDesc{},
Streams: []grpc.StreamDesc{},
}
// Registering methods
for _, m := range p.methods {
desc.Methods = append(desc.Methods, grpc.MethodDesc{
MethodName: m,
Handler: p.methodHandler(m),
})
}
return desc
}
// Generate method handler proxy.
func (p *Proxy) methodHandler(method string) func(
srv interface{},
ctx context.Context,
dec func(interface{},
) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
return func(
srv interface{},
ctx context.Context,
dec func(interface{}) error,
interceptor grpc.UnaryServerInterceptor,
) (interface{}, error) {
in := rawMessage{}
if err := dec(&in); err != nil {
return nil, wrapError(err)
}
if interceptor == nil {
return p.invoke(ctx, method, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: fmt.Sprintf("/%s/%s", p.name, method),
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return p.invoke(ctx, method, req.(rawMessage))
}
return interceptor(ctx, in, info, handler)
}
}
func (p *Proxy) invoke(ctx context.Context, method string, in rawMessage) (interface{}, error) {
payload, err := p.makePayload(ctx, method, in)
if err != nil {
return nil, err
}
resp, err := p.rr.Exec(payload)
if err != nil {
return nil, wrapError(err)
}
return rawMessage(resp.Body), nil
}
// makePayload generates RoadRunner compatible payload based on GRPC message. todo: return error
func (p *Proxy) makePayload(ctx context.Context, method string, body rawMessage) (*roadrunner.Payload, error) {
ctxMD := make(map[string][]string)
if md, ok := metadata.FromIncomingContext(ctx); ok {
for k, v := range md {
ctxMD[k] = v
}
}
if pr, ok := peer.FromContext(ctx); ok {
ctxMD[":peer.address"] = []string{pr.Addr.String()}
if pr.AuthInfo != nil {
ctxMD[":peer.auth-type"] = []string{pr.AuthInfo.AuthType()}
}
}
ctxData, err := json.Marshal(rpcContext{Service: p.name, Method: method, Context: ctxMD})
if err != nil {
return nil, err
}
return &roadrunner.Payload{Context: ctxData, Body: body}, nil
}
// mounts proper error code for the error
func wrapError(err error) error {
// internal agreement
if strings.Index(err.Error(), "|:|") != -1 {
chunks := strings.Split(err.Error(), "|:|")
code := codes.Internal
if phpCode, err := strconv.Atoi(chunks[0]); err == nil {
code = codes.Code(phpCode)
}
return status.Error(code, chunks[1])
}
return status.Error(codes.Internal, err.Error())
}