Auto stash before merge of "develop" and "origin/develop"

This commit is contained in:
Andy Pan 2018-05-24 23:43:34 +08:00
parent aaa7bad491
commit 5326374a22
6 changed files with 18 additions and 20 deletions

View File

@ -39,9 +39,9 @@ const (
// Init a instance pool when importing ants // Init a instance pool when importing ants
var defaultPool, _ = NewPool(DefaultPoolSize) var defaultPool, _ = NewPool(DefaultPoolSize)
// Push submit a task to pool // Submit submit a task to pool
func Push(task f) error { func Submit(task f) error {
return defaultPool.Push(task) return defaultPool.Submit(task)
} }
// Running returns the number of the currently running goroutines // Running returns the number of the currently running goroutines

View File

@ -44,8 +44,9 @@ const (
const RunTimes = 10000000 const RunTimes = 10000000
const loop = 10 const loop = 10
func demoFunc() { func demoFunc() error {
time.Sleep(loop * time.Millisecond) time.Sleep(loop * time.Millisecond)
return nil
} }
func demoPoolFunc(args interface{}) error { func demoPoolFunc(args interface{}) error {
@ -101,10 +102,9 @@ func BenchmarkGoroutine(b *testing.B) {
} }
func BenchmarkAntsPool(b *testing.B) { func BenchmarkAntsPool(b *testing.B) {
b.N = 3
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
for j := 0; j < RunTimes; j++ { for j := 0; j < RunTimes; j++ {
ants.Push(demoFunc) ants.Submit(demoFunc)
} }
b.Logf("running goroutines: %d", ants.Running()) b.Logf("running goroutines: %d", ants.Running())
} }

View File

@ -36,9 +36,10 @@ func TestDefaultPool(t *testing.T) {
var wg sync.WaitGroup var wg sync.WaitGroup
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
wg.Add(1) wg.Add(1)
ants.Push(func() { ants.Submit(func() error {
demoFunc() demoFunc()
wg.Done() wg.Done()
return nil
}) })
} }
wg.Wait() wg.Wait()
@ -111,7 +112,7 @@ func TestNoPool(t *testing.T) {
// var wg sync.WaitGroup // var wg sync.WaitGroup
// for i := 0; i < n; i++ { // for i := 0; i < n; i++ {
// wg.Add(1) // wg.Add(1)
// p.Push(func() { // p.Submit(func() {
// demoFunc() // demoFunc()
// //demoFunc() // //demoFunc()
// wg.Done() // wg.Done()

View File

@ -45,7 +45,7 @@ func myFunc(i interface{}) error {
// // submit all your tasks to ants pool // // submit all your tasks to ants pool
// for i := 0; i < runTimes; i++ { // for i := 0; i < runTimes; i++ {
// wg.Add(1) // wg.Add(1)
// ants.Push(func() { // ants.Submit(func() {
// myFunc() // myFunc()
// wg.Done() // wg.Done()
// }) // })

12
pool.go
View File

@ -30,9 +30,9 @@ import (
type sig struct{} type sig struct{}
type f func() type f func() error
// Pool accept the tasks from client,it will limit the total // Pool accept the tasks from client,it limits the total
// of goroutines to a given number by recycling goroutines. // of goroutines to a given number by recycling goroutines.
type Pool struct { type Pool struct {
// capacity of the pool. // capacity of the pool.
@ -48,12 +48,10 @@ type Pool struct {
// workers is a slice that store the available workers. // workers is a slice that store the available workers.
workers []*Worker workers []*Worker
// workerPool is a pool that saves a set of temporary objects.
workerPool sync.Pool
// release is used to notice the pool to closed itself. // release is used to notice the pool to closed itself.
release chan sig release chan sig
// lock for synchronous operation
lock sync.Mutex lock sync.Mutex
once sync.Once once sync.Once
@ -75,8 +73,8 @@ func NewPool(size int) (*Pool, error) {
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
// Push submit a task to pool // Submit submit a task to pool
func (p *Pool) Push(task f) error { func (p *Pool) Submit(task f) error {
if len(p.release) > 0 { if len(p.release) > 0 {
return ErrPoolClosed return ErrPoolClosed
} }

View File

@ -30,7 +30,7 @@ import (
type pf func(interface{}) error type pf func(interface{}) error
// PoolWithFunc accept the tasks from client,it will limit the total // PoolWithFunc accept the tasks from client,it limits the total
// of goroutines to a given number by recycling goroutines. // of goroutines to a given number by recycling goroutines.
type PoolWithFunc struct { type PoolWithFunc struct {
// capacity of the pool. // capacity of the pool.
@ -46,14 +46,13 @@ type PoolWithFunc struct {
// workers is a slice that store the available workers. // workers is a slice that store the available workers.
workers []*WorkerWithFunc workers []*WorkerWithFunc
// workerPool is a pool that saves a set of temporary objects.
//workerPool sync.Pool
// release is used to notice the pool to closed itself. // release is used to notice the pool to closed itself.
release chan sig release chan sig
// lock for synchronous operation
lock sync.Mutex lock sync.Mutex
// pf is the function for processing tasks
poolFunc pf poolFunc pf
once sync.Once once sync.Once