From 48fb0a1030303ccb9d788c4ef7305eb0b1786227 Mon Sep 17 00:00:00 2001 From: Kaveh Shahbazian Date: Sun, 10 Oct 2021 11:35:29 +0200 Subject: [PATCH] edit docs remove comments --- README.md | 20 ++++---- worker-pool.go | 133 ------------------------------------------------- 2 files changed, 10 insertions(+), 143 deletions(-) diff --git a/README.md b/README.md index 25dfad8..cd439ca 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ First, initialize the worker-poll with a mailbox of a certain size: ```go -pool := Init(n) +pool := New(n) defer pool.Stop() ``` @@ -23,7 +23,7 @@ type WorkerPool chan func() Jobs can be sent to the worker-pool in two different manners, blocking and nonblocking. To send a job to the worker-pool and block until it's completed: ```go -pool.Blocking(func() { +pool.Blocking(ctx, func() { // ... }) ``` @@ -31,34 +31,34 @@ pool.Blocking(func() { And to send a job to the worker-pool and then move on: ```go -pool.Nonblocking(func() { +pool.SemiBlocking(ctx, func() { // ... }) ``` -As long as there is an empty space in the mailbox, `Nonblocking` will just queue the job, and moves on. When there are no more empty spaces in the mailbox, `Nonblocking` becomes blocking. +As long as there is an empty space in the mailbox, `SemiBlocking` will just queue the job, and moves on. When there are no more empty spaces in the mailbox, `SemiBlocking` becomes blocking. -A worker-pool by default has one worker. To add workers to the worker-pool: +A worker-pool by default has no workers and they should be added explicitly. To add workers to the worker-pool: ```go -pool.Grow(9) +pool.Grow(ctx, 10) ``` -Now, the worker-pool has `10` workers, one default worker and nine added workers. +Now, the worker-pool has `10` workers. It's possible to add temporary workers to the worker-pool: ```go -pool.Grow(9, WithAbsoluteTimeout(time.Minute * 5)) +pool.Grow(ctx, 9, WithAbsoluteTimeout(time.Minute * 5)) ``` Also, instead of using and absolute timeout, an idle timeout can be used. In this case, added workers will stop, if they are idle for a certain duration: ```go -pool.Grow(9, WithIdleTimeout(time.Minute * 5)) +pool.Grow(ctx, 9, WithIdleTimeout(time.Minute * 5)) ``` -The `Blocking` and `Nonblocking` methods will panic if the worker-pool is stopped - to enforce visibility on job execution. +The `Blocking` and `SemiBlocking` methods will panic if the worker-pool is stopped - to enforce visibility on job execution. ## spool serializes the jobs in single worker mode diff --git a/worker-pool.go b/worker-pool.go index dc1e693..609922f 100644 --- a/worker-pool.go +++ b/worker-pool.go @@ -72,136 +72,3 @@ func (obj defaultExecutor) Received(fn T) { } func (obj defaultExecutor) Stopped() {} - -// import ( -// "log" -// "time" -// ) - -// // New creates a new workerpool. If initialPoolSize is zero, no initial workers will be started. -// // To have more workers, the Grow method should be used. -// func New(mailboxSize MailboxSize, initialPoolSize int, opts ...GrowthOption) WorkerPool { -// if mailboxSize < 0 { -// mailboxSize = 0 -// } - -// var pool WorkerPool = make(chan func(), mailboxSize) -// if initialPoolSize > 0 { -// pool.Grow(initialPoolSize, opts...) -// } - -// return pool -// } - -// func (pool WorkerPool) Stop() { -// close(pool) -// } - -// func (pool WorkerPool) start(options growthOptions) { -// go pool.worker(options) -// } - -// func (pool WorkerPool) worker(options growthOptions) { -// if workerStarted != nil { -// workerStarted(pool) -// } -// if workerStopped != nil { -// defer workerStopped(pool) -// } - -// var ( -// absoluteTimeout = options.absoluteTimeout -// idleTimeout = options.idleTimeout -// stopSignal = options.stopSignal -// ) - -// var absoluteTimeoutSignal, idleTimeoutSignal <-chan time.Time -// if absoluteTimeout > 0 { -// absoluteTimeoutSignal = time.After(absoluteTimeout) -// } - -// var requestCount RequestCount -// for { -// if options.respawnAfter > 0 && options.respawnAfter <= requestCount { -// pool.start(options) -// return -// } - -// if idleTimeout > 0 { -// idleTimeoutSignal = time.After(idleTimeout) -// } - -// select { -// case <-absoluteTimeoutSignal: -// return -// case <-idleTimeoutSignal: -// if options.respawnAfter > 0 { -// pool.start(options) -// } -// return -// case <-stopSignal: -// return -// case callback, ok := <-pool: -// if !ok { -// return -// } -// execCallback(callback) -// requestCount++ -// } -// } -// } - -// func execCallback(callback func()) { -// defer func() { -// if e := recover(); e != nil { -// log.Println(e) // TODO: -// } -// }() - -// callback() -// } - -// var ( -// workerStarted func(pool WorkerPool) -// workerStopped func(pool WorkerPool) -// ) - -// // growth options - -// func WithAbsoluteTimeout(timeout time.Duration) GrowthOption { -// return func(opts growthOptions) growthOptions { opts.absoluteTimeout = timeout; return opts } -// } - -// func WithIdleTimeout(timeout time.Duration) GrowthOption { -// return func(opts growthOptions) growthOptions { opts.idleTimeout = timeout; return opts } -// } - -// func WithStopSignal(stopSignal <-chan struct{}) GrowthOption { -// return func(opts growthOptions) growthOptions { opts.stopSignal = stopSignal; return opts } -// } - -// func WithRespawnAfter(respawnAfter RequestCount) GrowthOption { -// return func(opts growthOptions) growthOptions { opts.respawnAfter = respawnAfter; return opts } -// } - -// type GrowthOption func(growthOptions) growthOptions - -// type growthOptions struct { -// absoluteTimeout time.Duration -// idleTimeout time.Duration -// stopSignal <-chan struct{} -// respawnAfter RequestCount -// } - -// func applyOptions(opts ...GrowthOption) growthOptions { -// var options growthOptions -// for _, fn := range opts { -// options = fn(options) -// } -// return options -// } - -// type ( -// RequestCount int -// MailboxSize int -// )