代码
package main
import (
"crypto/tls"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"strings"
"time"
)
func panicIfNotNil(e error) {
if e != nil {
panic(e)
}
}
func loop() {
ex, err := os.Executable()
panicIfNotNil(err)
ex = path.Dir(ex)
for {
now := time.Now()
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DisableKeepAlives: true, // 可以关闭链接
}
req, err := http.NewRequest("GET", "https://httpbin.org/ip", nil)
panicIfNotNil(err)
client := &http.Client{
Timeout: time.Second * 8,
Transport: tr,
}
resp, err := client.Do(req)
if resp != nil {
bodyByteArr, err := ioutil.ReadAll(resp.Body)
panicIfNotNil(err)
log.Printf("status_code:%d, body:%s, cost:%v", resp.StatusCode, string(bodyByteArr), time.Since(now))
defer resp.Body.Close()
}
if err != nil && strings.Index(err.Error(), "Client.Timeout exceeded while awaiting headers") > -1 {
continue
}
panicIfNotNil(err)
time.Sleep(time.Second * 3)
}
}
func main() {
loop()
}
一开始只有 4-m 的样子,过了两分钟就到 10M 左右了,这是为什么呀,我可能确保链接时关闭了的 求大佬指点下