Skip to content

Commit 0a29120

Browse files
authored
Expose ants options (#155)
* task/queue: allow to set optional ants.Option's
1 parent bd8ff5e commit 0a29120

3 files changed

Lines changed: 56 additions & 21 deletions

File tree

sync/task/queue/config.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package queue
2+
3+
import (
4+
"runtime"
5+
"time"
6+
)
7+
8+
// Config configuration for task queue.
9+
type Config struct {
10+
// GoroutinePoolSize maximum number of running goroutine instances.
11+
GoroutinePoolSize int `yaml:"goroutinePoolSize" json:"goroutinePoolSize"`
12+
// Size size of queue. If it exhausted Submit returns error.
13+
Size int `yaml:"size" json:"size"`
14+
// MaxIdleTime sets up the interval time of cleaning up goroutines, 0 means never cleanup.
15+
MaxIdleTime time.Duration `yaml:"maxIdleTime" json:"maxIdleTime"`
16+
}
17+
18+
// SetDefaults set zero values to defautls.
19+
func (c *Config) SetDefaults() {
20+
if c.GoroutinePoolSize == 0 {
21+
c.GoroutinePoolSize = runtime.NumCPU() * 200
22+
}
23+
if c.Size == 0 {
24+
c.Size = 2 * 1024 * 1024
25+
}
26+
}
Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,32 @@ import (
88
"github.com/panjf2000/ants/v2"
99
)
1010

11-
// TaskQueue representation of task queue.
12-
type TaskQueue struct {
13-
goPool *ants.Pool
14-
limit int
15-
numParallelRequest int
11+
// Queue representation of task queue.
12+
type Queue struct {
13+
goPool *ants.Pool
14+
limit int
1615

1716
mutex sync.Mutex
1817
queue *list.List
1918
}
2019

21-
// New creates task queue which is processed by number of workers. Number of task is limited by limit.
22-
func New(numWorkers, limit int) (*TaskQueue, error) {
23-
p, err := ants.NewPool(numWorkers, ants.WithPreAlloc(true), ants.WithNonblocking(true))
20+
// New creates task queue which is processed by goroutines.
21+
func New(cfg Config) (*Queue, error) {
22+
if cfg.Size <= 0 {
23+
return nil, fmt.Errorf("invalid value of Size")
24+
}
25+
p, err := ants.NewPool(cfg.GoroutinePoolSize, ants.WithPreAlloc(true), ants.WithExpiryDuration(cfg.MaxIdleTime), ants.WithNonblocking(true))
2426
if err != nil {
2527
return nil, err
2628
}
27-
return &TaskQueue{
28-
queue: list.New(),
29-
goPool: p,
30-
limit: limit,
31-
numParallelRequest: numWorkers,
29+
return &Queue{
30+
queue: list.New(),
31+
goPool: p,
32+
limit: cfg.Size,
3233
}, nil
3334
}
3435

35-
func (q *TaskQueue) appendQueue(tasks []func()) error {
36+
func (q *Queue) appendQueue(tasks []func()) error {
3637
q.mutex.Lock()
3738
defer q.mutex.Unlock()
3839
if q.queue.Len()+len(tasks) > q.limit {
@@ -44,7 +45,7 @@ func (q *TaskQueue) appendQueue(tasks []func()) error {
4445
return nil
4546
}
4647

47-
func (q *TaskQueue) popQueue() func() {
48+
func (q *Queue) popQueue() func() {
4849
q.mutex.Lock()
4950
defer q.mutex.Unlock()
5051
if q.queue.Len() == 0 {
@@ -53,8 +54,8 @@ func (q *TaskQueue) popQueue() func() {
5354
return q.queue.Remove(q.queue.Front()).(func())
5455
}
5556

56-
// Submit appends and execute task by taskQueue.
57-
func (q *TaskQueue) Submit(tasks ...func()) error {
57+
// Submit appends and execute task by Queue.
58+
func (q *Queue) Submit(tasks ...func()) error {
5859
err := q.appendQueue(tasks)
5960
if err != nil {
6061
return err
@@ -72,7 +73,7 @@ func (q *TaskQueue) Submit(tasks ...func()) error {
7273
}
7374

7475
// Release closes queue and release it.
75-
func (q *TaskQueue) Release() {
76+
func (q *Queue) Release() {
7677
q.goPool.Release()
7778
q.mutex.Lock()
7879
defer q.mutex.Unlock()
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@ package queue_test
33
import (
44
"testing"
55

6-
"github.com/plgd-dev/kit/task/queue"
6+
"github.com/plgd-dev/kit/sync/task/queue"
77
"github.com/stretchr/testify/require"
88
)
99

1010
func TestTaskQueue_Submit(t *testing.T) {
11-
_, err := queue.New(-1, 10)
11+
_, err := queue.New(queue.Config{
12+
NumWorkers: -1,
13+
Size: 10,
14+
PreAlloc: true,
15+
})
1216
require.Error(t, err)
13-
q, err := queue.New(1, 2)
17+
q, err := queue.New(queue.Config{
18+
NumWorkers: 1,
19+
Size: 2,
20+
PreAlloc: true,
21+
})
1422
require.NoError(t, err)
1523
err = q.Submit(func() {}, func() {}, func() {})
1624
require.Error(t, err)

0 commit comments

Comments
 (0)