Skip to content

Commit 777cba1

Browse files
authored
Merge pull request #38 from tpiperatgod/dev
Keep retrying when dapr client initialization fails instead of just panic
2 parents a8e7f63 + 45765f5 commit 777cba1

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

context/context.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strconv"
1111
"strings"
1212
"sync"
13+
"time"
1314

1415
cloudevents "github.com/cloudevents/sdk-go/v2"
1516
"github.com/dapr/go-sdk/service/common"
@@ -418,13 +419,23 @@ func (ctx *FunctionContext) InitDaprClientIfNil() {
418419
}
419420

420421
if ctx.daprClient == nil {
422+
var err error
421423
ctx.mu.Lock()
422424
defer ctx.mu.Unlock()
423-
c, e := dapr.NewClientWithPort(clientGRPCPort)
424-
if e != nil {
425-
panic(e)
425+
426+
for attempts := 120; attempts > 0; attempts-- {
427+
c, e := dapr.NewClientWithPort(clientGRPCPort)
428+
if e == nil {
429+
ctx.daprClient = c
430+
break
431+
}
432+
err = e
433+
time.Sleep(500 * time.Millisecond)
434+
}
435+
436+
if ctx.daprClient == nil {
437+
klog.Errorf("failed to init dapr client: %v", err)
426438
}
427-
ctx.daprClient = c
428439
}
429440
}
430441

runtime/async/async.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ func (r *Runtime) RegisterOpenFunction(
107107
return nil, nil
108108
}
109109
})
110+
if funcErr == nil {
111+
klog.Infof("registered bindings handler: %s", input.Uri)
112+
}
110113
case ofctx.OpenFuncTopic:
111114
sub := &dapr.Subscription{
112115
PubsubName: input.ComponentName,
@@ -136,6 +139,9 @@ func (r *Runtime) RegisterOpenFunction(
136139
return false, nil
137140
}
138141
})
142+
if funcErr == nil {
143+
klog.Infof("registered pubsub handler: %s, topic: %s", input.ComponentName, input.Uri)
144+
}
139145
default:
140146
return fmt.Errorf("invalid input type: %s", input.GetType())
141147
}

0 commit comments

Comments
 (0)