0

Requirement: worker goroutines concurrently send HTTPS(S) requests to various IPs. The URL and server IP are specified by the "main" routine in the parameter list when creating workers.
For instance: there are 100 worker goroutines requests https://www.google.com/ from different server IPs. The first worker asks for it from 1.1.1.1, and the second one does exactly the same thing but a different server IP (say 2.2.2.2).
Current solution: assign one http.Client with customized dial function in http.Transport for each routine.

   go func(host, url string) {
        defer wg.Done()
        Client:=&http.Client{
        Transport: &http.Transport{
            DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
                n, e := net.Dial("tcp", host)
                fmt.Println(e)
                return n, e
            },
          },
       }

        resp, err := Client.Get(url)
        if err != nil {
            fmt.Println(err)
            return
        }

        defer resp.Body.Close()

        c, e := ioutil.ReadAll(resp.Body)
        if e != nil {
            return
        }
        fmt.Println(string(c))

    }(realHost, url)

according to the godoc, Clients and Transports are safe for concurrent use by multiple goroutines and for efficiency should only be created once and re-used.

Questions: So how can I fulfill my requirement by re-using the Client and transport?

1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented May 30, 2022 at 5:11

1 Answer 1

0

Create a request and swap hosts in the request.

req, err := http.NewRequst(“GET”, url, nil)
req.Host = req.URL.Host // set host header
req.URL.Host = host     // set dial host

Use the default client and transport to execute the request.

 resp, err := http.DefaultClient.Do(req)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but 1) why do you do req.Host = req.URL.Host? I guess req.Host always equals to req.URL.Host by default. What makes them different? 2) I don't think this solution works with HTTPS, it fails to check SSL cert (certificate name does not match input).
(1) the host header is different from the dial address. (2) Yeah, this only works with http.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.