-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathasync.go
187 lines (171 loc) · 5.25 KB
/
async.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package async
import (
"context"
"errors"
"fmt"
"os"
"strings"
dapr "github.com/dapr/go-sdk/service/common"
daprd "github.com/dapr/go-sdk/service/grpc"
"k8s.io/klog/v2"
ofctx "github.com/OpenFunction/functions-framework-go/context"
"github.com/OpenFunction/functions-framework-go/internal/functions"
"github.com/OpenFunction/functions-framework-go/plugin"
"github.com/OpenFunction/functions-framework-go/runtime"
)
const (
defaultPattern = "/"
)
type Runtime struct {
port string
pattern string
handler dapr.Service
grpcHander *FakeServer
}
func NewAsyncRuntime(port string, pattern string) (*Runtime, error) {
if pattern == "" {
pattern = defaultPattern
}
if testMode := os.Getenv(ofctx.TestModeEnvName); testMode == ofctx.TestModeOn {
handler, grpcHandler, err := NewFakeService(fmt.Sprintf(":%s", port))
if err != nil {
klog.Errorf("failed to create dapr grpc service: %v\n", err)
return nil, err
}
return &Runtime{
port: port,
pattern: pattern,
handler: handler,
grpcHander: grpcHandler,
}, nil
}
handler, err := daprd.NewService(fmt.Sprintf(":%s", port))
if err != nil {
klog.Errorf("failed to create dapr grpc service: %v\n", err)
return nil, err
}
return &Runtime{
port: port,
pattern: pattern,
handler: handler,
grpcHander: nil,
}, nil
}
func (r *Runtime) Start(ctx context.Context) error {
klog.Infof("Async Function serving grpc: listening on port %s", r.port)
klog.Fatal(r.handler.Start())
return nil
}
func (r *Runtime) RegisterHTTPFunction(
ctx ofctx.RuntimeContext,
prePlugins []plugin.Plugin,
postPlugins []plugin.Plugin,
rf *functions.RegisteredFunction,
) error {
return errors.New("async runtime cannot register http function")
}
func (r *Runtime) RegisterCloudEventFunction(
ctx context.Context,
funcContext ofctx.RuntimeContext,
prePlugins []plugin.Plugin,
postPlugins []plugin.Plugin,
rf *functions.RegisteredFunction,
) error {
return errors.New("async runtime cannot register cloudevent function")
}
func (r *Runtime) RegisterOpenFunction(
ctx ofctx.RuntimeContext,
prePlugins []plugin.Plugin,
postPlugins []plugin.Plugin,
rf *functions.RegisteredFunction,
) error {
// Register the asynchronous functions (based on the Dapr runtime)
return func(f func(ofctx.Context, []byte) (ofctx.Out, error)) error {
var funcErr error
// Initialize dapr client if it is nil
ctx.InitDaprClientIfNil()
// Serving function with inputs
if ctx.HasInputs() {
for name, input := range ctx.GetInputs() {
n := name
switch input.GetType() {
case ofctx.OpenFuncBinding:
input.Uri = input.ComponentName
funcErr = r.handler.AddBindingInvocationHandler(input.Uri, func(c context.Context, in *dapr.BindingEvent) (out []byte, err error) {
rm := runtime.NewRuntimeManager(ctx, prePlugins, postPlugins)
rm.FuncContext.SetEvent(n, in)
rm.FunctionRunWrapperWithHooks(rf.GetOpenFunctionFunction())
switch rm.FuncOut.GetCode() {
case ofctx.Success:
return rm.FuncOut.GetData(), nil
case ofctx.InternalError:
return nil, rm.FuncContext.GetError()
default:
return nil, nil
}
})
if funcErr == nil {
klog.Infof("registered bindings handler: %s", input.Uri)
}
case ofctx.OpenFuncTopic:
sub := &dapr.Subscription{
PubsubName: input.ComponentName,
Topic: input.Uri,
}
if input.PubSubRoutingRule != nil && input.PubSubRoutingRule.Match != "" {
sub.Route = input.PubSubRoutingRule.Route
sub.Match = input.PubSubRoutingRule.Match
sub.Priority = input.PubSubRoutingRule.Priority
}
funcErr = r.handler.AddTopicEventHandler(sub, func(c context.Context, e *dapr.TopicEvent) (retry bool, err error) {
rm := runtime.NewRuntimeManager(ctx, prePlugins, postPlugins)
rm.FuncContext.SetEvent(name, e)
rm.FunctionRunWrapperWithHooks(rf.GetOpenFunctionFunction())
switch rm.FuncOut.GetCode() {
case ofctx.Success:
return false, nil
case ofctx.InternalError:
err = rm.FuncContext.GetError()
if retry, ok := rm.FuncOut.GetMetadata()["retry"]; ok {
if strings.EqualFold(retry, "true") {
return true, err
} else if strings.EqualFold(retry, "false") {
return false, err
} else {
return false, err
}
}
return false, err
default:
return false, nil
}
})
if funcErr == nil {
klog.Infof("registered pubsub handler: %s, topic: %s", input.ComponentName, input.Uri)
}
default:
return fmt.Errorf("invalid input type: %s", input.GetType())
}
if funcErr != nil {
// When the function throws an exception,
// first call client.Close() to close the dapr client,
// then set fwk.funcContext.daprClient to nil
ctx.DestroyDaprClient()
klog.Errorf("failed to add dapr service handler: %v\n", funcErr)
return funcErr
}
}
// If a function has no input, just return it.
return nil
}
err := errors.New("no inputs defined for the function")
klog.Errorf("failed to register function: %v\n", err)
return err
}(rf.GetOpenFunctionFunction())
}
func (r *Runtime) Name() ofctx.Runtime {
return ofctx.Async
}
func (r *Runtime) GetHandler() interface{} {
return r.grpcHander
}