Skip to content

Commit e89d0ff

Browse files
committed
reuse transport & expose Account.SetTransport()
1 parent c83f12b commit e89d0ff

File tree

3 files changed

+82
-63
lines changed

3 files changed

+82
-63
lines changed

account.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"strconv"
66
"encoding/json"
7+
"net/http"
78
)
89

910
const (
@@ -20,12 +21,19 @@ func NewAccount(endpoint, secretId, secretKey string) *Account {
2021
}
2122
}
2223

23-
func (this *Account) SetProxy(proxyUrl string) {
24+
func (this *Account) SetProxy(proxyUrl string) *Account{
2425
this.client.setProxy(proxyUrl)
26+
return this
2527
}
2628

27-
func (this *Account) UnsetProxy() {
29+
func (this *Account) UnsetProxy() *Account{
2830
this.client.unsetProxy()
31+
return this
32+
}
33+
34+
func (this *Account) SetTransport(transport http.RoundTripper) *Account{
35+
this.client.CmqHttp.SetTransport(transport)
36+
return this
2937
}
3038

3139
func (this *Account) setSignMethod(method string) (err error) {

cmq_client.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ type CMQClient struct {
2121
SecretKey string
2222
Method string
2323
SignMethod string
24-
Proxy string
2524
CmqHttp *CMQHttp
2625
}
2726

@@ -48,12 +47,12 @@ func (this *CMQClient) setSignMethod(signMethod string) (err error) {
4847
}
4948

5049
func (this *CMQClient) setProxy(proxyUrl string) {
51-
this.Proxy = proxyUrl
50+
this.CmqHttp.setProxy(proxyUrl)
5251
return
5352
}
5453

5554
func (this *CMQClient) unsetProxy() {
56-
this.Proxy = ""
55+
this.CmqHttp.unsetProxy()
5756
return
5857
}
5958

@@ -121,10 +120,6 @@ func (this *CMQClient) call(action string, param map[string]string) (resp string
121120
//log.Printf("urlStr :%v", urlStr)
122121
//log.Printf("reqStr :%v", reqStr)
123122

124-
proxyUrlStr := this.Proxy
125-
if proxyUrl, found := param["proxyUrl"]; found {
126-
proxyUrlStr = proxyUrl
127-
}
128123
userTimeout := 0
129124
if UserpollingWaitSeconds, found := param["UserpollingWaitSeconds"]; found {
130125
userTimeout, err = strconv.Atoi(UserpollingWaitSeconds)
@@ -133,7 +128,7 @@ func (this *CMQClient) call(action string, param map[string]string) (resp string
133128
}
134129
}
135130

136-
resp, err = this.CmqHttp.request(this.Method, urlStr, reqStr, proxyUrlStr, userTimeout)
131+
resp, err = this.CmqHttp.request(this.Method, urlStr, reqStr, userTimeout)
137132
if err != nil {
138133
return resp, fmt.Errorf("CmqHttp.request failed: %v", err.Error())
139134
}

cmq_http.go

Lines changed: 69 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,112 @@
11
package cmq_go
22

33
import (
4-
"net"
54
"time"
65
"net/http"
76
"fmt"
87
"io/ioutil"
98
"net/url"
109
"bytes"
10+
"errors"
11+
"net"
12+
)
13+
14+
const (
15+
DEFAULT_HTTP_TIMEOUT = 3000 //ms
1116
)
1217

1318
type CMQHttp struct {
14-
timeout int
1519
isKeepAlive bool
1620
conn *http.Client
1721
}
1822

23+
var DefaultTransport = &http.Transport{
24+
Proxy: http.ProxyFromEnvironment,
25+
DialContext: (&net.Dialer{
26+
Timeout: 30 * time.Second,
27+
KeepAlive: 30 * time.Second,
28+
DualStack: true,
29+
}).DialContext,
30+
MaxIdleConns: 500,
31+
MaxIdleConnsPerHost: 100,
32+
IdleConnTimeout: 90 * time.Second,
33+
TLSHandshakeTimeout: 10 * time.Second,
34+
ExpectContinueTimeout: 1 * time.Second,
35+
}
36+
1937
func NewCMQHttp() *CMQHttp {
2038
return &CMQHttp{
21-
timeout: 10000,
2239
isKeepAlive: true,
23-
conn: nil,
40+
conn: &http.Client{
41+
Timeout: DEFAULT_HTTP_TIMEOUT * time.Millisecond,
42+
Transport: DefaultTransport,
43+
},
2444
}
45+
2546
}
2647

27-
func (this *CMQHttp) request(method, urlStr, reqStr, proxyUrlStr string, userTimeout int) (result string, err error){
28-
var client *http.Client
29-
timeout := 0
30-
if userTimeout >= 0 {
31-
timeout = userTimeout
48+
func (this *CMQHttp) setProxy(proxyUrlStr string) (err error) {
49+
if proxyUrlStr == "" {
50+
return
3251
}
33-
keepalive := 0
34-
if this.isKeepAlive {
35-
keepalive = 30
52+
proxyUrl, err := url.Parse(proxyUrlStr)
53+
if err != nil {
54+
return
3655
}
56+
transport, err := this.getTransport()
57+
if err != nil {
58+
return
59+
}
60+
transport.Proxy = http.ProxyURL(proxyUrl)
61+
return
62+
}
3763

38-
if proxyUrlStr == "" {
39-
unproxyTransport := &http.Transport{
40-
Proxy: http.ProxyFromEnvironment,
41-
DialContext: (&net.Dialer{
42-
Timeout: 30 * time.Second,
43-
KeepAlive: time.Duration(keepalive) * time.Second,
44-
DualStack: true,
45-
}).DialContext,
46-
MaxIdleConns: 100,
47-
IdleConnTimeout: 90 * time.Second,
48-
TLSHandshakeTimeout: 10 * time.Second,
49-
ExpectContinueTimeout: 1 * time.Second,
50-
}
64+
func (this *CMQHttp) unsetProxy() (err error) {
65+
transport, err := this.getTransport()
66+
if err != nil {
67+
return
68+
}
69+
transport.Proxy = nil
70+
return
71+
}
72+
73+
func (this *CMQHttp) getTransport() (*http.Transport, error) {
74+
if this.conn.Transport == nil {
75+
this.SetTransport(DefaultTransport)
76+
}
5177

52-
client = &http.Client{
53-
Timeout: time.Duration(timeout) * time.Second,
54-
Transport: unproxyTransport,
55-
}
56-
} else {
57-
proxyUrl, err := url.Parse(proxyUrlStr)
58-
if err != nil {
59-
panic(err)
60-
}
61-
proxyTransport := &http.Transport{
62-
Proxy: http.ProxyURL(proxyUrl),
63-
DialContext: (&net.Dialer{
64-
Timeout: 30 * time.Second,
65-
KeepAlive: time.Duration(keepalive) * time.Second,
66-
DualStack: true,
67-
}).DialContext,
68-
MaxIdleConns: 100,
69-
IdleConnTimeout: 90 * time.Second,
70-
TLSHandshakeTimeout: 10 * time.Second,
71-
ExpectContinueTimeout: 1 * time.Second,
72-
}
73-
client = &http.Client{
74-
Timeout: time.Duration(timeout) * time.Second,
75-
Transport: proxyTransport,
76-
}
78+
if transport, ok := this.conn.Transport.(*http.Transport); ok {
79+
return transport, nil
80+
}
81+
return nil, errors.New("transport is not an *http.Transport instance")
82+
}
83+
84+
func (this *CMQHttp) SetTransport(transport http.RoundTripper) {
85+
this.conn.Transport = transport
86+
}
87+
88+
func (this *CMQHttp) request(method, urlStr, reqStr string, userTimeout int) (result string, err error) {
89+
timeout := DEFAULT_HTTP_TIMEOUT
90+
if userTimeout >= 0 {
91+
timeout += userTimeout
7792
}
93+
this.conn.Timeout = time.Duration(timeout) * time.Millisecond
7894

7995
req, err := http.NewRequest(method, urlStr, bytes.NewReader([]byte(reqStr)))
8096
if err != nil {
8197
return "", fmt.Errorf("make http req error %v", err)
8298
}
83-
resp, err := client.Do(req)
99+
resp, err := this.conn.Do(req)
84100
if err != nil {
85101
return "", fmt.Errorf("http error %v", err)
86102
}
87103
defer resp.Body.Close()
88104
if resp.StatusCode != http.StatusOK {
89-
return "",fmt.Errorf("http error code %d", resp.StatusCode)
105+
return "", fmt.Errorf("http error code %d", resp.StatusCode)
90106
}
91107
body, err := ioutil.ReadAll(resp.Body)
92108
if err != nil {
93-
return "",fmt.Errorf("read http resp body error %v", err)
109+
return "", fmt.Errorf("read http resp body error %v", err)
94110
}
95111
result = string(body)
96112
return

0 commit comments

Comments
 (0)