Skip to content

Commit

Permalink
edit docs
Browse files Browse the repository at this point in the history
remove comments
  • Loading branch information
dc0d committed Oct 10, 2021
1 parent 7c4afe0 commit 48fb0a1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 143 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
```

Expand All @@ -23,42 +23,42 @@ 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() {
// ...
})
```

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

Expand Down
133 changes: 0 additions & 133 deletions worker-pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
// )

0 comments on commit 48fb0a1

Please sign in to comment.