Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panic via nsq.Producer #45

Merged
merged 1 commit into from
Jun 15, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Producer struct {
stopFlag int32
exitChan chan int
wg sync.WaitGroup
guard sync.Mutex
}

// ProducerTransaction is returned by the async publish methods
Expand Down Expand Up @@ -86,13 +87,17 @@ func (w *Producer) String() string {

// Stop initiates a graceful stop of the Producer (permanent)
//
// NOTE: receive on StopChan to block until this process completes
// NOTE: this blocks until completion
func (w *Producer) Stop() {
w.guard.Lock()
if !atomic.CompareAndSwapInt32(&w.stopFlag, 0, 1) {
w.guard.Unlock()
return
}
w.log(LogLevelInfo, "stopping")
close(w.exitChan)
w.close()
w.guard.Unlock()
w.wg.Wait()
}

Expand Down Expand Up @@ -181,6 +186,9 @@ func (w *Producer) sendCommandAsync(cmd *Command, doneChan chan *ProducerTransac
}

func (w *Producer) connect() error {
w.guard.Lock()
defer w.guard.Unlock()

if atomic.LoadInt32(&w.stopFlag) == 1 {
return ErrStopped
}
Expand Down