/* Demonstrate fan-out by calling httpbin 12 times all at once */
Demonstrate fan-out by calling httpbin 12 times all at once
/ * Demonstrate fan - out by calling httpbin 12 times all at once * /
func HTTPBinHandler(w http.ResponseWriter, r *http.Request) {
	SetPlatformResponseHeaders(w, r)
	ch := make(chan *FanOutResponse)
	responses := []*FanOutResponse{}
	statusCodes := [12]string{
		"200", "201", "204", "206",
		"301", "307",
		"400", "401", "403", "404",
		"500", "501",
	}
	baseURL := "https://httpbin.org/status/"
	for _, code := range statusCodes {
		go func(url string) {
			resp, err := http.Get(url)
			ch <- &FanOutResponse{url, resp, err}
		}(baseURL + code)
	}
	waitLoop:
		for {
			r := <-ch
			responses = append(responses, r)
			if len(responses) == len(statusCodes) {
				break waitLoop
			}
		}
	done := map[string]string{
		"done?":"yep",
	}
	json.NewEncoder(w).Encode(done)
}
func HTTPBinHandler ( w http . ResponseWriter , r * http . Request ) { SetPlatformResponseHeaders ( w , r ) ch := make ( chan * FanOutResponse ) responses := [ ] * FanOutResponse { } statusCodes := [ 12 ] string { " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , } baseURL := " " for _ , code := range statusCodes { go func ( url string ) { resp , err := http . Get ( url ) ch <- & FanOutResponse { url , resp , err } } ( baseURL + code ) } waitLoop : for { r := <- ch responses = append ( responses , r ) if len ( responses ) == len ( statusCodes ) { break waitLoop } } done := map [ string ] string { " " : " " , } json . NewEncoder ( w ) . Encode ( done ) }
Demonstrate fan-out by calling httpbin 12 times all at once
Demonstrate fan - out by calling httpbin 12 times all at once
// Create a channel and response holder // These are the status codes we'll hit // Place the requests // Create goroutine for the url // Write our response to the channel // Await the response object, this could use optimization //FIXME: Replace this with the json responses