Created
December 13, 2018 08:13
-
-
Save jreisinger/f95905ec37b82c79417d26a67342420e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // Goroutines can share a single channel and multiple goroutines can read or write from the channel. It can be exploited to make workload balancer in just a few lines of code. | |
| package main | |
| import ( | |
| "fmt" | |
| "io/ioutil" | |
| "net/http" | |
| _ "os" | |
| ) | |
| func getPage(url string) (int, error) { | |
| resp, err := http.Get(url) | |
| if err != nil { | |
| return 0, err | |
| } | |
| defer resp.Body.Close() | |
| body, err := ioutil.ReadAll(resp.Body) | |
| if err != nil { | |
| return 0, err | |
| } | |
| return len(body), nil | |
| } | |
| func worker(urlCh chan string, sizeCh chan string, id int) { | |
| for { | |
| url := <-urlCh | |
| pageLen, err := getPage(url) | |
| if err == nil { | |
| sizeCh <- fmt.Sprintf("%s has length %d bytes (%d)", url, pageLen, id) | |
| } else { | |
| sizeCh <- fmt.Sprintf("Error getting %s: %s", url, err) | |
| } | |
| } | |
| } | |
| func generator(url string, urlCh chan string) { | |
| urlCh <- url | |
| } | |
| func main() { | |
| urlCh := make(chan string) | |
| sizeCh := make(chan string) | |
| // Simple workload balancer | |
| for i := 0; i < 10; i++ { | |
| go worker(urlCh, sizeCh, i) | |
| } | |
| urls := []string{"http://reisinge.net", "http://reisinge1.net", "http://perl.com", "http://yahoo.com"} | |
| for _, url := range urls { | |
| go generator(url, urlCh) | |
| } | |
| for i := 0; i < len(urls); i++ { | |
| fmt.Printf("%s\n", <-sizeCh) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment