From 9af581c8674229d2deb0cff7bb7d4cbf4ca8ce71 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sat, 4 Aug 2018 09:29:53 +0800 Subject: [PATCH] optimization for default pool --- ants.go | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/ants.go b/ants.go index 3191b1f..6b3d51c 100644 --- a/ants.go +++ b/ants.go @@ -36,7 +36,26 @@ const ( ) // Init a instance pool when importing ants -var defaultAntsPool, _ = NewPool(DefaultAntsPoolSize) +var ( + defaultAntsPool *Pool + err error +) + +func init() { + defaultAntsPool, err = NewPool(DefaultAntsPoolSize) + if err != nil { + panic(err) + } +} + +// Release Closed the default pool +func Release() { + defaultAntsPool.Release() + defaultAntsPool, err = NewPool(DefaultAntsPoolSize) + if err != nil { + panic(err) + } +} // Submit submit a task to pool func Submit(task f) error { @@ -58,12 +77,6 @@ func Free() int { return defaultAntsPool.Free() } -// Release Closed the default pool -func Release() { - defaultAntsPool.Release() - -} - // Errors for the Ants API var ( ErrInvalidPoolSize = errors.New("invalid size for pool")