Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: return the error from Pool.Submit/PoolWithFunc.Invoke accordingly #297

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,12 @@ func (p *Pool) Submit(task func()) error {
if p.IsClosed() {
return ErrPoolClosed
}
if w := p.retrieveWorker(); w != nil {

w, err := p.retrieveWorker()
if w != nil {
w.inputFunc(task)
return nil
}
return ErrPoolOverload
return err
}

// Running returns the number of workers currently running.
Expand Down Expand Up @@ -332,7 +333,7 @@ func (p *Pool) addWaiting(delta int) {
}

// retrieveWorker returns an available worker to run the tasks.
func (p *Pool) retrieveWorker() (w worker) {
func (p *Pool) retrieveWorker() (w worker, err error) {
p.lock.Lock()

retry:
Expand All @@ -354,7 +355,7 @@ retry:
// Bail out early if it's in nonblocking mode or the number of pending callers reaches the maximum limit value.
if p.options.Nonblocking || (p.options.MaxBlockingTasks != 0 && p.Waiting() >= p.options.MaxBlockingTasks) {
p.lock.Unlock()
return
return nil, ErrPoolOverload
}

// Otherwise, we'll have to keep them blocked and wait for at least one worker to be put back into pool.
Expand All @@ -364,7 +365,7 @@ retry:

if p.IsClosed() {
p.lock.Unlock()
return
return nil, ErrPoolClosed
}

goto retry
Expand Down
13 changes: 7 additions & 6 deletions pool_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,12 @@ func (p *PoolWithFunc) Invoke(args interface{}) error {
if p.IsClosed() {
return ErrPoolClosed
}
if w := p.retrieveWorker(); w != nil {

w, err := p.retrieveWorker()
if w != nil {
w.inputParam(args)
return nil
}
return ErrPoolOverload
return err
}

// Running returns the number of workers currently running.
Expand Down Expand Up @@ -338,7 +339,7 @@ func (p *PoolWithFunc) addWaiting(delta int) {
}

// retrieveWorker returns an available worker to run the tasks.
func (p *PoolWithFunc) retrieveWorker() (w worker) {
func (p *PoolWithFunc) retrieveWorker() (w worker, err error) {
p.lock.Lock()

retry:
Expand All @@ -360,7 +361,7 @@ retry:
// Bail out early if it's in nonblocking mode or the number of pending callers reaches the maximum limit value.
if p.options.Nonblocking || (p.options.MaxBlockingTasks != 0 && p.Waiting() >= p.options.MaxBlockingTasks) {
p.lock.Unlock()
return
return nil, ErrPoolOverload
}

// Otherwise, we'll have to keep them blocked and wait for at least one worker to be put back into pool.
Expand All @@ -370,7 +371,7 @@ retry:

if p.IsClosed() {
p.lock.Unlock()
return
return nil, ErrPoolClosed
}

goto retry
Expand Down