Skip to content

Commit d348bcb

Browse files
perf(apiclient): add connection keep-alive http header, reuse http client
1 parent f96260b commit d348bcb

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

apiclient/apiclient.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ type APIClient struct {
104104
logger LG.ILogger
105105
subUser string
106106
roleSeparator string
107+
client *http.Client
107108
}
108109

109110
// RequestOptions represents the options for an API request.
@@ -129,6 +130,7 @@ func NewAPIClient() *APIClient {
129130
ua: "",
130131
logger: nil,
131132
roleSeparator: ":",
133+
client: &http.Client{},
132134
}
133135
cl.UseLIVESystem()
134136
cl.SetDefaultLogger()
@@ -405,12 +407,11 @@ func (cl *APIClient) Request(cmd map[string]interface{}, opts ...*RequestOptions
405407
secured := cl.GetPOSTData(newcmd, true)
406408

407409
val, err := cl.GetProxy()
408-
client := &http.Client{
409-
Timeout: cl.socketTimeout,
410-
}
410+
cl.client.Timeout = cl.socketTimeout
411+
411412
if err == nil {
412413
if proxyconfigurl, parsingerr := url.Parse(val); parsingerr == nil {
413-
client.Transport = &http.Transport{Proxy: http.ProxyURL(proxyconfigurl)}
414+
cl.client.Transport = &http.Transport{Proxy: http.ProxyURL(proxyconfigurl)}
414415
} else if cl.debugMode {
415416
fmt.Println("Not able to parse configured Proxy URL: " + val)
416417
}
@@ -424,14 +425,15 @@ func (cl *APIClient) Request(cmd map[string]interface{}, opts ...*RequestOptions
424425
}
425426
return r
426427
}
427-
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
428-
req.Header.Add("Expect", "")
429-
req.Header.Add("User-Agent", cl.GetUserAgent())
428+
req.Header.Set("Connection", "keep-alive")
429+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
430+
req.Header.Set("Expect", "")
431+
req.Header.Set("User-Agent", cl.GetUserAgent())
430432
val, err = cl.GetReferer()
431433
if err != nil {
432434
req.Header.Add("Referer", val)
433435
}
434-
resp, err2 := client.Do(req)
436+
resp, err2 := cl.client.Do(req)
435437
if err2 != nil {
436438
tpl := rtm.GetTemplate("httperror")
437439
r := R.NewResponse(tpl, newcmd, cfg)

0 commit comments

Comments
 (0)