@@ -6,15 +6,17 @@ import (
6
6
"strconv"
7
7
"time"
8
8
9
- proxyruntime "github.com/OpenFunction/dapr-proxy/pkg/runtime"
10
- "github.com/OpenFunction/dapr-proxy/pkg/utils"
11
9
ofctx "github.com/OpenFunction/functions-framework-go/context"
12
10
"github.com/OpenFunction/functions-framework-go/framework"
11
+ "github.com/cenkalti/backoff/v4"
13
12
diag "github.com/dapr/dapr/pkg/diagnostics"
14
13
"github.com/dapr/dapr/pkg/modes"
15
14
"github.com/dapr/dapr/pkg/runtime"
16
15
"github.com/pkg/errors"
17
16
"k8s.io/klog/v2"
17
+
18
+ proxyruntime "github.com/OpenFunction/dapr-proxy/pkg/runtime"
19
+ "github.com/OpenFunction/dapr-proxy/pkg/utils"
18
20
)
19
21
20
22
const (
@@ -48,17 +50,20 @@ func main() {
48
50
port , _ := strconv .Atoi (funcContext .GetPort ())
49
51
protocol := utils .GetEnvVar (protocolEnvVar , defaultAppProtocol )
50
52
config := & proxyruntime.Config {
51
- Protocol : runtime .Protocol (protocol ),
52
- Host : host ,
53
- Port : port ,
54
- Mode : modes .KubernetesMode ,
53
+ Protocol : runtime .Protocol (protocol ),
54
+ Host : host ,
55
+ Port : port ,
56
+ Mode : modes .KubernetesMode ,
57
+ MaxBufferSize : 1000000 ,
55
58
}
56
59
57
60
FuncRuntime = proxyruntime .NewFuncRuntime (config , funcContext )
58
61
if err := FuncRuntime .CreateFuncChannel (); err != nil {
59
62
klog .Exit (err )
60
63
}
61
64
65
+ go FuncRuntime .ProcessEvents ()
66
+
62
67
if err := fwk .Register (ctx , EventHandler ); err != nil {
63
68
klog .Exit (err )
64
69
}
@@ -75,35 +80,37 @@ func EventHandler(ctx ofctx.Context, in []byte) (ofctx.Out, error) {
75
80
klog .V (4 ).Infof ("Input: %s - Event Forwarding Elapsed: %vms" , ctx .GetInputName (), elapsed )
76
81
}()
77
82
78
- // Forwarding BindingEvent
83
+ c := ctx .GetNativeContext ()
84
+
85
+ // Handle BindingEvent
79
86
bindingEvent := ctx .GetBindingEvent ()
80
87
if bindingEvent != nil {
81
- data , err := FuncRuntime .OnBindingEvent (ctx , bindingEvent )
82
- if err != nil {
83
- klog .Error (err )
84
- return ctx .ReturnOnInternalError (), err
85
- } else {
86
- out := new (ofctx.FunctionOut )
87
- out .WithData (data )
88
- out .WithCode (ofctx .Success )
89
- return out , nil
90
- }
88
+ FuncRuntime .EnqueueBindingEvent (& c , bindingEvent )
91
89
}
92
90
93
- // Forwarding TopicEvent
91
+ // Handle TopicEvent
94
92
topicEvent := ctx .GetTopicEvent ()
95
93
if topicEvent != nil {
96
- err := FuncRuntime .OnTopicEvent (ctx , topicEvent )
97
- if err != nil {
98
- klog .Error (err )
99
- return ctx .ReturnOnInternalError (), err
100
- } else {
101
- out := new (ofctx.FunctionOut )
102
- out .WithCode (ofctx .Success )
103
- return out , nil
104
- }
94
+ FuncRuntime .EnqueueTopicEvent (& c , topicEvent )
105
95
}
106
96
107
- err := errors .New ("Only Binding and Pubsub events are supported" )
108
- return ctx .ReturnOnInternalError (), err
97
+ var resp * proxyruntime.EventResponse
98
+ err := backoff .Retry (func () error {
99
+ resp = FuncRuntime .GetEventResponse (& c )
100
+ if resp == nil {
101
+ return errors .New ("Failed to get event response" )
102
+ }
103
+ return nil
104
+ }, utils .NewExponentialBackOff ())
105
+
106
+ if err != nil {
107
+ e := errors .New ("Processing event timeout" )
108
+ klog .Error (e )
109
+ return ctx .ReturnOnInternalError (), e
110
+ } else {
111
+ out := new (ofctx.FunctionOut )
112
+ out .WithData (resp .Data )
113
+ out .WithCode (ofctx .Success )
114
+ return out , nil
115
+ }
109
116
}
0 commit comments