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
6 changed files
with
214 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module github.com/alitto/pond | ||
|
||
go 1.17 | ||
go 1.18 |
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 pond | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
) | ||
|
||
// TaskGroup represents a group of related tasks | ||
type TaskGroup struct { | ||
pool *WorkerPool | ||
waitGroup sync.WaitGroup | ||
} | ||
|
||
// Submit adds a task to this group and sends it to the worker pool to be executed | ||
func (g *TaskGroup) Submit(task func()) { | ||
g.waitGroup.Add(1) | ||
|
||
g.pool.Submit(func() { | ||
defer g.waitGroup.Done() | ||
|
||
task() | ||
}) | ||
} | ||
|
||
// Wait waits until all the tasks in this group have completed | ||
func (g *TaskGroup) Wait() { | ||
|
||
// Wait for all tasks to complete | ||
g.waitGroup.Wait() | ||
} | ||
|
||
// TaskGroup represents a group of related tasks associated to a context | ||
type TaskGroupWithContext struct { | ||
TaskGroup | ||
ctx context.Context | ||
cancel context.CancelFunc | ||
errOnce sync.Once | ||
err error | ||
} | ||
|
||
// Submit adds a task to this group and sends it to the worker pool to be executed | ||
func (g *TaskGroupWithContext) Submit(task func() error) { | ||
g.waitGroup.Add(1) | ||
|
||
g.pool.Submit(func() { | ||
defer g.waitGroup.Done() | ||
|
||
// If context has already been cancelled, skip task execution | ||
if g.ctx != nil { | ||
select { | ||
case <-g.ctx.Done(): | ||
return | ||
default: | ||
} | ||
} | ||
|
||
// don't actually ignore errors | ||
err := task() | ||
if err != nil { | ||
g.errOnce.Do(func() { | ||
g.err = err | ||
if g.cancel != nil { | ||
g.cancel() | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
// Wait blocks until either all the tasks submitted to this group have completed, | ||
// one of them returned a non-nil error or the context associated to this group | ||
// was canceled. | ||
func (g *TaskGroupWithContext) Wait() error { | ||
|
||
// Wait for all tasks to complete | ||
tasksCompleted := make(chan struct{}) | ||
go func() { | ||
g.waitGroup.Wait() | ||
tasksCompleted <- struct{}{} | ||
}() | ||
|
||
select { | ||
case <-tasksCompleted: | ||
// If context was provided, cancel it to signal all running tasks to stop | ||
g.cancel() | ||
case <-g.ctx.Done(): | ||
} | ||
|
||
return g.err | ||
} |
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,107 @@ | ||
package pond_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
"github.com/alitto/pond" | ||
) | ||
|
||
func TestGroupSubmit(t *testing.T) { | ||
|
||
pool := pond.New(5, 1000) | ||
assertEqual(t, 0, pool.RunningWorkers()) | ||
|
||
// Submit groups of tasks | ||
var doneCount, taskCount int32 | ||
var groups []*pond.TaskGroup | ||
for i := 0; i < 5; i++ { | ||
group := pool.Group() | ||
for j := 0; j < i+5; j++ { | ||
group.Submit(func() { | ||
time.Sleep(1 * time.Millisecond) | ||
atomic.AddInt32(&doneCount, 1) | ||
}) | ||
taskCount++ | ||
} | ||
groups = append(groups, group) | ||
} | ||
|
||
// Wait for all groups to complete | ||
for _, group := range groups { | ||
group.Wait() | ||
} | ||
|
||
assertEqual(t, int32(taskCount), atomic.LoadInt32(&doneCount)) | ||
} | ||
|
||
func TestGroupContext(t *testing.T) { | ||
|
||
pool := pond.New(3, 100) | ||
assertEqual(t, 0, pool.RunningWorkers()) | ||
|
||
// Submit a group of tasks | ||
var doneCount, startedCount int32 | ||
group, ctx := pool.GroupContext(context.Background()) | ||
for i := 0; i < 10; i++ { | ||
group.Submit(func() error { | ||
atomic.AddInt32(&startedCount, 1) | ||
|
||
select { | ||
case <-time.After(5 * time.Millisecond): | ||
atomic.AddInt32(&doneCount, 1) | ||
case <-ctx.Done(): | ||
} | ||
|
||
return nil | ||
}) | ||
} | ||
|
||
err := group.Wait() | ||
assertEqual(t, nil, err) | ||
assertEqual(t, int32(10), atomic.LoadInt32(&startedCount)) | ||
assertEqual(t, int32(10), atomic.LoadInt32(&doneCount)) | ||
} | ||
|
||
func TestGroupContextWithError(t *testing.T) { | ||
|
||
pool := pond.New(1, 100) | ||
assertEqual(t, 0, pool.RunningWorkers()) | ||
|
||
expectedErr := errors.New("Something went wrong") | ||
|
||
// Submit a group of tasks | ||
var doneCount, startedCount int32 | ||
group, ctx := pool.GroupContext(context.Background()) | ||
for i := 0; i < 10; i++ { | ||
n := i | ||
group.Submit(func() error { | ||
atomic.AddInt32(&startedCount, 1) | ||
|
||
// Task number 5 fails | ||
if n == 4 { | ||
time.Sleep(10 * time.Millisecond) | ||
return expectedErr | ||
} | ||
|
||
select { | ||
case <-time.After(5 * time.Millisecond): | ||
atomic.AddInt32(&doneCount, 1) | ||
case <-ctx.Done(): | ||
} | ||
|
||
return nil | ||
}) | ||
} | ||
|
||
err := group.Wait() | ||
assertEqual(t, expectedErr, err) | ||
|
||
pool.StopAndWait() | ||
|
||
assertEqual(t, int32(5), atomic.LoadInt32(&startedCount)) | ||
assertEqual(t, int32(4), atomic.LoadInt32(&doneCount)) | ||
} |
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