Skip to content

add route to subscription when using http service & bugfix #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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