Add shard iterator type as config option

Fixes: https://github.com/harlow/kinesis-consumer/issues/74
This commit is contained in:
Harlow Ward 2018-12-28 19:39:47 -08:00
parent 5688ff2820
commit 3527b603d3

View file

@ -47,6 +47,13 @@ func WithClient(client kinesisiface.KinesisAPI) Option {
} }
} }
// ShardIteratorType overrides the starting point for the consumer
func WithShardIteratorType(t string) Option {
return func(c *Consumer) {
c.initialShardIteratorType = t
}
}
// ScanStatus signals the consumer if we should continue scanning for next record // ScanStatus signals the consumer if we should continue scanning for next record
// and whether to checkpoint. // and whether to checkpoint.
type ScanStatus struct { type ScanStatus struct {
@ -65,6 +72,7 @@ func New(streamName string, opts ...Option) (*Consumer, error) {
// new consumer with no-op checkpoint, counter, and logger // new consumer with no-op checkpoint, counter, and logger
c := &Consumer{ c := &Consumer{
streamName: streamName, streamName: streamName,
initialShardIteratorType: "TRIM_HORIZON",
checkpoint: &noopCheckpoint{}, checkpoint: &noopCheckpoint{},
counter: &noopCounter{}, counter: &noopCounter{},
logger: &noopLogger{ logger: &noopLogger{
@ -92,6 +100,7 @@ func New(streamName string, opts ...Option) (*Consumer, error) {
// Consumer wraps the interaction with the Kinesis stream // Consumer wraps the interaction with the Kinesis stream
type Consumer struct { type Consumer struct {
streamName string streamName string
initialShardIteratorType string
client kinesisiface.KinesisAPI client kinesisiface.KinesisAPI
logger Logger logger Logger
checkpoint Checkpoint checkpoint Checkpoint
@ -258,7 +267,7 @@ func (c *Consumer) getShardIterator(streamName, shardID, lastSeqNum string) (*st
params.ShardIteratorType = aws.String("AFTER_SEQUENCE_NUMBER") params.ShardIteratorType = aws.String("AFTER_SEQUENCE_NUMBER")
params.StartingSequenceNumber = aws.String(lastSeqNum) params.StartingSequenceNumber = aws.String(lastSeqNum)
} else { } else {
params.ShardIteratorType = aws.String("TRIM_HORIZON") params.ShardIteratorType = aws.String(c.initialShardIteratorType)
} }
resp, err := c.client.GetShardIterator(params) resp, err := c.client.GetShardIterator(params)