@@ -21,7 +21,8 @@ type Subscriber struct {
21
21
dynamoSvc DynamoService
22
22
streamSvc StreamService
23
23
24
- shards sync.Map
24
+ shards sync.Map
25
+ shardCount int
25
26
26
27
shardProcessQueue chan * shardProcessContext
27
28
@@ -68,9 +69,11 @@ func (r *Subscriber) applyDefaults() {
68
69
}
69
70
70
71
func (r * Subscriber ) SetShardSequences (shardSequences []* ShardSequence ) {
71
- for _ , shardSequence := range shardSequences {
72
- if len (shardSequence .SequenceNumber ) > 0 {
73
- r .shards .Store (shardSequence .ShardId , shardSequence .SequenceNumber )
72
+ if r .shardCount == 0 {
73
+ for _ , shardSequence := range shardSequences {
74
+ if len (shardSequence .SequenceNumber ) > 0 {
75
+ r .shards .Store (shardSequence .ShardId , shardSequence .SequenceNumber )
76
+ }
74
77
}
75
78
}
76
79
}
@@ -211,37 +214,37 @@ func (r *Subscriber) Subscribe() (<-chan *types.Record, <-chan error) {
211
214
r .sendError (err )
212
215
continue
213
216
}
214
- if first {
215
- r .shards .Range (func (key any , value any ) bool {
216
- shardId := key .(string )
217
- sequenceNumber := value .(string )
218
- r .shardProcessQueue <- newShardProcessContext (
219
- & dynamodbstreams.GetShardIteratorInput {
220
- StreamArn : streamArn ,
221
- ShardIteratorType : r .shardSequenceIteratorType ,
222
- ShardId : & shardId ,
223
- SequenceNumber : & sequenceNumber ,
224
- },
225
- r .shardIteratorInitialInterval ,
226
- r .shardIteratorMaxInterval ,
227
- )
228
- return true
229
- })
230
- first = false
231
- }
232
217
for _ , shard := range shards {
218
+ var sequenceNumber * string
219
+ shardIteratorType := r .shardIteratorType
220
+
221
+ if first {
222
+ sqn , ok := r .shards .Load (* shard .ShardId )
223
+ if ok {
224
+ shardIteratorType = r .shardSequenceIteratorType
225
+ sequenceNumberStr := sqn .(string )
226
+ sequenceNumber = & sequenceNumberStr
227
+ r .shards .Delete (* shard .ShardId )
228
+ } else {
229
+ shardIteratorType = r .shardIteratorType
230
+ }
231
+ }
232
+
233
233
if _ , exist := r .shards .LoadOrStore (* shard .ShardId , "" ); ! exist {
234
+ r .shardCount ++
234
235
r .shardProcessQueue <- newShardProcessContext (
235
236
& dynamodbstreams.GetShardIteratorInput {
236
237
StreamArn : streamArn ,
237
- ShardIteratorType : r . shardIteratorType ,
238
+ ShardIteratorType : shardIteratorType ,
238
239
ShardId : shard .ShardId ,
240
+ SequenceNumber : sequenceNumber ,
239
241
},
240
242
r .shardIteratorInitialInterval ,
241
243
r .shardIteratorMaxInterval ,
242
244
)
243
245
}
244
246
}
247
+ first = false
245
248
}
246
249
}
247
250
}()
0 commit comments