Created
January 12, 2023 02:17
-
-
Save akahn/bf18b2e096558f7bc69a93a185bb53a6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 ( | |
"io" | |
"log" | |
"net/http" | |
"time" | |
) | |
func main() { | |
// Create a server with low idle timeout | |
server := &http.Server{ | |
Addr: ":8080", | |
// IdleTimeout is the maximum amount of time to wait for the | |
// next request when keep-alives are enabled. If IdleTimeout | |
// is zero, the value of ReadTimeout is used. If both are | |
// zero, there is no timeout. | |
IdleTimeout: time.Nanosecond, | |
} | |
go func() { | |
err := server.ListenAndServe() | |
if err != nil { | |
log.Fatalf("failed to start http server: %s", err) | |
} | |
}() | |
client := &http.Client{} | |
for i := 0; i < 2; i++ { | |
req, _ := http.NewRequest("GET", "http://0.0.0.0:8080", nil) | |
// The second request, which uses keep-alive by default since it's HTTP 1.1, should fail due to the tiny | |
// IdleTimeout set above. But it succeeds. | |
resp, err := client.Do(req) | |
if err != nil { | |
log.Fatalf("http request failed: %s", err) | |
} | |
body, _ := io.ReadAll(resp.Body) | |
resp.Body.Close() | |
log.Printf("got response: `%s`", body) | |
time.Sleep(time.Millisecond) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment