forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coprocess_grpc.go
115 lines (89 loc) · 2.98 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
112
113
114
115
// +build coprocess
// +build grpc
package main
import (
"net"
"net/url"
"time"
"errors"
"github.com/TykTechnologies/logrus"
"github.com/TykTechnologies/tyk/coprocess"
"github.com/TykTechnologies/tykcommon"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
// CoProcessName specifies the driver name.
const CoProcessName string = "grpc"
// MessageType sets the default message type.
var MessageType = coprocess.ProtobufMessage
var grpcConnection *grpc.ClientConn
var grpcClient coprocess.DispatcherClient
// GRPCDispatcher implements a coprocess.Dispatcher
type GRPCDispatcher struct {
coprocess.Dispatcher
}
func dialer(addr string, timeout time.Duration) (conn net.Conn, err error) {
var grpcUrl *url.URL
grpcUrl, err = url.Parse(config.CoProcessOptions.CoProcessGRPCServer)
if err != nil {
log.WithFields(logrus.Fields{
"prefix": "coprocess-grpc",
}).Error(err)
return nil, err
}
if grpcUrl == nil || config.CoProcessOptions.CoProcessGRPCServer == "" {
var errString = "No gRPC URL is set!"
log.WithFields(logrus.Fields{
"prefix": "coprocess-grpc",
}).Error(errString)
return nil, errors.New(errString)
}
grpcUrlString := config.CoProcessOptions.CoProcessGRPCServer[len(grpcUrl.Scheme)+3 : len(config.CoProcessOptions.CoProcessGRPCServer)]
return net.DialTimeout(grpcUrl.Scheme, grpcUrlString, timeout)
}
// Dispatch takes a CoProcessMessage and sends it to the CP.
func (d *GRPCDispatcher) DispatchObject(object *coprocess.Object) (*coprocess.Object, error) {
newObject, err := grpcClient.Dispatch(context.Background(), object)
if err != nil {
log.WithFields(logrus.Fields{
"prefix": "coprocess-grpc",
}).Error(err)
}
return newObject, err
}
// DispatchEvent dispatches a Tyk event.
func (d *GRPCDispatcher) DispatchEvent(eventJSON []byte) {
eventObject := &coprocess.Event{string(eventJSON)}
_, err := grpcClient.DispatchEvent(context.Background(), eventObject)
if err != nil {
log.WithFields(logrus.Fields{
"prefix": "coprocess-grpc",
}).Error(err)
}
return
}
// Reload triggers a reload affecting CP middlewares and event handlers.
func (d *GRPCDispatcher) Reload() {
return
}
// HandleMiddlewareCache isn't used by gRPC.
func (d* GRPCDispatcher) HandleMiddlewareCache(b *tykcommon.BundleManifest, basePath string) {
return
}
// NewCoProcessDispatcher wraps all the actions needed for this CP.
func NewCoProcessDispatcher() (dispatcher coprocess.Dispatcher, err error) {
dispatcher, err = &GRPCDispatcher{}, nil
grpcConnection, err = grpc.Dial("", grpc.WithInsecure(), grpc.WithDialer(dialer))
grpcClient = coprocess.NewDispatcherClient(grpcConnection)
if err != nil {
log.WithFields(logrus.Fields{
"prefix": "coprocess-grpc",
}).Error(err)
}
return dispatcher, err
}
// Dispatch prepares a CoProcessMessage, sends it to the GlobalDispatcher and gets a reply.
func (c *CoProcessor) Dispatch(object *coprocess.Object) (newObject *coprocess.Object, err error) {
newObject, err = GlobalDispatcher.DispatchObject(object)
return newObject, err
}