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

Cleanup: Replace custom goroutine shutdown listeners with shared context wrapper #38957

Merged
merged 2 commits into from
Apr 23, 2024
Merged
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
19 changes: 19 additions & 0 deletions filebeat/input/v2/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
package v2

import (
"context"
"time"

"github.com/elastic/beats/v7/libbeat/beat"
conf "github.com/elastic/elastic-agent-libs/config"
"github.com/elastic/elastic-agent-libs/logp"
Expand Down Expand Up @@ -111,3 +114,19 @@ type Canceler interface {
Done() <-chan struct{}
Err() error
}

type cancelerCtx struct {
Canceler
}

func GoContextFromCanceler(c Canceler) context.Context {
return cancelerCtx{c}
}

func (c cancelerCtx) Deadline() (deadline time.Time, ok bool) {
return time.Time{}, false
}

func (c cancelerCtx) Value(_ any) any {
return nil
}
14 changes: 1 addition & 13 deletions x-pack/filebeat/input/awscloudwatch/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,7 @@ func (in *cloudwatchInput) Test(ctx v2.TestContext) error {
}

func (in *cloudwatchInput) Run(inputContext v2.Context, pipeline beat.Pipeline) error {
var err error

// Wrap input Context's cancellation Done channel a context.Context. This
// goroutine stops with the parent closes the Done channel.
ctx, cancelInputCtx := context.WithCancel(context.Background())
go func() {
defer cancelInputCtx()
select {
case <-inputContext.Cancelation.Done():
case <-ctx.Done():
}
}()
defer cancelInputCtx()
ctx := v2.GoContextFromCanceler(inputContext.Cancelation)

// Create client for publishing events and receive notification of their ACKs.
client, err := pipeline.ConnectWith(beat.ClientConfig{
Expand Down
12 changes: 1 addition & 11 deletions x-pack/filebeat/input/awss3/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,7 @@ func (in *s3Input) Run(inputContext v2.Context, pipeline beat.Pipeline) error {
return fmt.Errorf("can not start persistent store: %w", err)
}

// Wrap input Context's cancellation Done channel a context.Context. This
// goroutine stops with the parent closes the Done channel.
ctx, cancelInputCtx := context.WithCancel(context.Background())
go func() {
defer cancelInputCtx()
select {
case <-inputContext.Cancelation.Done():
case <-ctx.Done():
}
}()
defer cancelInputCtx()
ctx := v2.GoContextFromCanceler(inputContext.Cancelation)

if in.config.QueueURL != "" {
regionName, err := getRegionFromQueueURL(in.config.QueueURL, in.config.AWSConfig.Endpoint, in.config.RegionName)
Expand Down
Loading