Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query URL without redirect in Go

Tags:

redirect

go

I am writing a benchmark test for a redirect script.

I wisg my program to query certain URL that redirects to AppStore. But I do not wish to download AppStore page. I just wish to log redirect URL or error.

How do I tell Go to query URL without second redirect query?


UPDATE

Both answers are correct BUT:

I tried both solutions. I am doing benchmarking. I run 1 or many go processes with 10 - 500 go routines. They query URL in a loop. My server is also written in go. It reports number of requests every second.

  • First solution: http.DefaultTransport.RoundTrip - works slow, gives errors. First 4 seconds works fine. Making 300-500 queries then performance drops to 80 query per second.

Then drops to 0-5 query per second and queryies script start getting errors like

dial tcp IP:80: A connection attempt failed because the connected 
party did not properly respond after a period of time, or established 
connection failed because connected host has failed to respond.

I guess it re-use connection that is closed.

  • Second solution: CheckRedirect field works with constant performance. I am not sure if it re-uses connections or it opens a new connection for every request. I create client for every request in a loop. It is how it will behave in a real life (every request is a new connection). Is there way to ensure that connections are closed after each query and not re-used?

That is why I am going to mark second solution as such that answer my question. But for my research it is very important that each query was a new connection. How can I ensure with second solution?

like image 952
Max Avatar asked Jan 19 '13 23:01

Max


1 Answers

You need to use an http.Transport instead of an http.Client. Transport is lower-level and does not follow redirects.

req, err := http.NewRequest("GET", "http://example.com/redirectToAppStore", nil)
// ...
resp, err := http.DefaultTransport.RoundTrip(req)
like image 89
andybalholm Avatar answered Oct 09 '22 21:10

andybalholm