Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

add Offset() to PartitionConsumer interface #221

Merged
merged 3 commits into from Mar 16, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions partitions.go
Expand Up @@ -19,6 +19,10 @@ type PartitionConsumer interface {

// Partition returns the consumed partition
Partition() int32

// Offset returns the offset used for creating the PartitionConsumer instance.
// Offset can be a literal offset, or OffsetNewest, or OffsetOldest
Offset() int64
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please rename this method to InitialOffset?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I will do it.

}

type partitionConsumer struct {
Expand All @@ -29,6 +33,7 @@ type partitionConsumer struct {

topic string
partition int32
offset int64

closeOnce sync.Once
closeErr error
Expand All @@ -37,12 +42,14 @@ type partitionConsumer struct {
}

func newPartitionConsumer(manager sarama.Consumer, topic string, partition int32, info offsetInfo, defaultOffset int64) (*partitionConsumer, error) {
pcm, err := manager.ConsumePartition(topic, partition, info.NextOffset(defaultOffset))
offset := info.NextOffset(defaultOffset)
pcm, err := manager.ConsumePartition(topic, partition, offset)

// Resume from default offset, if requested offset is out-of-range
if err == sarama.ErrOffsetOutOfRange {
info.Offset = -1
pcm, err = manager.ConsumePartition(topic, partition, defaultOffset)
offset = defaultOffset
pcm, err = manager.ConsumePartition(topic, partition, offset)
}
if err != nil {
return nil, err
Expand All @@ -54,6 +61,7 @@ func newPartitionConsumer(manager sarama.Consumer, topic string, partition int32

topic: topic,
partition: partition,
offset: offset,

dying: make(chan none),
dead: make(chan none),
Expand All @@ -66,6 +74,9 @@ func (c *partitionConsumer) Topic() string { return c.topic }
// Partition implements PartitionConsumer
func (c *partitionConsumer) Partition() int32 { return c.partition }

// Offset implements PartitionConsumer
func (c *partitionConsumer) Offset() int64 { return c.offset }

// AsyncClose implements PartitionConsumer
func (c *partitionConsumer) AsyncClose() {
c.closeOnce.Do(func() {
Expand Down