forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coprocess_grpc.go
109 lines (89 loc) · 3 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
// +build coprocess
// +build grpc
package main
import (
"errors"
"net"
"net/url"
"time"
"github.com/Sirupsen/logrus"
"golang.org/x/net/context"
"google.golang.org/grpc"
"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/config"
"github.com/TykTechnologies/tyk/coprocess"
)
// CoProcessName specifies the driver name.
const CoProcessName = apidef.GrpcDriver
// 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) (net.Conn, error) {
grpcUrl, err := url.Parse(config.Global().CoProcessOptions.CoProcessGRPCServer)
if err != nil {
log.WithFields(logrus.Fields{
"prefix": "coprocess-grpc",
}).Error(err)
return nil, err
}
if grpcUrl == nil || config.Global().CoProcessOptions.CoProcessGRPCServer == "" {
errString := "No gRPC URL is set!"
log.WithFields(logrus.Fields{
"prefix": "coprocess-grpc",
}).Error(errString)
return nil, errors.New(errString)
}
grpcUrlString := config.Global().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) 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{
Payload: string(eventJSON),
}
_, err := grpcClient.DispatchEvent(context.Background(), eventObject)
if err != nil {
log.WithFields(logrus.Fields{
"prefix": "coprocess-grpc",
}).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) {}
// NewCoProcessDispatcher wraps all the actions needed for this CP.
func NewCoProcessDispatcher() (coprocess.Dispatcher, error) {
if config.Global().CoProcessOptions.CoProcessGRPCServer == "" {
return nil, errors.New("No gRPC URL is set")
}
var err error
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 nil, err
}
return &GRPCDispatcher{}, nil
}
// Dispatch prepares a CoProcessMessage, sends it to the GlobalDispatcher and gets a reply.
func (c *CoProcessor) Dispatch(object *coprocess.Object) (*coprocess.Object, error) {
return GlobalDispatcher.DispatchObject(object)
}