-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: renxiangyu <[email protected]>
- Loading branch information
renxiangyu
committed
Dec 5, 2023
1 parent
88a3efe
commit efc8fc9
Showing
3 changed files
with
235 additions
and
29 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
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,57 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
) | ||
|
||
type GoroutinePool struct { | ||
pool chan int | ||
funcChan chan RoutineFunc | ||
waitGroup *sync.WaitGroup | ||
} | ||
|
||
type RoutineFunc struct { | ||
f interface{} | ||
args []interface{} | ||
} | ||
|
||
func NewGoroutinePool(size int) *GoroutinePool { | ||
return &GoroutinePool{ | ||
pool: make(chan int, size), | ||
funcChan: make(chan RoutineFunc, size), | ||
waitGroup: &sync.WaitGroup{}, | ||
} | ||
} | ||
|
||
func (g *GoroutinePool) Submit(f interface{}, args ...interface{}) { | ||
g.funcChan <- RoutineFunc{f: f, args: args} | ||
g.pool <- 1 | ||
g.waitGroup.Add(1) | ||
|
||
go func() { | ||
task := <-g.funcChan | ||
switch f := task.f.(type) { | ||
case func(): | ||
f() | ||
case func(args ...interface{}): | ||
f(task.args...) | ||
default: | ||
fmt.Println("Invalid task type") | ||
} | ||
defer g.Done() | ||
}() | ||
} | ||
|
||
func (g *GoroutinePool) Wait() { | ||
g.waitGroup.Wait() | ||
} | ||
|
||
func (g *GoroutinePool) Done() { | ||
<-g.pool | ||
g.waitGroup.Done() | ||
} | ||
|
||
func (g *GoroutinePool) Shutdown() { | ||
close(g.pool) | ||
} |
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,126 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestGoroutinePool_Submit(t *testing.T) { | ||
type fields struct { | ||
pool chan int | ||
funcChan chan RoutineFunc | ||
waitGroup *sync.WaitGroup | ||
} | ||
type args struct { | ||
f interface{} | ||
args []interface{} | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args []args | ||
}{ | ||
// func has no parameters | ||
{"Test1", fields{ | ||
pool: make(chan int, 5), | ||
funcChan: make(chan RoutineFunc, 5), | ||
waitGroup: &sync.WaitGroup{}, | ||
}, []args{ | ||
{ | ||
f: func() { | ||
for i := 0; i < 10; i++ { | ||
fmt.Println(i) | ||
time.Sleep(1000) | ||
} | ||
}, | ||
args: nil, | ||
}, { | ||
f: func() { | ||
for i := 10; i < 20; i++ { | ||
fmt.Println(i) | ||
time.Sleep(1000) | ||
} | ||
}, | ||
args: nil, | ||
}, | ||
}}, | ||
// func has parameters | ||
{"Test2", fields{ | ||
pool: make(chan int, 5), | ||
funcChan: make(chan RoutineFunc, 5), | ||
waitGroup: &sync.WaitGroup{}, | ||
}, []args{ | ||
{ | ||
f: func(args ...interface{}) { | ||
for _, arg := range args { | ||
fmt.Println(arg) | ||
time.Sleep(1000) | ||
} | ||
}, | ||
args: []interface{}{"a", "b", "c", "d"}, | ||
}, { | ||
f: func(args ...interface{}) { | ||
for _, arg := range args { | ||
fmt.Println(arg) | ||
time.Sleep(1000) | ||
} | ||
}, | ||
args: []interface{}{"e", "f", "g", "h"}, | ||
}, | ||
}}, | ||
// the thread capacity is 1 | ||
{"Test3", fields{ | ||
pool: make(chan int, 1), | ||
funcChan: make(chan RoutineFunc, 1), | ||
waitGroup: &sync.WaitGroup{}, | ||
}, []args{ | ||
{ | ||
f: func() { | ||
for i := 0; i < 10; i++ { | ||
fmt.Println(i) | ||
time.Sleep(1000) | ||
} | ||
}, | ||
args: nil, | ||
}, { | ||
f: func() { | ||
for i := 10; i < 20; i++ { | ||
fmt.Println(i) | ||
time.Sleep(1000) | ||
} | ||
}, | ||
args: nil, | ||
}, | ||
}}, | ||
//incorrect func parameter | ||
{"Test4", fields{ | ||
pool: make(chan int, 5), | ||
funcChan: make(chan RoutineFunc, 5), | ||
waitGroup: &sync.WaitGroup{}, | ||
}, []args{ | ||
{ | ||
f: func(a int) { | ||
fmt.Println(a) | ||
}, | ||
args: []interface{}{"hello"}, | ||
}, | ||
}}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
g := &GoroutinePool{ | ||
pool: tt.fields.pool, | ||
funcChan: tt.fields.funcChan, | ||
waitGroup: tt.fields.waitGroup, | ||
} | ||
for _, arg := range tt.args { | ||
g.Submit(arg.f, arg.args...) | ||
} | ||
g.Wait() | ||
g.Shutdown() | ||
fmt.Println("success!") | ||
}) | ||
} | ||
} |