-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[chore] [exporterhelper] Move workers from queue to queueSender (#8898)
Move the common workers loop logic from each queue implementation to the sender. Based on #8828
- Loading branch information
Showing
10 changed files
with
163 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package internal // import "go.opentelemetry.io/collector/exporter/exporterhelper/internal" | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
) | ||
|
||
type QueueConsumers[T any] struct { | ||
queue Queue[T] | ||
numConsumers int | ||
callback func(context.Context, T) | ||
stopWG sync.WaitGroup | ||
} | ||
|
||
func NewQueueConsumers[T any](q Queue[T], numConsumers int, callback func(context.Context, T)) *QueueConsumers[T] { | ||
return &QueueConsumers[T]{ | ||
queue: q, | ||
numConsumers: numConsumers, | ||
callback: callback, | ||
stopWG: sync.WaitGroup{}, | ||
} | ||
} | ||
|
||
// Start ensures that all consumers are started. | ||
func (c *QueueConsumers[T]) Start() { | ||
var startWG sync.WaitGroup | ||
for i := 0; i < c.numConsumers; i++ { | ||
c.stopWG.Add(1) | ||
startWG.Add(1) | ||
go func() { | ||
startWG.Done() | ||
defer c.stopWG.Done() | ||
for { | ||
item, success := c.queue.Poll() | ||
if !success { | ||
return | ||
} | ||
c.callback(item.Context, item.Request) | ||
item.OnProcessingFinished() | ||
} | ||
}() | ||
} | ||
startWG.Wait() | ||
} | ||
|
||
// Shutdown ensures that all consumers are stopped. | ||
func (c *QueueConsumers[T]) Shutdown() { | ||
c.stopWG.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.