Skip to content

Commit 82a7fd9

Browse files
committed
共用一个http.Client实例
1 parent 0016eae commit 82a7fd9

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

http_client.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,13 @@ func (m *mt) RoundTrip(req *http.Request) (res *http.Response, err error) {
3333
return
3434
}
3535

36+
var c *http.Client
37+
3638
func client() *http.Client {
37-
return &http.Client{Transport: &mt{http.Transport{}}}
39+
if c == nil {
40+
c = &http.Client{Transport: &mt{}}
41+
}
42+
return c
3843
}
3944

4045
func RegisterHook(hook func(req *http.Request, reqBody []byte, res *http.Response, startAt time.Time, stopAt time.Time, err error)) {

mch_req_v3.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (mr *mchReqV3) Do(method string) (err error) {
8484
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusAccepted && resp.StatusCode != http.StatusNoContent {
8585
var rs mch_api_v3.ErrorResp
8686
_ = json.Unmarshal(raw, &rs)
87-
return errors.New(fmt.Sprintf("%v | Request-ID:%v", rs.Message, resp.Header.Get("Request-ID")))
87+
return errors.New(rs.Message)
8888
}
8989
if mr.api != mch_api_v3.OtherCertificates {
9090
if err = mr.account.VerifyV3(resp.Header, raw); err != nil {

mp_req.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,26 @@ func (mp *mpReq) Do() (err error) {
7171
mp.account.ServerHost = mp_api.ServerHostUniversal
7272
}
7373
var apiUrl = fmt.Sprintf("https://%v/%v?%v", mp.account.ServerHost, mp.path, v.Encode())
74-
var resp *http.Response
74+
var req *http.Request
7575
if mp.sendData == nil {
76-
resp, err = client().Get(apiUrl)
76+
req, err = http.NewRequest(http.MethodGet, apiUrl, nil)
7777
} else {
7878
var buf = new(bytes.Buffer)
7979
var coder = json.NewEncoder(buf)
8080
coder.SetEscapeHTML(false)
8181
if err = coder.Encode(mp.sendData); err != nil {
8282
return
8383
}
84-
resp, err = client().Post(apiUrl, "application/json", buf)
84+
req, err = http.NewRequest(http.MethodPost, apiUrl, buf)
85+
req.Header.Set("Content-Type", "application/json")
86+
}
87+
if err != nil {
88+
return
89+
}
90+
resp, err := client().Do(req)
91+
if resp != nil {
92+
defer resp.Body.Close()
8593
}
86-
defer resp.Body.Close()
8794
if err != nil {
8895
return
8996
}

0 commit comments

Comments
 (0)