Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http: allow concurrent requests with different Transport proxy #31952

Closed
windzhu0514 opened this issue May 10, 2019 · 5 comments
Closed

net/http: allow concurrent requests with different Transport proxy #31952

windzhu0514 opened this issue May 10, 2019 · 5 comments

Comments

@windzhu0514
Copy link

@windzhu0514 windzhu0514 commented May 10, 2019

What version of Go are you using (go version)?

$ go version
go version go1.12 windows/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
set GOARCH=amd64
set GOOS=windows

What did you do?

there is one transport in my program
several goroutine send get request with same url
when one request return 403 , i change the proxy

What did you expect to see?

every goroutine can use it own proxy
it seems transport.Proxy is not safe for concurrent

What did you see instead?

the proxy in other goroutine request is also change

example code

package main

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

func main() {
	log.SetFlags(log.Lshortfile | log.Ldate | log.Ltime)
	client := &http.Client{
		Transport: &http.Transport{
			//Proxy: http.ProxyFromEnvironment,
			DialContext: (&net.Dialer{
				Timeout:   5 * 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, "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.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
}

i want each request use it's own proxy

@andybons andybons changed the title concurrent request with different proxy net/http: allow concurrent requests with different Transport proxy May 13, 2019
@andybons

This comment has been minimized.

Copy link
Member

@andybons andybons commented May 13, 2019

Can you show example code?

@bradfitz

@andybons andybons added this to the Unplanned milestone May 13, 2019
@windzhu0514

This comment has been minimized.

Copy link
Author

@windzhu0514 windzhu0514 commented May 14, 2019

i have add some example code @bradfitz

@bradfitz

This comment has been minimized.

Copy link
Member

@bradfitz bradfitz commented May 14, 2019

Correct; you cannot set Transport.Proxy from two concurrent goroutines, just like how you can't set anything else concurrently from two concurrent goroutines.

Your Transport.Proxy function should be set before any concurrent use and then it should consult its request *http.Request input to make its routing decision.

@bradfitz bradfitz closed this May 14, 2019
@sinylei

This comment has been minimized.

Copy link

@sinylei sinylei commented May 19, 2019

@windzhu0514 , Hi, buddy, I met exactly the same issue, I have a proxy pool, for each http request I will choose one proxy from the pool. With only one HttpClient, proxy is not concurrent safe.
Have you found any solution? Use different HttpClient for each Proxy?

@windzhu0514

This comment has been minimized.

Copy link
Author

@windzhu0514 windzhu0514 commented May 20, 2019

@sinylei the solution i found is writing youself Proxy select func and set transport.Proxy before before any concurrent use.
erery new request will call the proxy select func

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.