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
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@windzhu0514
Copy link

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
Copy link
Member

Can you show example code?

@bradfitz

@andybons andybons added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label May 13, 2019
@andybons andybons added this to the Unplanned milestone May 13, 2019
@bradfitz bradfitz added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label May 13, 2019
@windzhu0514
Copy link
Author

i have add some example code @bradfitz

@bradfitz
Copy link
Contributor

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.

@sinylei
Copy link

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
Copy link
Author

@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

@golang golang locked and limited conversation to collaborators May 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

5 participants