// NewPartitionConsumer creates a new partition consumer instance func NewPartitionConsumer(group *ConsumerGroup, partition int32) (*PartitionConsumer, error) { config := sarama.ConsumerConfig{ DefaultFetchSize: group.config.DefaultFetchSize, EventBufferSize: group.config.EventBufferSize, MaxMessageSize: group.config.MaxMessageSize, MaxWaitTime: group.config.MaxWaitTime, MinFetchSize: group.config.MinFetchSize, OffsetMethod: sarama.OffsetMethodOldest, } offset, err := group.Offset(partition) if err != nil { return nil, err } else if offset > 0 { config.OffsetMethod = sarama.OffsetMethodManual config.OffsetValue = offset } stream, err := sarama.NewConsumer(group.client, group.topic, partition, group.name, &config) if err != nil { return nil, err } return &PartitionConsumer{ stream: stream, topic: group.topic, partition: partition, }, nil }
// Validate consumer config, maybe sarama can expose a public ConsumerConfig.Validate() one day func validateConsumerConfig(config *sarama.ConsumerConfig) error { if config.DefaultFetchSize < 0 { return sarama.ConfigurationError("Invalid DefaultFetchSize") } else if config.DefaultFetchSize == 0 { config.DefaultFetchSize = 1024 } if config.MinFetchSize < 0 { return sarama.ConfigurationError("Invalid MinFetchSize") } else if config.MinFetchSize == 0 { config.MinFetchSize = 1 } if config.MaxWaitTime <= 0 { return sarama.ConfigurationError("Invalid MaxWaitTime") } else if config.MaxWaitTime < 100 { sarama.Logger.Println("ConsumerConfig.MaxWaitTime is very low, which can cause high CPU and network usage. See sarama documentation for details.") } if config.MaxMessageSize < 0 { return sarama.ConfigurationError("Invalid MaxMessageSize") } else if config.EventBufferSize < 0 { return sarama.ConfigurationError("Invalid EventBufferSize") } return nil }