|
|
|
@ -6,24 +6,24 @@ import ( |
|
|
|
|
"sync" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
func NewConsumer(nsqHost string) *consumer { |
|
|
|
|
return &consumer{ |
|
|
|
|
func NewConsumer(nsqHost string) *Consumer { |
|
|
|
|
return &Consumer{ |
|
|
|
|
consumers: make([]*nsq.Consumer, 0), |
|
|
|
|
host: nsqHost, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type consumer struct { |
|
|
|
|
type Consumer struct { |
|
|
|
|
consumers []*nsq.Consumer |
|
|
|
|
host string |
|
|
|
|
wg sync.WaitGroup |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (c *consumer) Name() string { |
|
|
|
|
func (c *Consumer) Name() string { |
|
|
|
|
return "consumer" |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (s *consumer) Start() error { |
|
|
|
|
func (s *Consumer) Start() error { |
|
|
|
|
for _, v := range s.consumers { |
|
|
|
|
if err := v.ConnectToNSQD(s.host); err != nil { |
|
|
|
|
return err |
|
|
|
@ -31,7 +31,7 @@ func (s *consumer) Start() error { |
|
|
|
|
} |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
func (s *consumer) Shutdown(ctx context.Context) error { |
|
|
|
|
func (s *Consumer) Shutdown(ctx context.Context) error { |
|
|
|
|
for _, v := range s.consumers { |
|
|
|
|
v.Stop() |
|
|
|
|
} |
|
|
|
@ -39,7 +39,7 @@ func (s *consumer) Shutdown(ctx context.Context) error { |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (s *consumer) Register(topic, channel string, fun func(msg *nsq.Message) error) error { |
|
|
|
|
func (s *Consumer) Register(topic, channel string, fun func(msg *nsq.Message) error) error { |
|
|
|
|
consumer, err := nsq.NewConsumer(topic, channel, nsq.NewConfig()) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|