Skip to content

Commit

Permalink
Added the Add function
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterclaerhout committed Sep 29, 2019
1 parent bb46bc3 commit 21a5523
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 16 deletions.
76 changes: 62 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package main

import (
"fmt"
"net/http"

"github.com/pieterclaerhout/go-waitgroup"
)

Expand All @@ -31,23 +33,69 @@ func main() {

wg := waitgroup.NewWaitGroup(3)

for _, url := range urls {
wg.BlockAdd()
go func(url string) {
defer wg.Done()
fmt.Println("%s: checking", url)
res, err := http.Get(url)
if err != nil {
fmt.Println("Error: %v")
} else {
defer res.Body.Close()
fmt.Println("%s: result: %v", err)
}
}(url)
}
for _, url := range urls {
wg.BlockAdd()
go func(url string) {
defer wg.Done()
fmt.Printf("%s: checking\n", url)
res, err := http.Get(url)
if err != nil {
fmt.Println("Error: %v")
} else {
defer res.Body.Close()
fmt.Printf("%s: result: %v\n", url, err)
}
}(url)
}

wg.Wait()
fmt.Println("Finished")

}
```

There is also a way to use function closures to make it even more readable:

```go
package main

import (
"fmt"
"net/http"

"github.com/pieterclaerhout/go-waitgroup"
)

func main() {

urls := []string{
"https://www.easyjet.com/",
"https://www.skyscanner.de/",
"https://www.ryanair.com",
"https://wizzair.com/",
"https://www.swiss.com/",
}

wg := waitgroup.NewWaitGroup(3)

for _, url := range urls {

urlToCheck := url
wg.Add(func() {
fmt.Printf("%s: checking\n", urlToCheck)
res, err := http.Get(urlToCheck)
if err != nil {
fmt.Println("Error: %v")
} else {
defer res.Body.Close()
fmt.Printf("%s: result: %v\n", urlToCheck, err)
}
})

}

wg.Wait()
fmt.Println("Finished")

}
```
41 changes: 41 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"fmt"
"net/http"

"github.com/pieterclaerhout/go-waitgroup"
)

func main() {

urls := []string{
"https://www.easyjet.com/",
"https://www.skyscanner.de/",
"https://www.ryanair.com",
"https://wizzair.com/",
"https://www.swiss.com/",
}

wg := waitgroup.NewWaitGroup(3)

for _, url := range urls {

urlToCheck := url
wg.Add(func() {
fmt.Printf("%s: checking\n", urlToCheck)
res, err := http.Get(urlToCheck)
if err != nil {
fmt.Println("Error: %v")
} else {
defer res.Body.Close()
fmt.Printf("%s: result: %v\n", urlToCheck, err)
}
})

}

wg.Wait()
fmt.Println("Finished")

}
11 changes: 9 additions & 2 deletions waitgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,20 @@ func NewWaitGroup(size int) *WaitGroup {
return wg
}

// Add add the function close to the waitgroup
func (wg *WaitGroup) Add(closure func()) {
wg.BlockAdd()
go func() {
defer wg.Done()
closure()
}()
}

// BlockAdd pushes ‘one’ into the group. Blocks if the group is full.
func (wg *WaitGroup) BlockAdd() {
if wg.size > 0 {
wg.pool <- 1
}
// atomic.AddInt64(&wg.waitCount, 1)
wg.waitGroup.Add(1)
}

Expand All @@ -40,7 +48,6 @@ func (wg *WaitGroup) Done() {
if wg.size > 0 {
<-wg.pool
}
// atomic.AddInt64(&wg.waitCount, -1)
wg.waitGroup.Done()
}

Expand Down

0 comments on commit 21a5523

Please sign in to comment.