KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have an http client which creates multiple connections to the host. I want to set a maximum number of connections it can set to a particular host. There are no such options in go's request.Transport. My code looks like package main import ( "fmt" "net/http" "net/url" ) const ( endpoint_url_fmt = "https://blah.com/api1?%s" ) func main() { transport := http.Transport{ DisableKeepAlives : false } outParams := url.Values{} outParams.Set("method", "write") outParams.Set("message", "BLAH") for { // Encode as part of URI. outboundRequest, err := http.NewRequest( "GET", fmt.Sprintf(endpoint_url_fmt, outParams.Encode()), nil ) outboundRequest.Close = false _ , err = transport.RoundTrip(outboundRequest) if err != nil { fmt.Println(err) } } } I would expect this to create 1 connection. As I am calling it in a for-loop. But this keeps creating an infinite number of connections. Where as similar python code using the requests library creates only one connection. #!/usr/bin/env python import requests endpoint_url_fmt = "https://something.com/restserver.php" params = {} params['method'] = 'write' params['category'] = category_errors_scuba params['message'] = "blah" while True: r = requests.get(endpoint_url_fmt, params = params) For some reason the go code is not reusing http connections. EDIT : The go code needs the body to be closed to reuse the connection. resp , err = transport.RoundTrip(outboundRequest) resp.Close() // This allows the connection to be reused
Tags (comma-separated)
Save Edits
Cancel