Skip to content

Commit

Permalink
removed error from New
Browse files Browse the repository at this point in the history
  • Loading branch information
hmoragrega committed Jan 30, 2021
1 parent b3e28e0 commit 35d05d8
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ type Config struct {

To have a pool with a tweaked config you can call `NewWithConfig`
```go
pool, err := workers.NewWithConfig(workers.Config{
pool := workers.Must(NewWithConfig(workers.Config{
Min: 3,
Max: 10,
Initial: 5,
})
}))
```

#### Starting the pool
Expand Down Expand Up @@ -197,9 +197,9 @@ still be running concurrently if there are more workers).

As an exercise let's log the job result with our favourite logging library.
```go
// jobLogger is a middleware that logs the result of a job
// loggerMiddleware is a middleware that logs the result of a job
// with "debug" or "error" level depending on the result.
jobLogger := func(name string) workers.MiddlewareFunc {
loggerMiddleware := func(name string) workers.MiddlewareFunc {
return func(job workers.Job) workers.Job {
return workers.JobFunc(func(ctx context.Context) error {
err := job.Do(ctx)
Expand All @@ -212,7 +212,7 @@ jobLogger := func(name string) workers.MiddlewareFunc {
}
}

pool := workers.Must(workers.New(jobLogger("my-job")))
pool := workers.New(loggerMiddleware("my-job"))
pool.Start(workers.JobFunc(func(ctx context.Context) error {
return someWork()
}))
Expand Down
2 changes: 1 addition & 1 deletion middleware/counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestCounterMiddleware(t *testing.T) {
return nil
})

p := workers.Must(workers.New(&counter))
p := workers.New(&counter)
if err := p.Start(job); err != nil {
t.Fatal("cannot start pool", err)
}
Expand Down
2 changes: 1 addition & 1 deletion middleware/elapsed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestElapsedMiddleware(t *testing.T) {
return nil
})

p := workers.Must(workers.New(&elapsed))
p := workers.New(&elapsed)
if err := p.Start(job); err != nil {
t.Fatal("cannot start pool", err)
}
Expand Down
5 changes: 2 additions & 3 deletions middleware/wait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ func TestWaitMiddleware_Wait(t *testing.T) {
return nil
})

p := workers.Must(workers.New(Wait(tc.wait)))

p := workers.New(Wait(tc.wait))
poolStarted := time.Now()
if err := p.Start(job); err != nil {
t.Fatal("cannot start pool", err)
Expand All @@ -62,7 +61,7 @@ func TestWaitMiddleware_Cancelled(t *testing.T) {
return nil
})

p := workers.Must(workers.New(Wait(time.Second)))
p := workers.New(Wait(time.Second))
if err := p.Start(job); err != nil {
t.Fatal("cannot start pool", err)
}
Expand Down
21 changes: 18 additions & 3 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,16 @@ type Config struct {
// New creates a new pool with the default configuration.
//
// It accepts an arbitrary number of job middlewares to run.
func New(middlewares ...Middleware) (*Pool, error) {
return NewWithConfig(Config{}, middlewares...)
func New(middlewares ...Middleware) *Pool {
return newDefault(middlewares...)
}

// NewWithConfig creates a new pool with an specific configuration.
//
// It accepts an arbitrary number of job middlewares to run.
func NewWithConfig(cfg Config, middlewares ...Middleware) (*Pool, error) {
if cfg.Initial == 0 {
cfg.Initial = 1
cfg.Initial = defaultInitial
}
if cfg.Min < 0 {
return nil, fmt.Errorf("%w: min %d", ErrInvalidMin, cfg.Min)
Expand Down Expand Up @@ -143,6 +143,21 @@ func Must(p *Pool, err error) *Pool {
return p
}

const (
defaultMin = 0
defaultMax = 0
defaultInitial = 1
)

func newDefault(middlewares ...Middleware) *Pool {
return &Pool{
min: defaultMin,
max: defaultMax,
initial: defaultInitial,
mws: middlewares,
}
}

// Pool is a pool of workers that can be started
// to run a job non-stop concurrently.
type Pool struct {
Expand Down

0 comments on commit 35d05d8

Please sign in to comment.