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

Adding lo.WaitFor #269

Merged
merged 4 commits into from
Jun 28, 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
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ Concurrency helpers:
- [Synchronize](#synchronize)
- [Async](#async)
- [Transaction](#transaction)
- [WaitFor](#waitfor)

Error handling:

Expand Down Expand Up @@ -2837,6 +2838,38 @@ _, _ = transaction.Process(-5)
// rollback 1
```

### WaitFor

Runs periodically until a condition is validated.

```go
alwaysTrue := func(i int) bool { return true }
alwaysFalse := func(i int) bool { return false }
laterTrue := func(i int) bool {
return i > 5
}

iterations, duration, ok := lo.WaitFor(alwaysTrue, 10*time.Millisecond, time.Millisecond)
// 1
// 0ms
// true

iterations, duration, ok := lo.WaitFor(alwaysFalse, 10*time.Millisecond, time.Millisecond)
// 10
// 10ms
// false

iterations, duration, ok := lo.WaitFor(laterTrue, 10*time.Millisecond, time.Millisecond)
// 7
// 7ms
// true

iterations, duration, ok := lo.WaitFor(laterTrue, 10*time.Millisecond, 5*time.Millisecond)
// 2
// 10ms
// false
```

### Validate

Helper function that creates an error when a condition is not met.
Expand Down
37 changes: 36 additions & 1 deletion concurrency.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package lo

import "sync"
import (
"sync"
"time"
)

type synchronize struct {
locker sync.Locker
Expand Down Expand Up @@ -93,3 +96,35 @@ func Async6[A, B, C, D, E, F any](f func() (A, B, C, D, E, F)) <-chan Tuple6[A,
}()
return ch
}

// WaitFor runs periodically until a condition is validated.
func WaitFor(condition func(i int) bool, maxDuration time.Duration, tick time.Duration) (int, time.Duration, bool) {
Copy link
Contributor

Choose a reason for hiding this comment

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

What about this

Suggested change
func WaitFor(condition func(i int) bool, maxDuration time.Duration, tick time.Duration) (int, time.Duration, bool) {
func WaitFor(condition func(i int) bool, maxDuration time.Duration, tick time.Duration) (int, time.Duration, bool) {
return WaitForWithCtx(context.Background(), condition, maxDuration, tick)
}
// WaitForWithCtx runs periodically until a condition is validated or context is cancelled
func WaitForWithCtx(ctx context.Context, condition func(i int) bool, maxDuration time.Duration, tick time.Duration) (int, time.Duration, bool) {
if condition(0) {
return 1, 0, true
}
start := time.Now()
timer := time.NewTimer(maxDuration)
ticker := time.NewTicker(tick)
defer func() {
timer.Stop()
ticker.Stop()
}()
i := 1
for {
select {
case <-ctx.Done():
return i, time.Since(start), false
case <-timer.C:
return i, time.Since(start), false
case <-ticker.C:
if condition(i) {
return i + 1, time.Since(start), true
}
i++
}
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Owner Author

Choose a reason for hiding this comment

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

Please open a new PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

if condition(0) {
return 1, 0, true
}

start := time.Now()

timer := time.NewTimer(maxDuration)
ticker := time.NewTicker(tick)

defer func() {
timer.Stop()
ticker.Stop()
}()

i := 1

for {
select {
case <-timer.C:
return i, time.Since(start), false
case <-ticker.C:
if condition(i) {
return i + 1, time.Since(start), true
}

i++
}
}
}
44 changes: 44 additions & 0 deletions concurrency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,47 @@ func TestAsyncX(t *testing.T) {
}
}
}

func TestWaitFor(t *testing.T) {
t.Parallel()
testWithTimeout(t, 100*time.Millisecond)
is := assert.New(t)

alwaysTrue := func(i int) bool { return true }
alwaysFalse := func(i int) bool { return false }

iter, duration, ok := WaitFor(alwaysTrue, 10*time.Millisecond, time.Millisecond)
is.Equal(1, iter)
is.Equal(time.Duration(0), duration)
is.True(ok)
iter, duration, ok = WaitFor(alwaysFalse, 10*time.Millisecond, 4*time.Millisecond)
is.Equal(3, iter)
is.InEpsilon(10*time.Millisecond, duration, float64(500*time.Microsecond))
is.False(ok)

laterTrue := func(i int) bool {
return i >= 5
}

iter, duration, ok = WaitFor(laterTrue, 10*time.Millisecond, time.Millisecond)
is.Equal(6, iter)
is.InEpsilon(6*time.Millisecond, duration, float64(500*time.Microsecond))
is.True(ok)
iter, duration, ok = WaitFor(laterTrue, 10*time.Millisecond, 5*time.Millisecond)
is.Equal(2, iter)
is.InEpsilon(10*time.Millisecond, duration, float64(500*time.Microsecond))
is.False(ok)

counter := 0

alwaysFalse = func(i int) bool {
is.Equal(counter, i)
counter++
return false
}

iter, duration, ok = WaitFor(alwaysFalse, 10*time.Millisecond, time.Millisecond)
is.Equal(10, iter)
is.InEpsilon(10*time.Millisecond, duration, float64(500*time.Microsecond))
is.False(ok)
}
Loading