Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: duplicate subscribe to shard #19

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion kinesumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,12 +539,17 @@ func (k *Kinesumer) consumePipe(stream string, shard *Shard) {
func (k *Kinesumer) subscribeToShard(streamEvents chan kinesis.SubscribeToShardEventStreamEvent, stream string, shard *Shard) {
defer close(streamEvents)

var (
subscribeStart time.Time
consumerARN string
)
for {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)

newConsumerARN := k.efoMeta[stream].consumerARN
input := &kinesis.SubscribeToShardInput{
ConsumerARN: aws.String(k.efoMeta[stream].consumerARN),
ConsumerARN: aws.String(newConsumerARN),
ShardId: aws.String(shard.ID),
StartingPosition: &kinesis.StartingPosition{
Type: aws.String(kinesis.ShardIteratorTypeLatest),
Expand All @@ -556,12 +561,30 @@ func (k *Kinesumer) subscribeToShard(streamEvents chan kinesis.SubscribeToShardE
input.StartingPosition.SetSequenceNumber(seq.(string))
}

// NOTE(proost): https://docs.aws.amazon.com/kinesis/latest/APIReference/API_SubscribeToShard.html
switch {
case subscribeStart.IsZero(): // first time.
case consumerARN != newConsumerARN: // consumer changed.
default:
time.Sleep(subscribeStart.Add(5 * time.Second).Sub(time.Now()))
}

output, err := k.client.SubscribeToShardWithContext(ctx, input)
if err != nil {
var awsErr awserr.Error
if errors.As(err, &awsErr) {
if awsErr.Code() == kinesis.ErrCodeResourceInUseException {
subscribeStart = time.Now()
consumerARN = newConsumerARN
}
}

k.sendOrDiscardError(errors.WithStack(err))
cancel()
continue
}
subscribeStart = time.Now()
consumerARN = newConsumerARN

open := true
for open {
Expand All @@ -576,6 +599,10 @@ func (k *Kinesumer) subscribeToShard(streamEvents chan kinesis.SubscribeToShardE
return
case e, ok := <-output.GetEventStream().Events():
if !ok {
err := output.GetStream().Close()
if err != nil {
k.sendOrDiscardError(errors.WithStack(err))
}
cancel()
open = false
}
Expand Down