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.
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.
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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With