Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go1.23 timers #11334

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions processor/batchprocessor/batch_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,11 @@ type batch interface {
sizeBytes(item any) int
}

var _ consumer.Traces = (*batchProcessor)(nil)
var _ consumer.Metrics = (*batchProcessor)(nil)
var _ consumer.Logs = (*batchProcessor)(nil)
var (
_ consumer.Traces = (*batchProcessor)(nil)
_ consumer.Metrics = (*batchProcessor)(nil)
_ consumer.Logs = (*batchProcessor)(nil)
)

// newBatchProcessor returns a new batch processor component.
func newBatchProcessor(set processor.Settings, cfg *Config, batchFunc func() batch) (*batchProcessor, error) {
Expand Down Expand Up @@ -259,12 +261,6 @@ func (b *shard) hasTimer() bool {
return b.timer != nil
}

func (b *shard) stopTimer() {
if b.hasTimer() && !b.timer.Stop() {
<-b.timer.C
}
}

func (b *shard) resetTimer() {
if b.hasTimer() {
b.timer.Reset(b.processor.timeout)
Expand Down
12 changes: 12 additions & 0 deletions processor/batchprocessor/batch_processor_1_22.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:build !go1.23

package batchprocessor // import "go.opentelemetry.io/collector/processor/batchprocessor"

func (b *shard) stopTimer() {
if b.hasTimer() && !b.timer.Stop() {
<-b.timer.C
}
}
12 changes: 12 additions & 0 deletions processor/batchprocessor/batch_processor_1_23.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:build go1.23

package batchprocessor // import "go.opentelemetry.io/collector/processor/batchprocessor"

func (b *shard) stopTimer() {
if b.hasTimer() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to add a test to capture the regression this is fixing?

b.timer.Stop()
}
}
32 changes: 32 additions & 0 deletions processor/batchprocessor/batch_processor_timer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//go:build go1.23

package batchprocessor

import (
"context"
"testing"
"time"
)

func TestShardTimer(t *testing.T) {
_timer := time.NewTimer(1 * time.Millisecond)
testShard := shard{
processor: nil,
exportCtx: context.Background(),
timer: _timer,
newItem: make(chan any),
batch: nil,
}
done := make(chan bool)
go func() {
time.Sleep(3 * time.Millisecond)
testShard.stopTimer()
done <- true
}()
select {
case <-time.NewTimer(50 * time.Millisecond).C:
t.Fail()
case <-done:
break
}
}