@@ -21,14 +21,6 @@ type batch struct {
21
21
done multiDone
22
22
}
23
23
24
- type batcherSettings [T any ] struct {
25
- sizerType request.SizerType
26
- sizer request.Sizer [T ]
27
- partitioner Partitioner [T ]
28
- next sender.SendFunc [T ]
29
- maxWorkers int
30
- }
31
-
32
24
// shardBatcher continuously batch incoming requests and flushes asynchronously if minimum size limit is met or on timeout.
33
25
type shardBatcher struct {
34
26
cfg BatchConfig
@@ -43,6 +35,17 @@ type shardBatcher struct {
43
35
shutdownCh chan struct {}
44
36
}
45
37
38
+ func newShard (cfg BatchConfig , sizerType request.SizerType , sizer request.Sizer [request.Request ], workerPool chan struct {}, next sender.SendFunc [request.Request ]) * shardBatcher {
39
+ return & shardBatcher {
40
+ cfg : cfg ,
41
+ workerPool : workerPool ,
42
+ sizerType : sizerType ,
43
+ sizer : sizer ,
44
+ consumeFunc : next ,
45
+ shutdownCh : make (chan struct {}, 1 ),
46
+ }
47
+ }
48
+
46
49
func (qb * shardBatcher ) resetTimer () {
47
50
if qb .cfg .FlushTimeout > 0 {
48
51
qb .timer .Reset (qb .cfg .FlushTimeout )
@@ -88,7 +91,7 @@ func (qb *shardBatcher) Consume(ctx context.Context, req request.Request, done D
88
91
}
89
92
90
93
reqList , mergeSplitErr := qb .currentBatch .req .MergeSplit (ctx , int (qb .cfg .MaxSize ), qb .sizerType , req )
91
- // If failed to merge signal all Done callbacks from current batch as well as the current request and reset the current batch.
94
+ // If failed to merge signal all Done callbacks from the current batch as well as the current request and reset the current batch.
92
95
if mergeSplitErr != nil || len (reqList ) == 0 {
93
96
done .OnDone (mergeSplitErr )
94
97
qb .currentBatchMu .Unlock ()
@@ -102,7 +105,7 @@ func (qb *shardBatcher) Consume(ctx context.Context, req request.Request, done D
102
105
103
106
// We have at least one result in the reqList, if more results here is what that means:
104
107
// - First result will contain items from the current batch + some results from the current request.
105
- // - All other results except first will contain items only from current request.
108
+ // - All other results except first will contain items only from the current request.
106
109
// - Last result may not have enough data to be flushed.
107
110
108
111
// Logic on how to deal with the current batch:
@@ -145,27 +148,22 @@ func (qb *shardBatcher) Consume(ctx context.Context, req request.Request, done D
145
148
}
146
149
}
147
150
148
- // startTimeBasedFlushingGoroutine starts a goroutine that flushes on timeout.
149
- func (qb * shardBatcher ) startTimeBasedFlushingGoroutine () {
150
- qb .stopWG .Add (1 )
151
- go func () {
152
- defer qb .stopWG .Done ()
153
- for {
154
- select {
155
- case <- qb .shutdownCh :
156
- return
157
- case <- qb .timer .C :
158
- qb .flushCurrentBatchIfNecessary ()
159
- }
160
- }
161
- }()
162
- }
163
-
164
151
// Start starts the goroutine that reads from the queue and flushes asynchronously.
165
152
func (qb * shardBatcher ) start (_ context.Context , _ component.Host ) {
166
153
if qb .cfg .FlushTimeout > 0 {
167
154
qb .timer = time .NewTimer (qb .cfg .FlushTimeout )
168
- qb .startTimeBasedFlushingGoroutine ()
155
+ qb .stopWG .Add (1 )
156
+ go func () {
157
+ defer qb .stopWG .Done ()
158
+ for {
159
+ select {
160
+ case <- qb .shutdownCh :
161
+ return
162
+ case <- qb .timer .C :
163
+ qb .flushCurrentBatchIfNecessary ()
164
+ }
165
+ }
166
+ }()
169
167
}
170
168
}
171
169
0 commit comments