This commit is contained in:
crusader 2018-05-11 18:41:42 +09:00
parent fa0f700a9b
commit 3072c9dff3

View File

@ -37,11 +37,6 @@ func (c *Consumer) Start() error {
return fmt.Errorf("%s ConsumerCtx is nil", c.logHeader()) return fmt.Errorf("%s ConsumerCtx is nil", c.logHeader())
} }
reader := oek.Reader(c.ConsumerHandler.GetConsumerName())
if nil == reader {
return fmt.Errorf("%s Reader[%s] is not valid", c.logHeader(), c.ConsumerHandler.GetConsumerName())
}
if err := c.ConsumerHandler.Init(c.ctx); nil != err { if err := c.ConsumerHandler.Init(c.ctx); nil != err {
return fmt.Errorf("%s Init error %v", c.logHeader(), err) return fmt.Errorf("%s Init error %v", c.logHeader(), err)
} }
@ -49,7 +44,7 @@ func (c *Consumer) Start() error {
c.stopChan = make(chan struct{}) c.stopChan = make(chan struct{})
c.stopWg.Add(1) c.stopWg.Add(1)
go c.handleConsumer(reader) go c.handleConsumer()
return nil return nil
} }
@ -72,7 +67,7 @@ func (c *Consumer) logHeader() string {
return fmt.Sprintf("Consumer[%s]:", c.ConsumerHandler.GetName()) return fmt.Sprintf("Consumer[%s]:", c.ConsumerHandler.GetName())
} }
func (c *Consumer) handleConsumer(reader *kafka.Reader) { func (c *Consumer) handleConsumer() {
defer func() { defer func() {
c.ConsumerHandler.OnStop(c.ctx) c.ConsumerHandler.OnStop(c.ctx)
logging.Logger().Infof("%s Stopped", c.logHeader()) logging.Logger().Infof("%s Stopped", c.logHeader())
@ -87,7 +82,7 @@ func (c *Consumer) handleConsumer(reader *kafka.Reader) {
stopChan := make(chan struct{}) stopChan := make(chan struct{})
receiveDoneChan := make(chan error) receiveDoneChan := make(chan error)
go c.handleReceive(stopChan, receiveDoneChan, reader) go c.handleReceive(stopChan, receiveDoneChan)
logging.Logger().Infof("%s Started", c.logHeader()) logging.Logger().Infof("%s Started", c.logHeader())
@ -100,7 +95,7 @@ func (c *Consumer) handleConsumer(reader *kafka.Reader) {
} }
} }
func (c *Consumer) handleReceive(stopChan <-chan struct{}, doneChan chan<- error, reader *kafka.Reader) { func (c *Consumer) handleReceive(stopChan <-chan struct{}, doneChan chan<- error) {
var ( var (
m kafka.Message m kafka.Message
stopping atomic.Value stopping atomic.Value
@ -111,6 +106,12 @@ func (c *Consumer) handleReceive(stopChan <-chan struct{}, doneChan chan<- error
doneChan <- err doneChan <- err
}() }()
reader := oek.Reader(c.ConsumerHandler.GetConsumerName())
if nil == reader {
err = fmt.Errorf("%s Reader[%s] is not valid", c.logHeader(), c.ConsumerHandler.GetConsumerName())
return
}
for { for {
readChan := make(chan struct{}) readChan := make(chan struct{})
go func() { go func() {