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

New helpers for concurrency: lo.Timeout + lo.Deadline + lo.Race #267

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

samber
Copy link
Owner

@samber samber commented Nov 15, 2022

  • Timeout: func Timeout(duration time.Duration, callback func(done func())) error
  • Deadline: func Deadline(duration time.Time, callback func(done func())) error
  • Race: func Race(callbacks ...func(done func()))

Timeout and Deadline should not be used when a context is available.

lo.Timeout(2*time.Second, func(done func()) {
    // very long job

   done()

   // free stuff
})
lo.Deadline(myDate, func(done func()) {
    // very long job

   done()

   // free stuff
})
funcA := func(done func()) {
    // very long job

   done()

   // free stuff
}
funcB := func(done func()) {
    // very long job

   done()

   // free stuff
}
funcC := func(done func()) {
    // very long job

   done()

   // free stuff
}

lo.Race(funcA, funcB, funcC)

I'm not really sure Timeout and Deadline should be added to this library, since context.WithTimeout and context.WithDeadline are highly recommended.

For all helpers, isDone func() bool might be passed to callbacks.

Should we add a timeout/deadline to lo.Race()? More helpers lo.RaceWithTimeout() lo.RaceWithDeadline()?

Should lo.Race() return anything ?

WDYT ?

@samber samber force-pushed the feat-concurrency-timeout-deadline-race branch from 433de2c to 2aaada2 Compare November 15, 2022 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant