Use http proxy concurrently

one transport in my program
several goroutine send http request
if one request response status code is 403 ,i change a proxy for that request
Will it affect other goroutine ?

package main

import (
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"os"
"os/signal"
"syscall"
"time"
)

func main() {
client := &http.Client{
	Transport: &http.Transport{
		//Proxy: http.ProxyFromEnvironment,
		DialContext: (&net.Dialer{
			Timeout:   30 * time.Second,
			KeepAlive: 30 * time.Second,
		}).DialContext,
		MaxIdleConns:          100,
		IdleConnTimeout:       90 * time.Second,
		TLSHandshakeTimeout:   10 * time.Second,
		ExpectContinueTimeout: 1 * time.Second,
	},
}

go func() {
	req, _ := http.NewRequest(http.MethodGet, "https://www.google.com", nil)
    client.Transport.(*http.Transport).Proxy = func(request *http.Request) (url *url.URL, e error) {
		return url.Parse("http://10.101.32.12")
	}
	
	response, err := client.Do(req)
	if err != nil {
		log.Println(err)
		return
	}

	defer response.Body.Close()
	data, _ := ioutil.ReadAll(response.Body)
	log.Println(string(data))

}()

go func() {
	req, _ := http.NewRequest(http.MethodGet, "http://www.baidu.com", nil)
	client.Transport.(*http.Transport).Proxy = func(request *http.Request) (url *url.URL, e error) {
		return url.Parse("http://10.101.32.13")
	}
	response, err := client.Do(req)
	if err != nil {
		log.Println(err)
		return
	}

	defer response.Body.Close()
	data, _ := ioutil.ReadAll(response.Body)
	log.Println(string(data))

}()

chOver := make(chan os.Signal, 1)
signal.Notify(chOver, syscall.SIGINT, syscall.SIGTERM)
<-chOver

}

the first goroutine use the proxy too.

Do you have any sample code to show?

In principle, each request gets its own goroutine, so any output that is written to the headers or body of that request is independent of the other requests.

1 Like

thanks for you answer,i had add some sample code

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.