-
Notifications
You must be signed in to change notification settings - Fork 7
/
remote_extension.go
135 lines (111 loc) · 3.28 KB
/
remote_extension.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
package client
import (
"context"
"github.com/apibrew/apibrew/pkg/ext"
"github.com/apibrew/apibrew/pkg/model"
"github.com/apibrew/apibrew/pkg/resource_model"
"github.com/apibrew/apibrew/pkg/resources"
"github.com/apibrew/apibrew/pkg/service"
"github.com/apibrew/apibrew/pkg/service/annotations"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/structpb"
"net"
)
type remoteExtension struct {
serviceKey string
host string
remoteHost string
client *client
ext.FunctionServer
functions map[string]ExternalFunction
registeredExtensions []*resource_model.Extension
}
func (e *remoteExtension) PrepareCall(ext *resource_model.Extension) resource_model.ExternalCall {
return resource_model.ExternalCall{
FunctionCall: &resource_model.FunctionCall{
Host: e.remoteHost,
FunctionName: ext.Name,
},
}
}
func (e *remoteExtension) getServiceKey() string {
return e.serviceKey
}
func (e *remoteExtension) RegisterExtension(newExtension *resource_model.Extension) {
e.registeredExtensions = append(e.registeredExtensions, newExtension)
}
func (e *remoteExtension) RegisterFunction(name string, handler ExternalFunction) {
e.functions[name] = handler
}
// WithServiceKey
func (e *remoteExtension) WithServiceKey(serviceKey string) Extension {
e.serviceKey = serviceKey
return e
}
func (e *remoteExtension) FunctionCall(ctx context.Context, req *ext.FunctionCallRequest) (*ext.FunctionCallResponse, error) {
if e.functions[req.Name] == nil {
return nil, status.Error(codes.NotFound, "External function not found: "+req.Name)
}
result, err := e.functions[req.Name](ctx, req.Event)
if err != nil {
return nil, err
}
return &ext.FunctionCallResponse{Event: result}, nil
}
func (e *remoteExtension) Run(ctx context.Context) error {
server := grpc.NewServer()
ext.RegisterFunctionServer(server, e)
l, err := net.Listen("tcp", e.host)
if err != nil {
return err
}
go func() {
<-ctx.Done()
server.Stop()
}()
if e.serviceKey != "" {
extensions, _, err := e.client.ListRecords(ctx, service.RecordListParams{
Namespace: resources.ExtensionResource.Namespace,
Resource: resources.ExtensionResource.Name,
})
if err != nil {
return err
}
for _, ex := range extensions {
exr := resource_model.ExtensionMapperInstance.FromRecord(ex)
if annotations.Get(exr, annotations.ServiceKey) == e.serviceKey {
// check if it is in registration list
var found = false
for _, re := range e.registeredExtensions {
if re.Id.String() == exr.Id.String() {
found = true
break
}
}
if !found {
log.Warn("Removing orphaned remoteExtension: ", exr.Id)
err = e.client.DeleteRecord(ctx, resources.ExtensionResource.Namespace, resources.ExtensionResource.Name, &model.Record{
Properties: map[string]*structpb.Value{
"id": structpb.NewStringValue(exr.Id.String()),
},
})
if err != nil {
return err
}
}
}
}
}
return server.Serve(l)
}
func (d *client) NewRemoteExtension(host string, remoteHost string) Extension {
return &remoteExtension{
client: d,
host: host,
remoteHost: remoteHost,
functions: make(map[string]ExternalFunction),
}
}