Last active
August 6, 2020 13:25
-
-
Save jreisinger/4792c2c2a4938ca46a37856249fef558 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
// A common use case for Go is to take a stream of jobs of work and perform them | |
// automatically scaling up and down as work becomes available. | |
package main | |
import ( | |
"bufio" | |
"fmt" | |
"log" | |
"net/http" | |
"os" | |
"sync" | |
) | |
type task interface { | |
process() | |
output() | |
} | |
type factory interface { | |
create(line string) task | |
} | |
func run(f factory) { | |
var wg sync.WaitGroup | |
in := make(chan task) | |
wg.Add(1) | |
go func() { | |
s := bufio.NewScanner(os.Stdin) | |
for s.Scan() { | |
in <- f.create(s.Text()) | |
} | |
if s.Err() != nil { | |
log.Fatalf("Error reading STDIN: %s", s.Err()) | |
} | |
close(in) | |
wg.Done() | |
}() | |
out := make(chan task) | |
for i := 0; i < 1000; i++ { | |
wg.Add(1) | |
go func() { | |
for t := range in { | |
t.process() | |
out <- t | |
} | |
wg.Done() | |
}() | |
} | |
go func() { | |
wg.Wait() | |
close(out) | |
}() | |
for t := range out { | |
t.output() | |
} | |
} | |
type HTTPTask struct { | |
url string | |
ok bool | |
} | |
func (h *HTTPTask) process() { | |
resp, err := http.Get(h.url) | |
if err != nil { | |
h.ok = false | |
return | |
} | |
if resp.StatusCode == http.StatusOK { | |
h.ok = true | |
return | |
} | |
h.ok = false | |
} | |
type Factory struct { | |
} | |
func (f *Factory) create(line string) task { | |
h := &HTTPTask{} | |
h.url = line | |
return h | |
} | |
func (h *HTTPTask) output() { | |
fmt.Printf("%s %t\n", h.url, h.ok) | |
} | |
func main() { | |
f := &Factory{} | |
run(f) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment