@@ -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 ()
0 commit comments