Skip to content

Commit

Permalink
Merge pull request #66 from wrongerror/main
Browse files Browse the repository at this point in the history
add route to subscription when using http service & bugfix
  • Loading branch information
benjaminhuo authored Sep 26, 2022
2 parents 28cbda8 + 86f7bcc commit 617c147
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
2 changes: 2 additions & 0 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,8 @@ func (ctx *FunctionContext) GetNativeContext() context.Context {
}

func (ctx *FunctionContext) SetNativeContext(c context.Context) {
ctx.mu.Lock()
defer ctx.mu.Unlock()
ctx.Ctx = c
}

Expand Down
29 changes: 17 additions & 12 deletions runtime/async/async.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
daprruntime "github.com/dapr/dapr/pkg/runtime"
dapr "github.com/dapr/go-sdk/service/common"
grpcsvc "github.com/dapr/go-sdk/service/grpc"
httpsvc "github.com/dapr/go-sdk/service/grpc"
httpsvc "github.com/dapr/go-sdk/service/http"
"k8s.io/klog/v2"

ofctx "github.com/OpenFunction/functions-framework-go/context"
Expand All @@ -25,6 +25,7 @@ const (
)

type Runtime struct {
protocol string
port string
pattern string
handler dapr.Service
Expand All @@ -49,24 +50,23 @@ func NewAsyncRuntime(port string, pattern string) (*Runtime, error) {
}, nil
}

var newService func(address string) (s dapr.Service, err error)
var handler dapr.Service
protocol := daprruntime.Protocol(os.Getenv(protocolEnvVar))
switch protocol {
case daprruntime.HTTPProtocol:
newService = httpsvc.NewService
case daprruntime.GRPCProtocol:
newService = grpcsvc.NewService
handler = httpsvc.NewService(fmt.Sprintf(":%s", port))
default:
protocol = daprruntime.GRPCProtocol
newService = grpcsvc.NewService
}
handler, err := newService(fmt.Sprintf(":%s", port))
if err != nil {
klog.Errorf("failed to create dapr %v service: %v\n", protocol, err)
return nil, err
service, err := grpcsvc.NewService(fmt.Sprintf(":%s", port))
if err != nil {
klog.Errorf("failed to create dapr grpc service: %v\n", err)
return nil, err
}
handler = service
}

return &Runtime{
protocol: string(protocol),
port: port,
pattern: pattern,
handler: handler,
Expand All @@ -75,7 +75,7 @@ func NewAsyncRuntime(port string, pattern string) (*Runtime, error) {
}

func (r *Runtime) Start(ctx context.Context) error {
klog.Infof("Async Function serving grpc: listening on port %s", r.port)
klog.Infof("Async Function serving %s: listening on port %s", r.protocol, r.port)
klog.Fatal(r.handler.Start())
return nil
}
Expand Down Expand Up @@ -121,6 +121,7 @@ func (r *Runtime) RegisterOpenFunction(
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.SetNativeContext(c)
rm.FuncContext.SetEvent(n, in)
rm.FunctionRunWrapperWithHooks(rf.GetOpenFunctionFunction())

Expand All @@ -141,8 +142,12 @@ func (r *Runtime) RegisterOpenFunction(
PubsubName: input.ComponentName,
Topic: input.Uri,
}
if r.protocol == string(daprruntime.HTTPProtocol) {
sub.Route = fmt.Sprintf("/%s", input.Uri)
}
funcErr = r.handler.AddTopicEventHandler(sub, func(c context.Context, e *dapr.TopicEvent) (retry bool, err error) {
rm := runtime.NewRuntimeManager(ctx, prePlugins, postPlugins)
rm.FuncContext.SetNativeContext(c)
rm.FuncContext.SetEvent(n, e)
rm.FunctionRunWrapperWithHooks(rf.GetOpenFunctionFunction())

Expand Down

0 comments on commit 617c147

Please sign in to comment.