@@ -161,6 +161,10 @@ type partitionConsumer struct {
161
161
startMessageID atomicMessageID
162
162
lastDequeuedMsg * trackingMessageID
163
163
164
+ // This is used to track the seeking message id during the seek operation.
165
+ // It will be set to nil after seek completes and reconnected.
166
+ seekMessageID atomicMessageID
167
+
164
168
currentQueueSize uAtomic.Int32
165
169
scaleReceiverQueueHint uAtomic.Bool
166
170
incomingMessages uAtomic.Int32
@@ -365,6 +369,7 @@ func newPartitionConsumer(parent Consumer, client *client, options *partitionCon
365
369
maxQueueSize : int32 (options .receiverQueueSize ),
366
370
queueCh : make (chan []* message , options .receiverQueueSize ),
367
371
startMessageID : atomicMessageID {msgID : options .startMessageID },
372
+ seekMessageID : atomicMessageID {msgID : nil },
368
373
connectedCh : make (chan struct {}),
369
374
messageCh : messageCh ,
370
375
connectClosedCh : make (chan * connectionClosed , 1 ),
@@ -1023,7 +1028,7 @@ func (pc *partitionConsumer) requestSeek(msgID *messageID) error {
1023
1028
// 2. The startMessageID is reset to ensure accurate judgment when calling hasNext next time.
1024
1029
// Since the messages in the queue are cleared here reconnection won't reset startMessageId.
1025
1030
pc .lastDequeuedMsg = nil
1026
- pc .startMessageID .set (toTrackingMessageID (msgID ))
1031
+ pc .seekMessageID .set (toTrackingMessageID (msgID ))
1027
1032
pc .clearQueueAndGetNextMessage ()
1028
1033
return nil
1029
1034
}
@@ -1481,6 +1486,11 @@ func (pc *partitionConsumer) messageShouldBeDiscarded(msgID *trackingMessageID)
1481
1486
return false
1482
1487
}
1483
1488
1489
+ // if we start at latest message, we should never discard
1490
+ if pc .startMessageID .get ().equal (latestMessageID ) {
1491
+ return false
1492
+ }
1493
+
1484
1494
if pc .options .startMessageIDInclusive {
1485
1495
return pc .startMessageID .get ().greater (msgID .messageID )
1486
1496
}
@@ -1977,7 +1987,12 @@ func (pc *partitionConsumer) grabConn(assignedBrokerURL string) error {
1977
1987
KeySharedMeta : keySharedMeta ,
1978
1988
}
1979
1989
1980
- pc .startMessageID .set (pc .clearReceiverQueue ())
1990
+ if seekMsgID := pc .seekMessageID .get (); seekMsgID != nil {
1991
+ pc .startMessageID .set (seekMsgID )
1992
+ pc .seekMessageID .set (nil ) // Reset seekMessageID to nil to avoid persisting state across reconnects
1993
+ } else {
1994
+ pc .startMessageID .set (pc .clearReceiverQueue ())
1995
+ }
1981
1996
if pc .options .subscriptionMode != Durable {
1982
1997
// For regular subscriptions the broker will determine the restarting point
1983
1998
cmdSubscribe .StartMessageId = convertToMessageIDData (pc .startMessageID .get ())
0 commit comments