dapr / dapr/go-sdk

NewClient() bug if first call fails

Open
#280 6 comments 1 reaction 0 assignees View on GitHub
bug
Dominant language
Go
Stars
479
Forks
187
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**

I have a connection timeout problem, the default one second timeout is too short to allow the sidecar to be ready, so my first call to NewClient fails. Any subsequent call won't have a chance to make a good connection after that.

if the first call of dapr.NewClient fails, it returns the correct error, and daprClient is nil, which is ok.
```go
daprClient, err = dapr.NewClient()
```
if I call it a second time after some delay, I will get daprClient==nil and err==nil, which is false. as daprClient is nil, it should retry a new connection and give the correct error for the new connection.

**To Reproduce**
if dapr.NewClient fails each time, you won't get an error, and you won't get a chance to retry a new connection in each iteration
```go
var daprClient dapr.Client
var err error

for i := 0; i < 3; i++ {
time.Sleep(time.Second * 3)
daprClient, err = dapr.NewClient()
if err != nil {
logger.WithError(err).WithField("i", i).Warn("create dapr client loop")
} else {
break
}
}
if err != nil {
logger.WithError(err).Error("create dapr client")
return
}
```

**Expected behavior**

if NewClient fails, it should retry to connect on next call and return a correct error, and not use doOnce.Do inconditionally.

Contributor guide

Open the contributing guide

Research direction

Search the Go SDK for NewClient and its doOnce usage, then read the connection initialization path. Reproduce the failed-first-call sequence with the supplied retry loop and verify that each failed call returns the current connection error instead of nil; a later successful call should still return a client.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.