44 "context"
55 "errors"
66 "fmt"
7+ "github.com/ydb-platform/ydb-go-sdk/v3/internal/xslices"
78 "sync/atomic"
89
910 "github.com/ydb-platform/ydb-go-sdk/v3/internal/empty"
@@ -26,6 +27,7 @@ type batcher struct {
2627 closed bool
2728 closeChan empty.Chan
2829 messages batcherMessagesMap
30+ sessionsForFlush []* topicreadercommon.PartitionSession
2931}
3032
3133func newBatcher () * batcher {
@@ -81,6 +83,20 @@ func (b *batcher) PushRawMessage(session *topicreadercommon.PartitionSession, m
8183 return b .addNeedLock (session , newBatcherItemRawMessage (m ))
8284}
8385
86+ func (b * batcher ) FlushPartitionSession (session * topicreadercommon.PartitionSession ) {
87+ b .m .WithLock (func () {
88+ if b .closed {
89+ return
90+ }
91+
92+ if _ , ok := b .messages [session ]; ! ok {
93+ return
94+ }
95+
96+ b .sessionsForFlush = append (b .sessionsForFlush , session )
97+ })
98+ }
99+
84100func (b * batcher ) addNeedLock (session * topicreadercommon.PartitionSession , item batcherMessageOrderItem ) error {
85101 var currentItems batcherMessageOrderItems
86102 var ok bool
@@ -250,31 +266,59 @@ func (b *batcher) findNeedLock(filter batcherGetOptions) batcherResultCandidate
250266 return batcherResultCandidate {}
251267 }
252268
253- rawMessageOpts := batcherGetOptions {rawMessagesOnly : true }
254-
255269 var batchResult batcherResultCandidate
256270 needBatchResult := true
257271
258- for k , items := range b .messages {
259- head , rest , ok := rawMessageOpts .cutBatchItemsHead (items )
260- if ok {
261- return newBatcherResultCandidate (k , head , rest , true )
272+ for _ , session := range b .sessionsForFlush {
273+ items := b .messages [session ]
274+ candidate := b .findNeedLockProcessItem (filter , session , items , true )
275+ if ! candidate .Result .IsEmpty () {
276+ return candidate
262277 }
278+ }
263279
264- if needBatchResult {
265- head , rest , ok = b .applyForceFlagToOptions (filter ).cutBatchItemsHead (items )
266- if ! ok {
267- continue
268- }
280+ for session , items := range b .messages {
281+ candidate := b .findNeedLockProcessItem (filter , session , items , needBatchResult )
282+
283+ if candidate .Result .IsRawMessage () {
284+ return candidate
285+ }
269286
287+ if candidate .Result .IsBatch () {
288+ batchResult = candidate
270289 needBatchResult = false
271- batchResult = newBatcherResultCandidate (k , head , rest , true )
272290 }
273291 }
274292
275293 return batchResult
276294}
277295
296+ func (b * batcher ) findNeedLockProcessItem (
297+ filter batcherGetOptions ,
298+ k * topicreadercommon.PartitionSession ,
299+ items batcherMessageOrderItems ,
300+ needBatchResult bool ,
301+ ) (
302+ result batcherResultCandidate ,
303+ ) {
304+ rawMessageOpts := batcherGetOptions {rawMessagesOnly : true }
305+ head , rest , ok := rawMessageOpts .cutBatchItemsHead (items )
306+ if ok {
307+ return newBatcherResultCandidate (k , head , rest , true )
308+ }
309+
310+ if needBatchResult {
311+ head , rest , ok = b .applyForceFlagToOptions (filter ).cutBatchItemsHead (items )
312+ if ! ok {
313+ return batcherResultCandidate {}
314+ }
315+
316+ return newBatcherResultCandidate (k , head , rest , true )
317+ }
318+
319+ return batcherResultCandidate {}
320+ }
321+
278322func (b * batcher ) applyForceFlagToOptions (options batcherGetOptions ) batcherGetOptions {
279323 if ! b .forceIgnoreMinRestrictionsOnNextMessagesBatch {
280324 return options
@@ -289,6 +333,9 @@ func (b *batcher) applyForceFlagToOptions(options batcherGetOptions) batcherGetO
289333func (b * batcher ) applyNeedLock (res * batcherResultCandidate ) {
290334 if res .Rest .IsEmpty () && res .WaiterIndex >= 0 {
291335 delete (b .messages , res .Key )
336+ if len (b .sessionsForFlush ) > 0 && b .sessionsForFlush [0 ] == res .Key {
337+ b .sessionsForFlush = xslices .Delete (b .sessionsForFlush , 0 , 1 )
338+ }
292339 } else {
293340 b .messages [res .Key ] = res .Rest
294341 }
0 commit comments