Skip to content

Commit 15201f1

Browse files
authored
Prevent starting unnecessary goroutines (#9817)
Fixes #9739 Replaces #9814 Signed-off-by: Bogdan Drutu <[email protected]>
1 parent 2037527 commit 15201f1

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: processor/batch
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Prevent starting unnecessary goroutines.
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [9739]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: []

processor/batchprocessor/batch_processor.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ func newBatchProcessor(set processor.CreateSettings, cfg *Config, batchFunc func
129129
metadataLimit: int(cfg.MetadataCardinalityLimit),
130130
}
131131
if len(bp.metadataKeys) == 0 {
132-
bp.batcher = &singleShardBatcher{batcher: bp.newShard(nil)}
132+
s := bp.newShard(nil)
133+
s.start()
134+
bp.batcher = &singleShardBatcher{batcher: s}
133135
} else {
134136
bp.batcher = &multiShardBatcher{
135137
batchProcessor: bp,
@@ -156,8 +158,6 @@ func (bp *batchProcessor) newShard(md map[string][]string) *shard {
156158
exportCtx: exportCtx,
157159
batch: bp.batchFunc(),
158160
}
159-
b.processor.goroutines.Add(1)
160-
go b.start()
161161
return b
162162
}
163163

@@ -180,6 +180,11 @@ func (bp *batchProcessor) Shutdown(context.Context) error {
180180
}
181181

182182
func (b *shard) start() {
183+
b.processor.goroutines.Add(1)
184+
go b.startLoop()
185+
}
186+
187+
func (b *shard) startLoop() {
183188
defer b.processor.goroutines.Done()
184189

185190
// timerCh ensures we only block when there is a
@@ -320,6 +325,8 @@ func (mb *multiShardBatcher) consume(ctx context.Context, data any) error {
320325
var loaded bool
321326
b, loaded = mb.batchers.LoadOrStore(aset, mb.newShard(md))
322327
if !loaded {
328+
// Start the goroutine only if we added the object to the map, otherwise is already started.
329+
b.(*shard).start()
323330
mb.size++
324331
}
325332
mb.lock.Unlock()

0 commit comments

Comments
 (0)