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
5 changed files
with
122 additions
and
24 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,71 @@ | ||
package pond | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
) | ||
|
||
type TaskGroup = Group[func()] | ||
|
||
type TaskGroupWithContext = Group[func() error] | ||
|
||
// Group represents a group of related tasks | ||
type Group[T func() | func() error] struct { | ||
pool *WorkerPool | ||
waitGroup sync.WaitGroup | ||
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 *Group[T]) Submit(task T) { | ||
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: | ||
} | ||
} | ||
|
||
switch any(task).(type) { | ||
case func(): | ||
any(task).(func())() | ||
case func() error: | ||
// don't actually ignore errors | ||
err := any(task).(func() error)() | ||
if err != nil { | ||
fmt.Printf("Error not nil %v\n", err) | ||
g.errOnce.Do(func() { | ||
g.err = err | ||
if g.cancel != nil { | ||
g.cancel() | ||
} | ||
}) | ||
} else { | ||
fmt.Printf("Error is nil %v\n", err) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
// Wait waits until all the tasks in this group have completed | ||
func (g *Group[T]) Wait() error { | ||
|
||
// Wait for all tasks to complete | ||
g.waitGroup.Wait() | ||
|
||
// If context was provided, cancel it to signal all running tasks to stop | ||
if g.cancel != nil { | ||
g.cancel() | ||
} | ||
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
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