golint the codes

This commit is contained in:
Andy Pan 2018-05-20 21:41:32 +08:00
parent 47e2b9efaa
commit f0c48f295b
2 changed files with 13 additions and 11 deletions

14
ants.go
View File

@ -22,15 +22,15 @@ package ants
import "github.com/iris-contrib/errors" import "github.com/iris-contrib/errors"
const ( const (
// Default capacity for a default goroutine pool // DefaultPoolSize is the default capacity for a default goroutine pool
DEFAULT_POOL_SIZE = 50000 DefaultPoolSize = 50000
// Interval time to clean up goroutines // DefaultCleanIntervalTime is the interval time to clean up goroutines
DEFAULT_CLEAN_INTERVAL_TIME = 30 DefaultCleanIntervalTime = 30
) )
// Init a instance pool when importing ants // Init a instance pool when importing ants
var defaultPool, _ = NewPool(DEFAULT_POOL_SIZE) var defaultPool, _ = NewPool(DefaultPoolSize)
// Push submit a task to pool // Push submit a task to pool
func Push(task f) error { func Push(task f) error {
@ -59,6 +59,6 @@ func Release() {
// Errors for the Ants API // Errors for the Ants API
var ( var (
PoolSizeInvalidError = errors.New("invalid size for pool") ErrPoolSizeInvalid = errors.New("invalid size for pool")
PoolClosedError = errors.New("this pool has been closed") ErrPoolClosed = errors.New("this pool has been closed")
) )

10
pool.go
View File

@ -16,6 +16,7 @@
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package ants package ants
import ( import (
@ -56,9 +57,10 @@ type Pool struct {
closed int32 closed int32
} }
// NewPool generates a instance of ants pool
func NewPool(size int) (*Pool, error) { func NewPool(size int) (*Pool, error) {
if size <= 0 { if size <= 0 {
return nil, PoolSizeInvalidError return nil, ErrPoolSizeInvalid
} }
p := &Pool{ p := &Pool{
capacity: int32(size), capacity: int32(size),
@ -75,7 +77,7 @@ func NewPool(size int) (*Pool, error) {
// scanAndClean is a goroutine who will periodically clean up // scanAndClean is a goroutine who will periodically clean up
// after it is noticed that this pool is closed. // after it is noticed that this pool is closed.
func (p *Pool) scanAndClean() { func (p *Pool) scanAndClean() {
ticker := time.NewTicker(DEFAULT_CLEAN_INTERVAL_TIME * time.Second) ticker := time.NewTicker(DefaultCleanIntervalTime * time.Second)
go func() { go func() {
ticker.Stop() ticker.Stop()
for range ticker.C { for range ticker.C {
@ -93,7 +95,7 @@ func (p *Pool) scanAndClean() {
// Push submit a task to pool // Push submit a task to pool
func (p *Pool) Push(task f) error { func (p *Pool) Push(task f) error {
if atomic.LoadInt32(&p.closed) == 1 { if atomic.LoadInt32(&p.closed) == 1 {
return PoolClosedError return ErrPoolClosed
} }
w := p.getWorker() w := p.getWorker()
w.sendTask(task) w.sendTask(task)
@ -124,7 +126,7 @@ func (p *Pool) Release() error {
return nil return nil
} }
// Resize change the capacity of this pool // ReSize change the capacity of this pool
func (p *Pool) ReSize(size int) { func (p *Pool) ReSize(size int) {
atomic.StoreInt32(&p.capacity, int32(size)) atomic.StoreInt32(&p.capacity, int32(size))
} }