Sobes.tech
Junior — Senior

Limited goroutine pool for executing GET requests

livecode

Task condition

It is necessary to ensure that no more than three workers operate simultaneously, which make HTTP‑GET requests to URLs from the provided list and output the response body to standard output.

package main

import (
    "log"
)

// worker pool:
//   organize the launch of no more than 3 parallel workers,
//   which perform requests to all URLs from the array urls
//   and perform a GET request on each URL.
//   Output the response body to stdout in text form.

// example URLs received from the generator:
//
// http://echo.xff.pw:8080/?echo_time=100&echo_body=body-1
// http://echo.xff.pw:8080/?echo_time=1500&echo_body=body-2
// http://echo.xff.pw:8080/?echo_time=500&echo_body=body-3
// http://echo.xff.pw:8080/?echo_time=150&echo_body=body-4
// http://echo.xff.pw:8080/?echo_time=500&echo_body=body-5

// it is recommended not to change the main function,
// but if necessary for implementation — you may

func main() {
    pool := makePool(3)
    for url := range generateUrls(5) {
        log.Printf("=> Launching, %s", url)
        pool.DoWork(url)
    }
}