@@ -6,15 +6,17 @@ import (
66 "strconv"
77 "time"
88
9- proxyruntime "github.com/OpenFunction/dapr-proxy/pkg/runtime"
10- "github.com/OpenFunction/dapr-proxy/pkg/utils"
119 ofctx "github.com/OpenFunction/functions-framework-go/context"
1210 "github.com/OpenFunction/functions-framework-go/framework"
11+ "github.com/cenkalti/backoff/v4"
1312 diag "github.com/dapr/dapr/pkg/diagnostics"
1413 "github.com/dapr/dapr/pkg/modes"
1514 "github.com/dapr/dapr/pkg/runtime"
1615 "github.com/pkg/errors"
1716 "k8s.io/klog/v2"
17+
18+ proxyruntime "github.com/OpenFunction/dapr-proxy/pkg/runtime"
19+ "github.com/OpenFunction/dapr-proxy/pkg/utils"
1820)
1921
2022const (
@@ -48,17 +50,20 @@ func main() {
4850 port , _ := strconv .Atoi (funcContext .GetPort ())
4951 protocol := utils .GetEnvVar (protocolEnvVar , defaultAppProtocol )
5052 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 ,
5558 }
5659
5760 FuncRuntime = proxyruntime .NewFuncRuntime (config , funcContext )
5861 if err := FuncRuntime .CreateFuncChannel (); err != nil {
5962 klog .Exit (err )
6063 }
6164
65+ go FuncRuntime .ProcessEvents ()
66+
6267 if err := fwk .Register (ctx , EventHandler ); err != nil {
6368 klog .Exit (err )
6469 }
@@ -75,35 +80,37 @@ func EventHandler(ctx ofctx.Context, in []byte) (ofctx.Out, error) {
7580 klog .V (4 ).Infof ("Input: %s - Event Forwarding Elapsed: %vms" , ctx .GetInputName (), elapsed )
7681 }()
7782
78- // Forwarding BindingEvent
83+ c := ctx .GetNativeContext ()
84+
85+ // Handle BindingEvent
7986 bindingEvent := ctx .GetBindingEvent ()
8087 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 )
9189 }
9290
93- // Forwarding TopicEvent
91+ // Handle TopicEvent
9492 topicEvent := ctx .GetTopicEvent ()
9593 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 )
10595 }
10696
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+ }
109116}
0 commit comments