Created
June 23, 2017 05:58
-
-
Save dtynn/aab5506da831c2efa5106cef38f7047e to your computer and use it in GitHub Desktop.
go concurrency
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
func restore(repos []string) error { | |
errChan := make(chan error, 1) | |
sem := make(chan int, 4) // four jobs at once | |
var wg sync.WaitGroup | |
wg.Add(len(repos)) | |
for _, repo := range repos { | |
sem <- 1 | |
go func() { | |
defer func() { | |
wg.Done() | |
<-sem | |
}() | |
if err := fetch(repo); err != nil { | |
errChan <- err | |
} | |
}() | |
} | |
wg.Wait() | |
close(sem) | |
close(errChan) | |
return <-errChan | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment