-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from alitto/issue/22
Prevent deadlock when purging idle worker during task submission
- Loading branch information
Showing
12 changed files
with
145 additions
and
80 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
test: | ||
go test -race -v ./ | ||
|
||
coverage: | ||
go test -race -v -coverprofile=coverage.out -covermode=atomic ./ |
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,6 +1,6 @@ | ||
module github.com/alitto/pond/examples/dynamic_size | ||
|
||
go 1.18 | ||
go 1.19 | ||
|
||
require ( | ||
github.com/alitto/pond v1.7.1 | ||
|
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,6 +1,6 @@ | ||
module github.com/alitto/pond/examples/fixed_size | ||
|
||
go 1.18 | ||
go 1.19 | ||
|
||
require ( | ||
github.com/alitto/pond v1.7.1 | ||
|
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,6 +1,6 @@ | ||
module github.com/alitto/pond/examples/group_context | ||
|
||
go 1.18 | ||
go 1.19 | ||
|
||
require ( | ||
github.com/alitto/pond v1.7.1 | ||
|
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,6 +1,6 @@ | ||
module github.com/alitto/pond/examples/pool_context | ||
|
||
go 1.18 | ||
go 1.19 | ||
|
||
require github.com/alitto/pond v1.7.1 | ||
|
||
|
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,6 +1,6 @@ | ||
module github.com/alitto/pond/examples/fixed_size | ||
|
||
go 1.18 | ||
go 1.19 | ||
|
||
require ( | ||
github.com/alitto/pond v1.7.1 | ||
|
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,6 +1,6 @@ | ||
module github.com/alitto/pond/examples/task_group | ||
|
||
go 1.18 | ||
go 1.19 | ||
|
||
require ( | ||
github.com/alitto/pond v1.7.1 | ||
|
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.18 | ||
go 1.19 |
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,35 @@ | ||
package pond | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
) | ||
|
||
// worker represents a worker goroutine | ||
func worker(context context.Context, waitGroup *sync.WaitGroup, firstTask func(), tasks <-chan func(), taskExecutor func(func(), bool)) { | ||
|
||
// If provided, execute the first task immediately, before listening to the tasks channel | ||
if firstTask != nil { | ||
taskExecutor(firstTask, true) | ||
} | ||
|
||
defer func() { | ||
waitGroup.Done() | ||
}() | ||
|
||
for { | ||
select { | ||
case <-context.Done(): | ||
// Pool context was cancelled, exit | ||
return | ||
case task, ok := <-tasks: | ||
if task == nil || !ok { | ||
// We have received a signal to exit | ||
return | ||
} | ||
|
||
// We have received a task, execute it | ||
taskExecutor(task, false) | ||
} | ||
} | ||
} |