merlimat commented on a change in pull request #86: Simplify and refactor parts 
of  the single topic consumer.
URL: https://github.com/apache/pulsar-client-go/pull/86#discussion_r344845948
 
 

 ##########
 File path: pulsar/consumer_impl.go
 ##########
 @@ -0,0 +1,257 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package pulsar
+
+import (
+       "context"
+       "errors"
+       "fmt"
+       "sync"
+
+       log "github.com/sirupsen/logrus"
+
+       "github.com/apache/pulsar-client-go/pkg/pb"
+       "github.com/apache/pulsar-client-go/pulsar/internal"
+)
+
+var ErrConsumerClosed = errors.New("consumer closed")
+
+type consumer struct {
+       topic string
+
+       options ConsumerOptions
+
+       consumers []*partitionConsumer
+
+       // channel used to deliver message to clients
+       messageCh chan ConsumerMessage
+
+       closeCh chan struct{}
+       errorCh chan error
+
+       log *log.Entry
+}
+
+func newConsumer(client *client, options ConsumerOptions) (Consumer, error) {
+       if options.Topic == "" && options.Topics == nil && 
options.TopicsPattern == "" {
+               return nil, newError(TopicNotFound, "topic is required")
+       }
+
+       if options.SubscriptionName == "" {
+               return nil, newError(SubscriptionNotFound, "subscription name 
is required for consumer")
+       }
+
+       if options.ReceiverQueueSize == 0 {
+               options.ReceiverQueueSize = 1000
+       }
+
+       // did the user pass in a message channel?
+       messageCh := options.MessageChannel
+       if options.MessageChannel == nil {
+               messageCh = make(chan ConsumerMessage, 10)
+       }
+
+       // single topic consumer
+       if options.Topic != "" || len(options.Topics) == 1 {
+               topic := options.Topic
+               if topic == "" {
+                       topic = options.Topics[0]
+               }
+
+               if _, err := internal.ParseTopicName(topic); err != nil {
+                       return nil, err
+               }
+
+               return topicSubscribe(client, options, topic, messageCh)
+       }
+
+       return nil, newError(ResultInvalidTopicName, "topic name is required 
for consumer")
+}
+
+func topicSubscribe(client *client, options ConsumerOptions, topic string,
+       messageCh chan ConsumerMessage) (Consumer, error) {
+       consumer := &consumer{
+               topic:     topic,
+               messageCh: messageCh,
+               errorCh:   make(chan error),
+               log:       log.WithField("topic", topic),
+       }
+
+       partitions, err := client.TopicPartitions(topic)
+       if err != nil {
+               return nil, err
+       }
+
+       numPartitions := len(partitions)
+       consumer.consumers = make([]*partitionConsumer, numPartitions)
+
+       type ConsumerError struct {
+               err       error
+               partition int
+               consumer  *partitionConsumer
+       }
+
+       receiverQueueSize := options.ReceiverQueueSize
+       var wg sync.WaitGroup
+       ch := make(chan ConsumerError, numPartitions)
+       for partitionIdx, partitionTopic := range partitions {
+               wg.Add(1)
+               go func(idx int, pt string) {
+                       defer wg.Done()
+                       opts := &partitionConsumerOpts{
+                               topic:               pt,
+                               subscription:        options.SubscriptionName,
+                               subscriptionType:    options.Type,
+                               subscriptionInitPos: 
options.SubscriptionInitPos,
+                               partitionIdx:        idx,
+                               receiverQueueSize:   receiverQueueSize,
+                       }
+                       cons, err := newPartitionConsumer(consumer, client, 
opts, messageCh)
+                       ch <- ConsumerError{
+                               err:       err,
+                               partition: idx,
+                               consumer:  cons,
+                       }
+               }(partitionIdx, partitionTopic)
+       }
+
+       go func() {
+               wg.Wait()
+               close(ch)
+       }()
+
+       for ce := range ch {
+               if ce.err != nil {
+                       err = ce.err
+               } else {
+                       consumer.consumers[ce.partition] = ce.consumer
+               }
+       }
+
+       if err != nil {
+               // Since there were some failures, cleanup all the partitions 
that succeeded in creating the consumer
+               for _, c := range consumer.consumers {
+                       if c != nil {
+                               _ = c.Close()
+                       }
+               }
+               return nil, err
+       }
+
+       return consumer, nil
+}
+
+func (c *consumer) Topic() string {
+       return c.topic
+}
+
+func (c *consumer) Subscription() string {
+       return c.options.SubscriptionName
+}
+
+func (c *consumer) Unsubscribe() error {
+       var errMsg string
+       for _, consumer := range c.consumers {
+               if err := consumer.Unsubscribe(); err != nil {
+                       errMsg += fmt.Sprintf("topic %s, subscription %s: %s", 
c.Topic(), c.Subscription(), err)
+               }
+       }
+       if errMsg != "" {
+               return fmt.Errorf(errMsg)
+       }
+       return nil
+}
+
+func (c *consumer) Receive(ctx context.Context) (message Message, err error) {
+       //fmt.Println("recv chan", c.messageCh)
 
 Review comment:
   Remove commented line (or convert into log.debug)

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to