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


   @BewareMyPower:
   > However, for the code here, how could you ensure that when `changeState` 
is called, the lock has already been acquired in `waitUntilReady`?
   
   It doesn't matter whether `changeState()` is called first, or 
`waitUntilReady()` is called first. Using the lock prevents the race condition 
that can occur in the gap between the test in `for c.getState() != 
connectionReady` and the call to `c.cond.Wait()` occurring.
   
   If `changeState` gets called before `waitUntilReady`, then the broadcast 
happens first and all is well.
   
   if `waitUntilReady` gets called before `changeState`, then `waitUntilReady` 
will block on the `cond.Wait()`, whilst still holding the lock. However, 
`cond.Wait()` call has a clever internal release of the lock which I think 
should still allow the `changeState` to happen:
   
   ```go
   func (c *Cond) Wait() {
        c.checker.check()
        t := runtime_notifyListAdd(&c.notify)
        c.L.Unlock()
        runtime_notifyListWait(&c.notify, t)
        c.L.Lock()
   }
   ```
   
   (note that the `c.L` here is what we are calling `c.cond.L`).


-- 
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