github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/orderer/kafka/consumer.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8                   http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package kafka
    18  
    19  import (
    20  	"github.com/Shopify/sarama"
    21  	"github.com/hyperledger/fabric/orderer/localconfig"
    22  )
    23  
    24  // Consumer allows the caller to receive a stream of blobs
    25  // from the Kafka cluster for a specific partition.
    26  type Consumer interface {
    27  	Recv() <-chan *sarama.ConsumerMessage
    28  	Errors() <-chan *sarama.ConsumerError
    29  	Closeable
    30  }
    31  
    32  type consumerImpl struct {
    33  	parent    sarama.Consumer
    34  	partition sarama.PartitionConsumer
    35  }
    36  
    37  func newConsumer(brokers []string, kafkaVersion sarama.KafkaVersion, tls config.TLS, cp ChainPartition, offset int64) (Consumer, error) {
    38  	parent, err := sarama.NewConsumer(brokers, newBrokerConfig(kafkaVersion, rawPartition, tls))
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  	partition, err := parent.ConsumePartition(cp.Topic(), cp.Partition(), offset)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  	c := &consumerImpl{
    47  		parent:    parent,
    48  		partition: partition,
    49  	}
    50  	logger.Debugf("Created new consumer for session (partition %s, beginning offset %d)", cp, offset)
    51  	return c, nil
    52  }
    53  
    54  // Recv returns a channel with blobs received
    55  // from the Kafka cluster for a partition.
    56  func (c *consumerImpl) Recv() <-chan *sarama.ConsumerMessage {
    57  	return c.partition.Messages()
    58  }
    59  
    60  // Errors returns a channel with errors occuring during
    61  // the consumption of a partition from the Kafka cluster.
    62  func (c *consumerImpl) Errors() <-chan *sarama.ConsumerError {
    63  	return c.partition.Errors()
    64  }
    65  
    66  // Close shuts down the partition consumer.
    67  // Invoked by the session deliverer's Close method, which is itself called
    68  // during the processSeek function, between disabling and enabling the push.
    69  func (c *consumerImpl) Close() error {
    70  	if err := c.partition.Close(); err != nil {
    71  		return err
    72  	}
    73  	return c.parent.Close()
    74  }