Skip to content

Commit

Permalink
[add] support custom http client
Browse files Browse the repository at this point in the history
  • Loading branch information
shabicheng committed Apr 27, 2022
1 parent 29c41d6 commit dafcaaa
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
9 changes: 9 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ type Client struct {
UserAgent string // default defaultLogUserAgent
RequestTimeOut time.Duration
RetryTimeOut time.Duration
HTTPClient *http.Client

accessKeyLock sync.RWMutex
}
Expand All @@ -102,6 +103,9 @@ func convertLocked(c *Client, projName string) *LogProject {
p, _ := NewLogProject(projName, c.Endpoint, c.AccessKeyID, c.AccessKeySecret)
p.SecurityToken = c.SecurityToken
p.UserAgent = c.UserAgent
if c.HTTPClient != nil {
p.httpClient = c.HTTPClient
}
if c.RequestTimeOut != time.Duration(0) {
p.WithRequestTimeout(c.RequestTimeOut)
}
Expand All @@ -112,6 +116,11 @@ func convertLocked(c *Client, projName string) *LogProject {
return p
}

// SetHTTPClient set a custom http client, all request will send to sls by this client
func (c *Client) SetHTTPClient(client *http.Client) {
c.HTTPClient = client
}

// ResetAccessKeyToken reset client's access key token
func (c *Client) ResetAccessKeyToken(accessKeyID, accessKeySecret, securityToken string) {
c.accessKeyLock.Lock()
Expand Down
3 changes: 3 additions & 0 deletions client_interface.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sls

import (
"net/http"
"time"
)

Expand Down Expand Up @@ -40,6 +41,8 @@ func CreateTokenAutoUpdateClient(endpoint string, tokenUpdateFunc UpdateTokenFun

// ClientInterface for all log's open api
type ClientInterface interface {
// SetHTTPClient set a custom http client, all request will send to sls by this client
SetHTTPClient(client *http.Client)
// #################### Client Operations #####################
// ResetAccessKeyToken reset client's access key token
ResetAccessKeyToken(accessKeyID, accessKeySecret, securityToken string)
Expand Down
6 changes: 5 additions & 1 deletion client_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ func (c *Client) request(project, method, uri string, headers map[string]string,
}

// Get ready to do request
resp, err := defaultHttpClient.Do(req)
httpClient := c.HTTPClient
if httpClient == nil {
httpClient = defaultHttpClient
}
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
Expand Down
20 changes: 14 additions & 6 deletions log_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (p *LogProject) WithToken(token string) (*LogProject, error) {

// WithRequestTimeout with custom timeout for a request
func (p *LogProject) WithRequestTimeout(timeout time.Duration) *LogProject {
if p.httpClient == defaultHttpClient {
if p.httpClient == defaultHttpClient || p.httpClient == nil {
p.httpClient = &http.Client{
Timeout: timeout,
}
Expand Down Expand Up @@ -1079,7 +1079,9 @@ func (p *LogProject) DeleteLogging() (err error) {

func (p *LogProject) init() {
if p.retryTimeout == time.Duration(0) {
p.httpClient = defaultHttpClient
if p.httpClient == nil {
p.httpClient = defaultHttpClient
}
p.retryTimeout = defaultRetryTimeout
p.parseEndpoint()
}
Expand Down Expand Up @@ -1111,11 +1113,17 @@ func (p *LogProject) parseEndpoint() {
if ipRegex.MatchString(host) { // ip format
// use direct ip proxy
url, _ := url.Parse(fmt.Sprintf("%s%s", scheme, host))
p.httpClient = &http.Client{
Transport: &http.Transport{
if p.httpClient == nil || p.httpClient == defaultHttpClient {
p.httpClient = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(url),
},
Timeout: defaultRequestTimeout,
}
} else {
p.httpClient.Transport = &http.Transport{
Proxy: http.ProxyURL(url),
},
Timeout: defaultRequestTimeout,
}
}

}
Expand Down
6 changes: 6 additions & 0 deletions token_auto_update_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sls

import (
"errors"
"net/http"
"sync"
"time"

Expand Down Expand Up @@ -136,6 +137,11 @@ func (c *TokenAutoUpdateClient) processError(err error) (retry bool) {

}

// SetHTTPClient set a custom http client, all request will send to sls by this client
func (c *TokenAutoUpdateClient) SetHTTPClient(client *http.Client) {
c.logClient.SetHTTPClient(client)
}

func (c *TokenAutoUpdateClient) Close() error {
c.closeFlag = true
return nil
Expand Down

0 comments on commit dafcaaa

Please sign in to comment.