Last active
February 25, 2020 18:35
-
-
Save tjblackheart/e3213427eb889c2882db0030e35f60f8 to your computer and use it in GitHub Desktop.
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
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