-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
230 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package pool | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"sync" | ||
"time" | ||
) | ||
|
||
var ( | ||
ErrPoolTernimated = errors.New("pool is ternimated") | ||
ErrTimeout = errors.New("timeout") | ||
) | ||
|
||
type Pool struct { | ||
ctx context.Context | ||
cancel context.CancelFunc | ||
|
||
workers int | ||
|
||
tasks chan Task | ||
wait sync.WaitGroup | ||
} | ||
|
||
func NewPool(size int, workers int) *Pool { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
pool := &Pool{ | ||
ctx: ctx, | ||
cancel: cancel, | ||
workers: workers, | ||
tasks: make(chan Task, size), | ||
} | ||
|
||
pool.wait.Add(workers) | ||
|
||
for i := 0; i < workers; i++ { | ||
go pool.consume() | ||
} | ||
|
||
return pool | ||
} | ||
|
||
func (p *Pool) SubmitFn(timeout time.Duration, fn func()) error { | ||
if fn == nil { | ||
return errors.New("fn is nil") | ||
} | ||
|
||
taks := &task{ | ||
fn: fn, | ||
} | ||
return p.Submit(timeout, taks) | ||
} | ||
|
||
func (p *Pool) Submit(timeout time.Duration, task Task) error { | ||
if task == nil { | ||
return errors.New("task is nil") | ||
} | ||
|
||
if p.ctx.Err() != nil { | ||
return ErrPoolTernimated | ||
} | ||
|
||
select { | ||
case p.tasks <- task: | ||
return nil | ||
case <-time.After(timeout): | ||
return ErrTimeout | ||
} | ||
} | ||
|
||
func (p *Pool) consume() { | ||
defer p.wait.Done() | ||
for { | ||
select { | ||
case <-p.ctx.Done(): | ||
return | ||
case t := <-p.tasks: | ||
t.Execute() | ||
} | ||
} | ||
} | ||
|
||
func (p *Pool) Shutdown() { | ||
if err := p.ctx.Err(); err != nil { | ||
return | ||
} | ||
|
||
p.cancel() | ||
p.wait.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package pool | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"sync" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestError(t *testing.T) { | ||
pool := NewPool(0, 1) | ||
|
||
err := pool.SubmitFn(time.Second, nil) | ||
assert.Equal(t, "fn is nil", err.Error()) | ||
|
||
err = pool.Submit(time.Second, nil) | ||
assert.Equal(t, "task is nil", err.Error()) | ||
|
||
// panic should be recovered | ||
err = pool.SubmitFn(time.Second, func() { | ||
panic("foo") | ||
}) | ||
assert.NoError(t, err) | ||
|
||
pool.Shutdown() | ||
pool.Shutdown() // no panic | ||
} | ||
|
||
func TestSubmit(t *testing.T) { | ||
pool := NewPool(5, 1) | ||
wait := sync.WaitGroup{} | ||
for i := 0; i < 5; i++ { | ||
wait.Add(1) | ||
err := pool.SubmitFn(time.Second, func() { | ||
wait.Done() | ||
}) | ||
assert.NoError(t, err) | ||
} | ||
wait.Wait() | ||
} | ||
|
||
func TestSubmitWithTimeout(t *testing.T) { | ||
pool := NewPool(1, 1) | ||
err := pool.SubmitFn(time.Second, func() { | ||
time.Sleep(time.Second * 5) | ||
}) | ||
assert.NoError(t, err) | ||
err = pool.SubmitFn(time.Second, func() { | ||
time.Sleep(time.Second * 5) | ||
}) | ||
assert.NoError(t, err) | ||
err = pool.SubmitFn(time.Second, func() {}) | ||
assert.Equal(t, ErrTimeout, err) | ||
} | ||
|
||
func TestShutdown(t *testing.T) { | ||
pool := NewPool(1, 1) | ||
pool.Shutdown() | ||
err := pool.SubmitFn(time.Second, func() {}) | ||
assert.Equal(t, ErrPoolTernimated, err) | ||
} | ||
|
||
func TestGracefulShutdown(t *testing.T) { | ||
var counter int64 | ||
atomic.StoreInt64(&counter, 0) | ||
|
||
pool := NewPool(100, 100) | ||
|
||
for i := 0; i < 100; i++ { | ||
err := pool.SubmitFn(time.Second, func() { | ||
time.Sleep(time.Second) | ||
atomic.AddInt64(&counter, 1) | ||
}) | ||
assert.NoError(t, err) | ||
} | ||
|
||
pool.Shutdown() | ||
assert.EqualValues(t, 100, counter) // all submitted and scheduled tasks should be executed successfully | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package pool | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
) | ||
|
||
type Task interface { | ||
Execute() | ||
} | ||
|
||
type task struct { | ||
fn func() | ||
} | ||
|
||
func (t *task) Execute() { | ||
defer func() { | ||
if e := recover(); e != nil { | ||
buf := make([]byte, 2048) | ||
n := runtime.Stack(buf, false) | ||
buf = buf[:n] | ||
fmt.Printf("panic recovered: %v\n %s\n", e, buf) | ||
} | ||
}() | ||
t.fn() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters