Skip to content
This repository has been archived by the owner on Dec 28, 2020. It is now read-only.

Commit

Permalink
Merge pull request #5 from bredov/patch-1
Browse files Browse the repository at this point in the history
Make channels buffered to prevent goroutines leak.
  • Loading branch information
Burcu Dogan committed Jan 23, 2014
2 parents 3936429 + 05a4458 commit 9f25699
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions coop.go
Expand Up @@ -30,7 +30,7 @@ func At(t time.Time, fn func()) (done <-chan bool) {

// Runs until time in every dur.
func Until(t time.Time, dur time.Duration, fn func()) (done <-chan bool) {
ch := make(chan bool)
ch := make(chan bool, 1)
go untilRecv(ch, t, dur, fn)
return ch
}
Expand All @@ -48,7 +48,7 @@ func untilRecv(ch chan bool, t time.Time, dur time.Duration, fn func()) {

// Runs fn after duration. Similar to time.AfterFunc
func After(duration time.Duration, fn func()) (done <-chan bool) {
ch := make(chan bool)
ch := make(chan bool, 1)
time.AfterFunc(duration, func() {
fn()
ch <- true
Expand All @@ -69,7 +69,7 @@ func Every(dur time.Duration, fn func()) {
// channel if timeout occurs.
// TODO: cancel if timeout occurs
func Timeout(duration time.Duration, fn func()) (done <-chan bool) {
ch := make(chan bool)
ch := make(chan bool, 2)
go func() {
<-time.After(duration)
ch <- false
Expand All @@ -84,15 +84,16 @@ func Timeout(duration time.Duration, fn func()) (done <-chan bool) {
// Starts a job and returns a channel for cancellation signal.
// Once a message is sent to the channel, stops the fn.
func Killable(fn func()) (kill chan<- bool, done <-chan bool) {
ch := make(chan bool)
dch := make(chan bool)
ch := make(chan bool, 2)
dch := make(chan bool, 1)
go func() {
<-dch
ch <- false
}()
go func() {
fn()
ch <- true
dch <- true
}()
return dch, ch
}
Expand All @@ -102,7 +103,7 @@ func All(fns ...func()) (done <-chan bool) {
var wg sync.WaitGroup
wg.Add(len(fns))

ch := make(chan bool)
ch := make(chan bool, 1)
for _, fn := range fns {
go func(f func()) {
f()
Expand All @@ -119,7 +120,7 @@ func All(fns ...func()) (done <-chan bool) {
// Starts to run the given list of fns concurrently,
// at most n fns at a time.
func AllWithThrottle(throttle int, fns ...func()) (done <-chan bool) {
ch := make(chan bool)
ch := make(chan bool, 1)
go func() {
for {
num := throttle
Expand All @@ -142,7 +143,7 @@ func AllWithThrottle(throttle int, fns ...func()) (done <-chan bool) {
func Replicate(n int, fn func()) (done <-chan bool) {
var wg sync.WaitGroup
wg.Add(n)
ch := make(chan bool)
ch := make(chan bool, 1)
for i := 0; i < n; i++ {
go func() {
fn()
Expand Down

0 comments on commit 9f25699

Please sign in to comment.