http.Client 默认开启了 KeepAlived , 修改为禁用。
diff --git a/webservice.go b/webservice.go
index 3857002..17cc4f1 100644
--- a/webservice.go
+++ b/webservice.go
@@ -124,15 +124,21 @@
} else {
b = true
}
- transport = http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: b},
- TLSHandshakeTimeout: time.Duration(timeout) * time.Second}
+ transport = http.Transport{MaxIdleConnsPerHost: 0, DisableKeepAlives: true,
+ TLSClientConfig: &tls.Config{InsecureSkipVerify: b},
+ TLSHandshakeTimeout: time.Duration(timeout) * time.Second,
+ Dial: func(network, addr string) (net.Conn, error) {
+ default_timeout := time.Duration(1) * time.Second
+ return net.DialTimeout(network, addr, default_timeout)
+ }}
} else if strings.HasPrefix(w.BaseUrl, "http://") {
- transport = http.Transport{Dial: func(network, addr string) (net.Conn, error) {
- default_timeout := time.Duration(timeout) * time.Second
- return net.DialTimeout(network, addr, default_timeout)
- }}
+ transport = http.Transport{MaxIdleConnsPerHost: 0, DisableKeepAlives: true,
+ Dial: func(network, addr string) (net.Conn, error) {
+ default_timeout := time.Duration(1) * time.Second
+ return net.DialTimeout(network, addr, default_timeout)
+ }}
}
- return &http.Client{Transport: &transport}
+ return &http.Client{Transport: &transport, Timeout: time.Duration(timeout) * time.Second}
}
func (w *WebSession) DoPost(uri string, param map[string]string) (*http.Response, error) {
@@ -202,7 +208,7 @@
}
body, err := ioutil.ReadAll(r.Body)
- r.Body.Close()
+ defer r.Body.Close()
s := &FormJson{}
err = json.Unmarshal(body, &s)
@@ -237,7 +243,7 @@
}
body, err := ioutil.ReadAll(r.Body)
- r.Body.Close()
+ defer r.Body.Close()
s := &FormJson{}
err = json.Unmarshal(body, &s)
@@ -259,12 +265,12 @@
return nil, err
}
+ defer r.Body.Close()
if r.StatusCode != 200 {
return nil, errors.New(fmt.Sprintf("Request StatusCode:%v", r.StatusCode))
}
body, err := ioutil.ReadAll(r.Body)
- r.Body.Close()
return NewMessageReader(body), nil
}
@@ -308,13 +314,13 @@
log.Errorf("Status=%v, err=%v", r, err)
return
}
+ defer r.Body.Close()
if r.StatusCode != 200 {
log.Errorf("Request Error %v\n", r.StatusCode)
err = errors.New(fmt.Sprintf("Request Error, StatusCode : %v", r.StatusCode))
return
}
body, err := ioutil.ReadAll(r.Body)
- r.Body.Close()
var s interface{}
err = json.Unmarshal(body, &s)
if err != nil {