-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
286 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package routinepool | ||
|
||
import ( | ||
"runtime" | ||
"time" | ||
) | ||
|
||
type options struct { | ||
maxWorkers int | ||
idleTimeout time.Duration | ||
maxTaskSize int | ||
} | ||
|
||
func defaultOptions() *options { | ||
return &options{ | ||
maxWorkers: runtime.NumCPU(), | ||
idleTimeout: time.Second, | ||
maxTaskSize: runtime.NumCPU(), | ||
} | ||
} | ||
|
||
// Option defines pool options | ||
type Option interface { | ||
apply(*options) | ||
} | ||
|
||
type optionFunc struct { | ||
f func(*options) | ||
} | ||
|
||
func (o optionFunc) apply(opts *options) { | ||
o.f(opts) | ||
} | ||
|
||
// WithMaxWorkers set max worker routines, default is runtime.NumCPU(). | ||
func WithMaxWorkers(maxWorkers int) Option { | ||
return optionFunc{func(o *options) { | ||
if maxWorkers <= 0 { | ||
maxWorkers = runtime.NumCPU() | ||
} | ||
o.maxWorkers = maxWorkers | ||
}} | ||
} | ||
|
||
// WithIdleTimeout set idle timeout, default is 1s. if idle timeout is less than 1s, it will be set to 1s. | ||
// worker routine will be closed if no task received in idle timeout. if new task received, new worker will be created. | ||
func WithIdleTimeout(idleTimeout time.Duration) Option { | ||
return optionFunc{func(o *options) { | ||
if idleTimeout < time.Second { | ||
idleTimeout = time.Second | ||
} | ||
o.idleTimeout = idleTimeout | ||
}} | ||
} | ||
|
||
// WithMaxTaskSize set max task size, default use runtime.NumCPU(). | ||
func WithMaxTaskSize(maxTaskSize int) Option { | ||
return optionFunc{func(o *options) { | ||
if maxTaskSize <= 0 { | ||
maxTaskSize = runtime.NumCPU() | ||
} | ||
o.maxTaskSize = maxTaskSize | ||
}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package routinepool | ||
|
||
import ( | ||
"runtime" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestOptions(t *testing.T) { | ||
o := defaultOptions() | ||
WithMaxWorkers(10).apply(o) | ||
if o.maxWorkers != 10 { | ||
t.Errorf("WithMaxWorkers want 10, got %d", o.maxWorkers) | ||
} | ||
WithMaxWorkers(0).apply(o) | ||
if o.maxWorkers != runtime.NumCPU() { | ||
t.Errorf("WithMaxWorkers want %d, got %d", runtime.NumCPU(), o.maxWorkers) | ||
} | ||
|
||
WithIdleTimeout(time.Second * 2).apply(o) | ||
if o.idleTimeout != time.Second*2 { | ||
t.Errorf("WithIdleTimeout want %d, got %d", time.Second*2, o.idleTimeout) | ||
} | ||
WithIdleTimeout(time.Millisecond * 100).apply(o) | ||
if o.idleTimeout != time.Second { | ||
t.Errorf("WithIdleTimeout want %d, got %d", time.Second, o.idleTimeout) | ||
} | ||
|
||
WithMaxTaskSize(10).apply(o) | ||
if o.maxTaskSize != 10 { | ||
t.Errorf("WithMaxTaskSize want 10, got %d", o.maxTaskSize) | ||
} | ||
WithMaxTaskSize(0).apply(o) | ||
if o.maxTaskSize != runtime.NumCPU() { | ||
t.Errorf("WithMaxTaskSize want %d, got %d", runtime.NumCPU(), o.maxTaskSize) | ||
} | ||
} |
Oops, something went wrong.