Skip to content

Commit

Permalink
cshared: Make goroutine for Collect to be able to exit
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Hatake <[email protected]>
  • Loading branch information
cosmo0920 committed Oct 6, 2023
1 parent 1a9a1aa commit 6d38086
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
10 changes: 9 additions & 1 deletion cshared.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,16 @@ func prepareInputCollector() (err error) {

go func(theChannel chan<- Message) {
defer theInputLock.Unlock()
for {
select {
case <-runCtx.Done():
log.Printf("goroutine will be stopping: name=%q\n", theName)
return
default:
err = theInput.Collect(runCtx, theChannel)
}
}

err := theInput.Collect(runCtx, theChannel)
if err != nil {
fmt.Fprintf(os.Stderr,
"collect error: %s\n", err.Error())
Expand Down
13 changes: 6 additions & 7 deletions cshared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,16 @@ func TestInputCallbackDangle(t *testing.T) {
cdone := make(chan bool)
ptr := unsafe.Pointer(nil)

// prepare channel for input explicitly.
err := prepareInputCollector()
if err != nil {
t.Fail()
}

go func(test *testing.T) {
t := time.NewTicker(collectInterval)
defer t.Stop()

// prepare channel for input explicitly.
err := prepareInputCollector()
if err != nil {
test.Fail()
return
}

FLBPluginInputCallback(&ptr, nil)
for {
select {
Expand Down

0 comments on commit 6d38086

Please sign in to comment.