forked from TrueCloudLab/frostfs-node
[#901] util: implement Release
for PseudWorkerPool
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
parent
616013cb8a
commit
0beaed2ef4
3 changed files with 87 additions and 5 deletions
|
@ -1,5 +1,10 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"github.com/panjf2000/ants/v2"
|
||||
"go.uber.org/atomic"
|
||||
)
|
||||
|
||||
// WorkerPool represents the tool for control
|
||||
// the execution of go-routine pool.
|
||||
type WorkerPool interface {
|
||||
|
@ -9,16 +14,41 @@ type WorkerPool interface {
|
|||
// Implementation must return any error encountered
|
||||
// that prevented the function from being queued.
|
||||
Submit(func()) error
|
||||
|
||||
// Release releases worker pool resources. All `Submit` calls will
|
||||
// finish with ErrPoolClosed. It doesn't wait until all submitted
|
||||
// functions have returned so synchronization must be achieved
|
||||
// via other means (e.g. sync.WaitGroup).
|
||||
Release()
|
||||
}
|
||||
|
||||
// PseudoWorkerPool represents pseudo worker pool which executes submitted job immediately in the caller's routine..
|
||||
type PseudoWorkerPool struct{}
|
||||
// pseudoWorkerPool represents pseudo worker pool which executes submitted job immediately in the caller's routine.
|
||||
type pseudoWorkerPool struct {
|
||||
closed atomic.Bool
|
||||
}
|
||||
|
||||
// ErrPoolClosed is returned when submitting task to a closed pool.
|
||||
var ErrPoolClosed = ants.ErrPoolClosed
|
||||
|
||||
// NewPseudoWorkerPool returns new instance of a synchronous worker pool.
|
||||
func NewPseudoWorkerPool() WorkerPool {
|
||||
return &pseudoWorkerPool{}
|
||||
}
|
||||
|
||||
// Submit executes passed function immediately.
|
||||
//
|
||||
// Always returns nil.
|
||||
func (PseudoWorkerPool) Submit(fn func()) error {
|
||||
func (p *pseudoWorkerPool) Submit(fn func()) error {
|
||||
if p.closed.Load() {
|
||||
return ErrPoolClosed
|
||||
}
|
||||
|
||||
fn()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Release implements WorkerPool interface.
|
||||
func (p *pseudoWorkerPool) Release() {
|
||||
p.closed.Store(true)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue