Commit e7888cff authored by watermelon's avatar watermelon

opt: impl simple task poll

parent 2a672f6b
...@@ -13,6 +13,7 @@ const ( ...@@ -13,6 +13,7 @@ const (
// Task Pool Options // Task Pool Options
///////////////////////////////////////// /////////////////////////////////////////
// TaskPoolOption is optional settings for task pool
type TaskPoolOptions struct { type TaskPoolOptions struct {
tQLen int // task queue length. buffer size per queue tQLen int // task queue length. buffer size per queue
tQNumber int // task queue number. number of queue tQNumber int // task queue number. number of queue
...@@ -39,21 +40,21 @@ func (o *TaskPoolOptions) validate() { ...@@ -39,21 +40,21 @@ func (o *TaskPoolOptions) validate() {
type TaskPoolOption func(*TaskPoolOptions) type TaskPoolOption func(*TaskPoolOptions)
// @size is the task queue pool size // WithTaskPoolTaskQueueLength set @size of the task queue pool size
func WithTaskPoolTaskPoolSize(size int) TaskPoolOption { func WithTaskPoolTaskPoolSize(size int) TaskPoolOption {
return func(o *TaskPoolOptions) { return func(o *TaskPoolOptions) {
o.tQPoolSize = size o.tQPoolSize = size
} }
} }
// @length is the task queue length // WithTaskPoolTaskQueueLength set @length of the task queue length
func WithTaskPoolTaskQueueLength(length int) TaskPoolOption { func WithTaskPoolTaskQueueLength(length int) TaskPoolOption {
return func(o *TaskPoolOptions) { return func(o *TaskPoolOptions) {
o.tQLen = length o.tQLen = length
} }
} }
// @number is the task queue number // WithTaskPoolTaskQueueNumber set @number of the task queue number
func WithTaskPoolTaskQueueNumber(number int) TaskPoolOption { func WithTaskPoolTaskQueueNumber(number int) TaskPoolOption {
return func(o *TaskPoolOptions) { return func(o *TaskPoolOptions) {
o.tQNumber = number o.tQNumber = number
......
...@@ -35,6 +35,7 @@ import ( ...@@ -35,6 +35,7 @@ import (
type task func() type task func()
// GenericTaskPool represents an generic task pool.
type GenericTaskPool interface { type GenericTaskPool interface {
// AddTask wait idle worker add task // AddTask wait idle worker add task
AddTask(t task) bool AddTask(t task) bool
...@@ -64,7 +65,7 @@ type TaskPool struct { ...@@ -64,7 +65,7 @@ type TaskPool struct {
done chan struct{} done chan struct{}
} }
// build a task pool // NewTaskPool build a task pool
func NewTaskPool(opts ...TaskPoolOption) GenericTaskPool { func NewTaskPool(opts ...TaskPoolOption) GenericTaskPool {
var tOpts TaskPoolOptions var tOpts TaskPoolOptions
for _, opt := range opts { for _, opt := range opts {
...@@ -226,6 +227,7 @@ type taskPoolSimple struct { ...@@ -226,6 +227,7 @@ type taskPoolSimple struct {
sem chan struct{} sem chan struct{}
} }
// NewTaskPoolSimple build a simple task pool
func NewTaskPoolSimple(size int) GenericTaskPool { func NewTaskPoolSimple(size int) GenericTaskPool {
if size < 1 { if size < 1 {
size = runtime.NumCPU() * 100 size = runtime.NumCPU() * 100
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment