-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathcoprocess_grpc.go
111 lines (92 loc) · 2.9 KB
/
coprocess_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
package gateway
import (
"context"
"errors"
"net"
"net/url"
"time"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/coprocess"
)
var (
grpcConnection *grpc.ClientConn
grpcClient coprocess.DispatcherClient
)
// GRPCDispatcher implements a coprocess.Dispatcher
type GRPCDispatcher struct {
coprocess.Dispatcher
}
func (gw *Gateway) dialer(addr string, timeout time.Duration) (net.Conn, error) {
grpcURL, err := url.Parse(gw.GetConfig().CoProcessOptions.CoProcessGRPCServer)
if err != nil {
log.WithFields(logrus.Fields{
"prefix": "coprocess",
}).Error(err)
return nil, err
}
if grpcURL == nil || gw.GetConfig().CoProcessOptions.CoProcessGRPCServer == "" {
errString := "No gRPC URL is set!"
log.WithFields(logrus.Fields{
"prefix": "coprocess",
}).Error(errString)
return nil, errors.New(errString)
}
grpcURLString := gw.GetConfig().CoProcessOptions.CoProcessGRPCServer[len(grpcURL.Scheme)+3:]
return net.DialTimeout(grpcURL.Scheme, grpcURLString, timeout)
}
// Dispatch takes a CoProcessMessage and sends it to the CP.
func (d *GRPCDispatcher) Dispatch(object *coprocess.Object) (*coprocess.Object, error) {
return grpcClient.Dispatch(context.Background(), object)
}
// DispatchEvent dispatches a Tyk event.
func (d *GRPCDispatcher) DispatchEvent(eventJSON []byte) {
eventObject := &coprocess.Event{
Payload: string(eventJSON),
}
_, err := grpcClient.DispatchEvent(context.Background(), eventObject)
if err != nil {
log.WithFields(logrus.Fields{
"prefix": "coprocess",
}).Error(err)
}
}
// Reload triggers a reload affecting CP middlewares and event handlers.
func (d *GRPCDispatcher) Reload() {}
// HandleMiddlewareCache isn't used by gRPC.
func (d *GRPCDispatcher) HandleMiddlewareCache(b *apidef.BundleManifest, basePath string) {}
func (gw *Gateway) grpcCallOpts() grpc.DialOption {
recvSize := gw.GetConfig().CoProcessOptions.GRPCRecvMaxSize
sendSize := gw.GetConfig().CoProcessOptions.GRPCSendMaxSize
var opts []grpc.CallOption
if recvSize > 0 {
opts = append(opts, grpc.MaxCallRecvMsgSize(recvSize))
}
if sendSize > 0 {
opts = append(opts, grpc.MaxCallSendMsgSize(sendSize))
}
return grpc.WithDefaultCallOptions(opts...)
}
// NewGRPCDispatcher wraps all the actions needed for this CP.
func (gw *Gateway) NewGRPCDispatcher() (coprocess.Dispatcher, error) {
if gw.GetConfig().CoProcessOptions.CoProcessGRPCServer == "" {
return nil, errors.New("No gRPC URL is set")
}
authority := gw.GetConfig().CoProcessOptions.GRPCAuthority
var err error
grpcConnection, err = grpc.Dial("",
gw.grpcCallOpts(),
grpc.WithInsecure(),
grpc.WithAuthority(authority),
grpc.WithDialer(gw.dialer),
)
grpcClient = coprocess.NewDispatcherClient(grpcConnection)
if err != nil {
log.WithFields(logrus.Fields{
"prefix": "coprocess",
}).Error(err)
return nil, err
}
return &GRPCDispatcher{}, nil
}