修改 http 连接模式为 KeepAlived , 并增加了连接池
diff --git a/message.go b/message.go
index 2a78277..4036ad9 100644
--- a/message.go
+++ b/message.go
@@ -10,7 +10,7 @@
)
type MessageWriter struct {
- FuncNo int `json: "funcno"`
+ FuncNo int `json:"funcno"`
Attributes map[string]interface{}
ColumnNames []string
ColumnDescs []string
diff --git a/webservice.go b/webservice.go
index 43b2e87..ead20d5 100644
--- a/webservice.go
+++ b/webservice.go
@@ -16,17 +16,19 @@
"net/url"
"strconv"
"strings"
+ "sync"
"time"
)
type WebSession struct {
- AppId string
- TermId string
- Appsecret string
- BaseUrl string
- DefaultTimeout int
- session_key string
- ssl_verify bool
+ AppId string
+ TermId string
+ Appsecret string
+ BaseUrl string
+ DefaultTimeout int
+ session_key string
+ ssl_verify bool
+ httpConnectionPool sync.Pool
}
func safe_get_json_int(value interface{}) int {
@@ -83,7 +85,9 @@
}
func (w *WebSession) DoGet(uri string, params map[string]string) (*http.Response, error) {
- client := w.NewClient(3)
+ transport := w.httpConnectionPool.Get().(*http.Transport)
+ defer w.httpConnectionPool.Put(transport)
+ client := &http.Client{Transport: transport, Timeout: time.Duration(3) * time.Second}
full_url := w.BaseUrl + uri
@@ -115,34 +119,37 @@
return w.SignWithKey(w.Appsecret, message)
}
-func (w *WebSession) NewClient(timeout int) *http.Client {
+func newTransport(baseurl string, ssl_verify bool) *http.Transport {
var transport http.Transport
- if strings.HasPrefix(w.BaseUrl, "https://") {
+ if strings.HasPrefix(baseurl, "https://") {
var b bool
- if w.ssl_verify {
+ if ssl_verify {
b = false
} else {
b = true
}
- transport = http.Transport{MaxIdleConnsPerHost: 0, DisableKeepAlives: true,
+ transport = http.Transport{MaxIdleConnsPerHost: 0,
TLSClientConfig: &tls.Config{InsecureSkipVerify: b},
- TLSHandshakeTimeout: time.Duration(timeout) * time.Second,
+ TLSHandshakeTimeout: time.Duration(1) * 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{MaxIdleConnsPerHost: 0, DisableKeepAlives: true,
+ } else if strings.HasPrefix(baseurl, "http://") {
+ transport = http.Transport{MaxIdleConnsPerHost: 0,
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, Timeout: time.Duration(timeout) * time.Second}
+ log.Debugf("创建新连接")
+ return &transport
}
func (w *WebSession) DoPost(uri string, param map[string]string) (*http.Response, error) {
- client := w.NewClient(3)
+ transport := w.httpConnectionPool.Get().(*http.Transport)
+ defer w.httpConnectionPool.Put(transport)
+ client := &http.Client{Transport: transport, Timeout: time.Duration(3) * time.Second}
param["app_id"] = w.AppId
param["term_id"] = w.TermId
param["sign_method"] = "HMAC"
@@ -183,7 +190,10 @@
TermId: termid,
BaseUrl: baseurl,
DefaultTimeout: timeout,
- ssl_verify: sslVerify}
+ ssl_verify: sslVerify,
+ httpConnectionPool: sync.Pool{New: func() interface{} {
+ return newTransport(baseurl, sslVerify)
+ }}}
}
func (w *WebSession) getAuthToken() (string, error) {
@@ -286,7 +296,9 @@
func (w *WebSession) CallService2(path string, params map[string]interface{}, timeout int,
sign_field ...string) (response *ServiceResponse, err error) {
- client := w.NewClient(timeout)
+ transport := w.httpConnectionPool.Get().(*http.Transport)
+ defer w.httpConnectionPool.Put(transport)
+ client := http.Client{Transport: transport, Timeout: time.Duration(timeout) * time.Second}
err = nil
params["app_id"] = w.AppId
params["term_id"] = w.TermId