Skip to content

Commit

Permalink
fix .Wait
Browse files Browse the repository at this point in the history
  • Loading branch information
falmar committed May 6, 2023
1 parent 01d1de1 commit 9d4af03
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
35 changes: 35 additions & 0 deletions examples/basic/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"context"
"fmt"
"math/rand"
"time"

"github.com/falmar/krun"
)

func main() {
random := rand.New(rand.NewSource(time.Now().UnixNano()))
ctx := context.Background()
k := krun.New(krun.NewConfig{
Size: 10,
})

for i := 0; i < 100; i++ {
ctx := context.WithValue(ctx, "key", i)

r := k.Run(ctx, func(ctx context.Context) (interface{}, error) {
// do some work

return ctx.Value("key"), nil
})

go func(r <-chan *krun.Result) {
time.Sleep(time.Millisecond * (300 + time.Duration(random.Intn(700))))
fmt.Println("hello from index:", (<-r).Data)
}(r)
}

k.Wait(context.Background())
}
3 changes: 2 additions & 1 deletion krun.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func (k *krun) Run(ctx context.Context, f Job) <-chan *Result {
}

func (k *krun) Wait(ctx context.Context) {
breakL:
for {
select {
case <-ctx.Done():
Expand All @@ -83,7 +84,7 @@ func (k *krun) Wait(ctx context.Context) {
continue
}

break
break breakL
}
}
}
Expand Down

0 comments on commit 9d4af03

Please sign in to comment.