Skip to content

Commit

Permalink
fix: add waitGroup to start go func
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Jan 5, 2024
1 parent bb0f08b commit d02f2e6
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,15 @@ type client struct {
lastID int64
backoffFactory func() backoff.BackOff
cancelFunc context.CancelFunc
wg sync.WaitGroup
}

func (c *client) Start() {
c.setState(ClientConnecting)
boff := c.backoffFactory()
c.wg.Add(1)
go func() {
defer c.wg.Done()
for {
c.setErr(nil)
// Listen for state change to ClientConnected and signal backoff Reset then.
Expand Down Expand Up @@ -221,9 +224,8 @@ func (c *client) Start() {
func (c *client) Stop() {
if c.cancelFunc != nil {
c.cancelFunc()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) // in practice, it is faster than 5 seconds so this is just to avoid infinite block
defer cancel()
c.WaitForState(ctx, ClientClosed)
c.wg.Wait()
c.setState(ClientClosed)
}
}

Expand Down

0 comments on commit d02f2e6

Please sign in to comment.