Created
December 13, 2019 15:52
-
-
Save oxtoacart/ac5818491f0cdfcea0580253a562ea74 to your computer and use it in GitHub Desktop.
demonstrates what happens when transport closes idle connections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"io" | |
"log" | |
"net" | |
"net/http" | |
"os" | |
) | |
func main() { | |
client := &http.Client{ | |
Transport: &http.Transport{ | |
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { | |
fmt.Println("dialing") | |
d := &net.Dialer{} | |
conn, err := d.DialContext(ctx, network, addr) | |
if err == nil { | |
conn = &loggingConn{conn} | |
} | |
return conn, err | |
}, | |
MaxIdleConns: -1, | |
}, | |
} | |
for i := 0; i < 2; i++ { | |
resp, err := client.Get("http://ipecho.net/plain") | |
if err != nil { | |
log.Fatal(err) | |
} | |
io.Copy(os.Stdout, resp.Body) | |
resp.Body.Close() | |
fmt.Println("") | |
} | |
} | |
type loggingConn struct { | |
net.Conn | |
} | |
func (conn *loggingConn) Read(b []byte) (int, error) { | |
n, err := conn.Conn.Read(b) | |
fmt.Printf("read %d: %v\n", n, err) | |
return n, err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment