alitto / pond

🔘 Minimalistic and High-performance goroutine worker pool written in Go
MIT License
1.5k stars 65 forks source link

Fail tasks before while they are queued #75

Open tynrol opened 6 days ago

tynrol commented 6 days ago

Hello @alitto

I'm in need of feature to cancel tasks forsefully from pond queue, and there are ways to do it outside of pond but this is the prefered one. I'll gladly implement it myself, so the question is are you willing to integrate it in your project or i should implement it via fork?

alitto commented 6 days ago

Hey @tynrol

As you point out, the best way to cancel a particular task at the moment is to pass a custom cancellable context to it directly in the function body, and cancel it from the outside, e.g.:

ctx, cancel := context.WithCancel(context.Background())

pool.Submit(func() {
  select {
    case <-ctx.Done():
        fmt.Println("task canceled")
    case <-taskCompleted:
  }
  return
})

cancel() // Cancel the task

I'd like to know more about your proposal, please share a code example here to discuss. The preferred way to send contributions is via a fork of this repo.