...

Text file src/github.com/nozzle/throttler/README.md

Documentation: github.com/nozzle/throttler

     1# Throttler - intelligent WaitGroups
     2
     3[![GoDoc](https://godoc.org/github.com/nozzle/throttler?status.svg)](http://godoc.org/github.com/nozzle/throttler) [![Coverage Status](https://coveralls.io/repos/nozzle/throttler/badge.svg?branch=master)](https://coveralls.io/r/nozzle/throttler?branch=master)
     4
     5
     6 Throttler fills the gap between sync.WaitGroup and manually monitoring your goroutines with channels. The API is almost identical to Wait Groups, but it allows you to set a max number of workers that can be running simultaneously. It uses channels internally to block until a job completes by calling Done() or until all jobs have been completed. It also provides a built in error channel that captures your goroutine errors and provides access to them as `[]error` after you exit the loop.
     7
     8See a fully functional example on the playground at http://bit.ly/throttler-v3
     9
    10Compare the Throttler example to the sync.WaitGroup example from http://golang.org/pkg/sync/#example_WaitGroup
    11
    12### How to use Throttler
    13
    14```
    15// This example fetches several URLs concurrently,
    16// using a Throttler to block until all the fetches are complete.
    17// Compare to http://golang.org/pkg/sync/#example_WaitGroup
    18func ExampleThrottler() {
    19	var urls = []string{
    20		"http://www.golang.org/",
    21		"http://www.google.com/",
    22		"http://www.somestupidname.com/",
    23	}
    24	// Create a new Throttler that will get 2 urls at a time
    25	t := throttler.New(2, len(urls))
    26	for _, url := range urls {
    27		// Launch a goroutine to fetch the URL.
    28		go func(url string) {
    29			// Fetch the URL.
    30			err := http.Get(url)
    31			// Let Throttler know when the goroutine completes
    32			// so it can dispatch another worker
    33			t.Done(err)
    34		}(url)
    35		// Pauses until a worker is available or all jobs have been completed
    36		// Returning the total number of goroutines that have errored
    37		// lets you choose to break out of the loop without starting any more
    38		errorCount := t.Throttle()
    39	}
    40}
    41```
    42
    43### vs How to use a sync.WaitGroup
    44
    45```
    46// This example fetches several URLs concurrently,
    47// using a WaitGroup to block until all the fetches are complete.
    48func ExampleWaitGroup() {
    49	var wg sync.WaitGroup
    50	var urls = []string{
    51		"http://www.golang.org/",
    52		"http://www.google.com/",
    53		"http://www.somestupidname.com/",
    54	}
    55	for _, url := range urls {
    56		// Increment the WaitGroup counter.
    57		wg.Add(1)
    58		// Launch a goroutine to fetch the URL.
    59		go func(url string) {
    60			// Decrement the counter when the goroutine completes.
    61			defer wg.Done()
    62			// Fetch the URL.
    63			http.Get(url)
    64		}(url)
    65	}
    66	// Wait for all HTTP fetches to complete.
    67	wg.Wait()
    68}
    69```

View as plain text