Skip to content

Instantly share code, notes, and snippets.

@tjblackheart
Last active February 25, 2020 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjblackheart/e3213427eb889c2882db0030e35f60f8 to your computer and use it in GitHub Desktop.
Save tjblackheart/e3213427eb889c2882db0030e35f60f8 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net/http"
"time"
)
type cfg struct {
sites []string
max int
results chan string
errors chan error
}
func main() {
c := cfg{
sites: []string{
"https://www.mirkrestikom.ru/", // a slow site
"https://google.com",
"https://www.amazon.de",
"https://www.github.com",
"https://twitter.com",
"https://no.such.host.exists",
},
results: make(chan string),
errors: make(chan error),
max: 3,
}
for _, site := range c.sites {
go check(site, c.results, c.errors)
}
calls := 0
for {
select {
case r := <-c.results:
fmt.Println("received on results:", r)
case e := <-c.errors:
fmt.Println("received on errors:", e.Error())
}
calls++
if calls%len(c.sites) == 0 {
fmt.Println("Waiting for next cycle ...")
fmt.Println()
}
if calls == c.max*len(c.sites) {
fmt.Println("Finished.")
break
}
}
}
func check(site string, results chan string, errors chan error) {
start := time.Now()
r, err := http.Get(site)
if err != nil {
errors <- err
} else {
end := time.Since(start)
results <- fmt.Sprintf("%s: %s [%s]", site, r.Status, end.String())
}
time.Sleep(10 * time.Second)
check(site, results, errors)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment