Browse Source

feat: goroutine pool

master
OhYee 2 years ago
parent
commit
9e3d000a14
Signed by: OhYee
GPG Key ID: 5A9E1F63ED274FBB
  1. 44
      utils/goroutine_pool/pool.go

44
utils/goroutine_pool/pool.go

@ -0,0 +1,44 @@ @@ -0,0 +1,44 @@
package pool
// Pool is goroutine pool
type Pool struct {
cap int8
running int8
channel chan Func
}
// Func is gorouting function
type Func func()
// New returns a gorouting pool
func New(cap int) *Pool {
p := &Pool{
channel: make(chan Func, cap),
}
for i := 0; i < cap; i++ {
go p.Thread()
}
return p
}
// Thread is the backend threading
func (p *Pool) Thread() {
for {
select {
case f := <-p.channel:
f()
}
}
}
// Do a threading
func (p *Pool) Do(f Func) {
p.channel <- f
}
var pool = New(32)
// Do a threading
func Do(f Func) {
pool.Do(f)
}
Loading…
Cancel
Save