Skip to content

Commit

Permalink
update readme, breaking change: config is now pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
falmar committed Aug 6, 2023
1 parent 891316e commit 36c1b9b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ package main
import (
"context"
"fmt"
"github.com/falmar/krun"
"os"
"time"

"github.com/falmar/krun"
)

func main() {
queue := krun.New(krun.NewConfig{
queue := krun.New(&krun.Config{
Size: 5, // number of workers
WaitSleep: time.Millisecond * 10,
WaitSleep: time.Microsecond,
})

job := func(ctx context.Context) (interface{}, error) {
Expand All @@ -47,7 +48,9 @@ func main() {
os.Exit(1)
}

fmt.Println("Result:", res.Data)
queue.Wait(ctx)

fmt.Println("Result:", res.Data.(string))
}
```

Expand All @@ -57,5 +60,5 @@ Krun is released under the MIT License. See [LICENSE](LICENSE) for more informat


## TODO:
- [ ] unit test
- [x] unit test
- [ ] go releaser
33 changes: 17 additions & 16 deletions examples/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,33 @@ package main
import (
"context"
"fmt"
"math/rand"
"os"
"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,
queue := krun.New(&krun.Config{
Size: 5, // number of workers
WaitSleep: time.Microsecond,
})

for i := 0; i < 100; i++ {
ctx := context.WithValue(ctx, "key", i)
job := func(ctx context.Context) (interface{}, error) {
time.Sleep(time.Millisecond * 100)
return "Hello, Krun!", nil
}

r := k.Run(ctx, func(ctx context.Context) (interface{}, error) {
// do some work
time.Sleep(time.Millisecond * (300 + time.Duration(random.Intn(700))))
return ctx.Value("key"), nil
})
ctx := context.Background()
resChan := queue.Run(ctx, job)

go func(r <-chan *krun.Result) {
fmt.Println("hello from index:", (<-r).Data)
}(r)
res := <-resChan
if res.Error != nil {
fmt.Println("Error:", res.Error)
os.Exit(1)
}

k.Wait(context.Background())
queue.Wait(ctx)

fmt.Println("Result:", res.Data.(string))
}
4 changes: 2 additions & 2 deletions krun.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ type worker struct {
result chan *Result
}

type NewConfig struct {
type Config struct {
Size int
WaitSleep time.Duration
}

func New(cfg NewConfig) Krun {
func New(cfg *Config) Krun {
k := &krun{
n: cfg.Size,
workers: make(chan *worker, cfg.Size),
Expand Down
6 changes: 3 additions & 3 deletions krun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestNew(t *testing.T) {
t.Parallel()

t.Run("returns a Krun", func(t *testing.T) {
k := New(NewConfig{})
k := New(&Config{})
if k == nil {
t.Errorf("Expected Krun, got nil")
}
Expand All @@ -24,7 +24,7 @@ func TestNew(t *testing.T) {
})

t.Run("returns a Krun with the correct size", func(t *testing.T) {
k := New(NewConfig{Size: 5}).(*krun)
k := New(&Config{Size: 5}).(*krun)

if k.n != 5 {
t.Errorf("Expected 5, got %v", k.Size())
Expand All @@ -36,7 +36,7 @@ func TestNew(t *testing.T) {
})

t.Run("returns a Krun with the correct waitSleep", func(t *testing.T) {
k := New(NewConfig{WaitSleep: time.Second}).(*krun)
k := New(&Config{WaitSleep: time.Second}).(*krun)

if k.waitSleep != time.Second {
t.Errorf("Expected 1s, got %v", k.waitSleep)
Expand Down

0 comments on commit 36c1b9b

Please sign in to comment.