Skip to content

Instantly share code, notes, and snippets.

@inhies
Created November 17, 2013 06:06
Show Gist options
  • Save inhies/7509915 to your computer and use it in GitHub Desktop.
Save inhies/7509915 to your computer and use it in GitHub Desktop.
Demonstration of using tons of go routines to http.Get lots of web pages.
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"runtime"
"sync"
)
func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
}
func main() {
maxConncurrent := 100
var wg sync.WaitGroup
urls := make(chan string, maxConncurrent)
for i := 0; i < maxConncurrent; i++ {
go func() {
for url := range urls {
resp, err := http.Get(url)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if len(body) > 22609 {
fmt.Println("FOUND:", url)
}
wg.Done()
}
}()
}
for i := 0; i < 10000; i++ {
wg.Add(1)
urls <- fmt.Sprintf("http://dawnofthedeaddash.com/mobile/?code=%d", i)
}
wg.Wait()
fmt.Println("Finished")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment