Created
November 17, 2013 06:06
-
-
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.
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" | |
"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