Skip to content

Instantly share code, notes, and snippets.

@hxzhouh
Created September 5, 2024 01:33
Show Gist options
  • Save hxzhouh/1e63ef82a1088ac378384e30651b20c9 to your computer and use it in GitHub Desktop.
Save hxzhouh/1e63ef82a1088ac378384e30651b20c9 to your computer and use it in GitHub Desktop.
http request body leak
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
_ "net/http/pprof"
"time"
)
func NewHttpService() {
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, world!"))
})
err := http.ListenAndServe(":8081", nil)
if err != nil {
log.Println("http.ListenAndServe err:", err)
}
}
func main() {
go NewHttpService()
time.Sleep(1 * time.Second)
for { // 模拟大量请求
go makeRequest()
time.Sleep(50 * time.Millisecond)
}
}
func makeRequest() {
client := &http.Client{}
req, err := http.NewRequest(http.MethodGet, "http://localhost:8081", nil)
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
}
_, err = ioutil.ReadAll(res.Body)
// defer res.Body.Close()
if err != nil {
fmt.Println(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment