bschofield edited a comment on pull request #663:
URL: https://github.com/apache/pulsar-client-go/pull/663#issuecomment-963954888


   @BewareMyPower:
   > Therefore, I think a better solution is [...]
   
   I think that fix also looks fine (and maybe better), however you should 
avoid calling `defer` inside the for loop because a new defer call will be 
pushed onto the stack for each loop invocation. So it should be:
   
   ```go
   func (c *connection) waitUntilReady() error {
        for {
                switch state := c.getState(); state {
                case connectionReady:
                        return nil
                case connectionClosed:
                        return errors.New("connection error")
                default:
                        c.log.Debugf("Wait until connection is ready state=%s", 
state.String())
                        // Here we use c.cond.L.Lock() because we are concerned 
about race conditions between the switch statement above, and the cond.Wait() 
below
                        c.cond.L.Lock()
                        c.cond.Wait()
                        c.cond.L.Unlock()
                }
        }
   }
   ```
   
   I also edited your comment to make it clear why this lock is being used. I 
definitely agree that a comment is required, this is a really complicated 
situation and not made clearer by the slightly strange way that `sync.Cond` 
works.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to