Skip to content

Commit

Permalink
Merge pull request #133 from jeddenlea/master
Browse files Browse the repository at this point in the history
producer: fix connection race
  • Loading branch information
mreiferson committed Apr 8, 2015
2 parents a76e331 + 79c45bd commit a40c61c
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,11 @@ func (w *Producer) connect() error {
return ErrStopped
}

if !atomic.CompareAndSwapInt32(&w.state, StateInit, StateConnected) {
switch state := atomic.LoadInt32(&w.state); state {
case StateInit:
case StateConnected:
return nil
default:
return ErrNotConnected
}

Expand All @@ -258,9 +262,9 @@ func (w *Producer) connect() error {
if err != nil {
w.conn.Close()
w.log(LogLevelError, "(%s) error connecting to nsqd - %s", w.addr, err)
atomic.StoreInt32(&w.state, StateInit)
return err
}
atomic.StoreInt32(&w.state, StateConnected)
w.closeChan = make(chan int)
w.wg.Add(1)
go w.router()
Expand Down

0 comments on commit a40c61c

Please sign in to comment.