Skip to content

Commit

Permalink
Merge pull request #99 from mreiferson/close_race_99
Browse files Browse the repository at this point in the history
panic: runtime error: close of closed channel
  • Loading branch information
jehiah committed Jan 6, 2015
2 parents 103a1c5 + 71d3b1d commit 28c8ae1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
9 changes: 6 additions & 3 deletions consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ type Consumer struct {
stopFlag int32
connectedFlag int32
stopHandler sync.Once
exitHandler sync.Once

// read from this channel to block until consumer is cleanly stopped
StopChan chan int
Expand Down Expand Up @@ -1079,9 +1080,11 @@ func (r *Consumer) shouldFailMessage(message *Message, handler interface{}) bool
}

func (r *Consumer) exit() {
close(r.exitChan)
r.wg.Wait()
close(r.StopChan)
r.exitHandler.Do(func() {
close(r.exitChan)
r.wg.Wait()
close(r.StopChan)
})
}

func (r *Consumer) log(lvl LogLevel, line string, args ...interface{}) {
Expand Down
9 changes: 6 additions & 3 deletions producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ func NewProducer(addr string, config *Config) (*Producer, error) {
exitChan: make(chan int),
responseChan: make(chan []byte),
errorChan: make(chan []byte),
closeChan: make(chan int),
}
return p, nil
}
Expand Down Expand Up @@ -223,7 +222,7 @@ func (w *Producer) connect() error {
atomic.StoreInt32(&w.state, StateInit)
return err
}

w.closeChan = make(chan int)
w.wg.Add(1)
go w.router()

Expand Down Expand Up @@ -323,4 +322,8 @@ func (w *Producer) onConnResponse(c *Conn, data []byte) { w.responseChan <- data
func (w *Producer) onConnError(c *Conn, data []byte) { w.errorChan <- data }
func (w *Producer) onConnHeartbeat(c *Conn) {}
func (w *Producer) onConnIOError(c *Conn, err error) { w.close() }
func (w *Producer) onConnClose(c *Conn) { w.closeChan <- 1 }
func (w *Producer) onConnClose(c *Conn) {
w.guard.Lock()
defer w.guard.Unlock()
close(w.closeChan)
}

0 comments on commit 28c8ae1

Please sign in to comment.