1 package throttler 2 3 import "fmt" 4 5 type httpPkg struct{} 6 7 func (httpPkg) Get(url string) error { return nil } 8 9 var http httpPkg 10 11 // This example fetches several URLs concurrently, 12 // using a Throttler to block until all the fetches are complete. 13 // Compare to http://golang.org/pkg/sync/#example_WaitGroup 14 func ExampleThrottler() { 15 var urls = []string{ 16 "http://www.golang.org/", 17 "http://www.google.com/", 18 "http://www.somestupidname.com/", 19 } 20 // Create a new Throttler that will get 2 urls at a time 21 t := New(2, len(urls)) 22 for _, url := range urls { 23 // Launch a goroutine to fetch the URL. 24 go func(url string) { 25 // Fetch the URL. 26 err := http.Get(url) 27 // Let Throttler know when the goroutine completes 28 // so it can dispatch another worker 29 t.Done(err) 30 }(url) 31 // Pauses until a worker is available or all jobs have been completed 32 // Returning the total number of goroutines that have errored 33 // lets you choose to break out of the loop without starting any more 34 errorCount := t.Throttle() 35 if errorCount > 0 { 36 break 37 } 38 } 39 } 40 41 // This example fetches several URLs concurrently, 42 // using a Throttler to block until all the fetches are complete 43 // and checks the errors returned. 44 // Compare to http://golang.org/pkg/sync/#example_WaitGroup 45 func ExampleThrottler_errors() error { 46 var urls = []string{ 47 "http://www.golang.org/", 48 "http://www.google.com/", 49 "http://www.somestupidname.com/", 50 } 51 // Create a new Throttler that will get 2 urls at a time 52 t := New(2, len(urls)) 53 for _, url := range urls { 54 // Launch a goroutine to fetch the URL. 55 go func(url string) { 56 // Let Throttler know when the goroutine completes 57 // so it can dispatch another worker 58 defer t.Done(nil) 59 // Fetch the URL. 60 http.Get(url) 61 }(url) 62 // Pauses until a worker is available or all jobs have been completed 63 t.Throttle() 64 } 65 66 if t.Err() != nil { 67 // Loop through the errors to see the details 68 for i, err := range t.Errs() { 69 fmt.Printf("error #%d: %s", i, err) 70 } 71 return t.Err() 72 } 73 74 return nil 75 } 76