go-sdk icon indicating copy to clipboard operation
go-sdk copied to clipboard

NewClient() bug if first call fails

Open telemac opened this issue 3 years ago • 6 comments

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.

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

	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.

telemac avatar Apr 15 '22 16:04 telemac

I am also having the same exact issue. Just can't get it to work after spending days debugging it. Both client and server are on v1.7.0

@telemac were you able to make any progress?

munjalpatel avatar Apr 20 '22 13:04 munjalpatel

Hi @munjalpatel, a simple sleep for 5 seconds before calling dapr.NewClient solves the issue for now.

A dapr.NewClient taking a timeout or may be a context (the most idiomatic way) would be great too.

telemac avatar Apr 20 '22 15:04 telemac

@telemac Well, for me, the client is nil even after sleep. Both err and client are nil.

munjalpatel avatar Apr 20 '22 16:04 munjalpatel

Still having the same issue.

kayvonkhosrowpour avatar Aug 26 '22 19:08 kayvonkhosrowpour

retry to create client,can use dapr.NewClientWithPort

wXwcoder avatar Nov 10 '22 06:11 wXwcoder

Just an observation: I would expect a service to os.Exit(1) or panic(...) if the client cannot be created. In most container-based environments, this would trigger a respawn of the container, and the next creation of the client (the first in a new pod instance) would then work. Not sure if I would consider this a bug.

spolab avatar Jan 03 '23 20:01 spolab