Skip to content

Commit

Permalink
#56, #29 mutex removed, performance improved
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Feb 25, 2017
1 parent 9ccb81c commit 90b37fd
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 72 deletions.
47 changes: 20 additions & 27 deletions client.go
Expand Up @@ -23,7 +23,6 @@ import (
"regexp"
"runtime"
"strings"
"sync"
"time"
)

Expand Down Expand Up @@ -91,7 +90,6 @@ type Client struct {
outputDirectory string
scheme string
proxyURL *url.URL
mutex *sync.Mutex
closeConnection bool
beforeRequest []func(*Client, *Request) error
udBeforeRequest []func(*Client, *Request) error
Expand Down Expand Up @@ -292,7 +290,6 @@ func (c *Client) R() *Request {
RawRequest: nil,
client: c,
bodyBuf: nil,
proxyURL: nil,
multipartFiles: []*File{},
}

Expand All @@ -311,7 +308,6 @@ func (c *Client) R() *Request {
//
func (c *Client) OnBeforeRequest(m func(*Client, *Request) error) *Client {
c.udBeforeRequest = append(c.udBeforeRequest, m)

return c
}

Expand Down Expand Up @@ -491,6 +487,8 @@ func (c *Client) Mode() string {
//
func (c *Client) SetTLSClientConfig(config *tls.Config) *Client {
c.transport.TLSClientConfig = config
c.httpClient.Transport = c.transport

return c
}

Expand All @@ -507,6 +505,7 @@ func (c *Client) SetTimeout(timeout time.Duration) *Client {
err = conn.SetDeadline(time.Now().Add(timeout))
return conn, err
}
c.httpClient.Transport = c.transport

return c
}
Expand All @@ -520,9 +519,11 @@ func (c *Client) SetTimeout(timeout time.Duration) *Client {
func (c *Client) SetProxy(proxyURL string) *Client {
if pURL, err := url.Parse(proxyURL); err == nil {
c.proxyURL = pURL
c.transport.Proxy = http.ProxyURL(c.proxyURL)
c.httpClient.Transport = c.transport
} else {
c.Log.Printf("ERROR [%v]", err)
c.proxyURL = nil
c.RemoveProxy()
}

return c
Expand All @@ -533,6 +534,9 @@ func (c *Client) SetProxy(proxyURL string) *Client {
//
func (c *Client) RemoveProxy() *Client {
c.proxyURL = nil
c.transport.Proxy = nil
c.httpClient.Transport = c.transport

return c
}

Expand All @@ -541,7 +545,6 @@ func (c *Client) RemoveProxy() *Client {
func (c *Client) SetCertificates(certs ...tls.Certificate) *Client {
config := c.getTLSConfig()
config.Certificates = append(config.Certificates, certs...)

return c
}

Expand Down Expand Up @@ -591,21 +594,22 @@ func (c *Client) SetOutputDirectory(dirPath string) *Client {
// },
// }
//
// resty.SetTransport(&transport)
// resty.SetTransport(transport)
//
func (c *Client) SetTransport(transport *http.Transport) *Client {
if transport != nil {
c.transport = transport
c.httpClient.Transport = c.transport
}

return c
}

// SetScheme method sets custom scheme in the resty client. Its way to override default.
// SetScheme method sets custom scheme in the resty client. It's way to override default.
// resty.SetScheme("http")
//
func (c *Client) SetScheme(scheme string) *Client {
if c.scheme == "" {
if len(strings.TrimSpace(scheme)) > 0 {
c.scheme = scheme
}

Expand All @@ -619,6 +623,11 @@ func (c *Client) SetCloseConnection(close bool) *Client {
return c
}

// IsProxySet method returns the true if proxy is set on client otherwise false.
func (c *Client) IsProxySet() bool {
return c.proxyURL != nil
}

// executes the given `Request` object and returns response
func (c *Client) execute(req *Request) (*Response, error) {
// Apply Request middleware
Expand All @@ -633,28 +642,17 @@ func (c *Client) execute(req *Request) (*Response, error) {
}
}

// resty middlewares
for _, f := range c.beforeRequest {
err = f(c, req)
if err != nil {
return nil, err
}
}

c.mutex.Lock()

if req.proxyURL != nil {
c.transport.Proxy = http.ProxyURL(req.proxyURL)
} else if c.proxyURL != nil {
c.transport.Proxy = http.ProxyURL(c.proxyURL)
}

req.Time = time.Now()
c.httpClient.Transport = c.transport

resp, err := c.httpClient.Do(req.RawRequest)

c.mutex.Unlock()

response := &Response{
Request: req,
RawResponse: resp,
Expand Down Expand Up @@ -704,16 +702,11 @@ func (c *Client) disableLogPrefix() {
func (c *Client) getTLSConfig() *tls.Config {
if c.transport.TLSClientConfig == nil {
c.transport.TLSClientConfig = &tls.Config{}
c.httpClient.Transport = c.transport
}

return c.transport.TLSClientConfig
}

// IsProxySet method returns the true if proxy is set on client otherwise false.
func (c *Client) IsProxySet() bool {
return c.proxyURL != nil
}

//
// File
//
Expand Down
4 changes: 1 addition & 3 deletions client_test.go
Expand Up @@ -7,7 +7,6 @@ package resty
import (
"crypto/tls"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
Expand Down Expand Up @@ -127,9 +126,9 @@ func TestClientProxyOverrideError(t *testing.T) {

c := dc()
c.SetTimeout(1 * time.Second)
c.SetProxy("//not.a.user@%66%6f%6f.com:8888")

resp, err := c.R().
SetProxy("//not.a.user@%66%6f%6f.com:8888").
Get(ts.URL)
assertEqual(t, false, resp == nil)
assertEqual(t, true, err == nil)
Expand Down Expand Up @@ -167,7 +166,6 @@ func TestOnBeforeRequestModification(t *testing.T) {
defer ts.Close()

resp, err := tc.R().Get(ts.URL + "/")
fmt.Println(err, resp)

assertError(t, err)
assertEqual(t, http.StatusOK, resp.StatusCode())
Expand Down
6 changes: 3 additions & 3 deletions default.go
Expand Up @@ -11,7 +11,6 @@ import (
"net/http/cookiejar"
"net/url"
"os"
"sync"
"time"

"golang.org/x/net/publicsuffix"
Expand All @@ -34,12 +33,13 @@ func New() *Client {
Cookies: make([]*http.Cookie, 0),
Debug: false,
Log: getLogger(os.Stderr),
RetryCount: 0,
httpClient: &http.Client{Jar: cookieJar},
transport: &http.Transport{},
mutex: &sync.Mutex{},
RetryCount: 0,
}

c.httpClient.Transport = c.transport

// Default redirect policy
c.SetRedirectPolicy(NoRedirectPolicy())

Expand Down
18 changes: 0 additions & 18 deletions request.go
Expand Up @@ -334,24 +334,6 @@ func (r *Request) SetOutput(file string) *Request {
return r
}

// SetProxy method sets the Proxy URL for current Request. It does not affect client level
// proxy settings. Request level proxy settings takes higher priority, even though client
// level proxy settings exists.
// resty.R().
// SetProxy("http://proxyserver:8888").
// Get("http://httpbin.org/get")
//
func (r *Request) SetProxy(proxyURL string) *Request {
if pURL, err := url.Parse(proxyURL); err == nil {
r.proxyURL = pURL
} else {
r.client.Log.Printf("ERROR [%v]", err)
r.proxyURL = nil
}

return r
}

// SetSRV method sets the details to query the service SRV record and execute the
// request.
// resty.R().
Expand Down
1 change: 0 additions & 1 deletion request16.go
Expand Up @@ -42,7 +42,6 @@ type Request struct {
setContentLength bool
isSaveResponse bool
outputFile string
proxyURL *url.URL
multipartFiles []*File
}

Expand Down
3 changes: 1 addition & 2 deletions request17.go
@@ -1,4 +1,4 @@
// +build go1.7
// +build go1.7 go1.8

// Copyright (c) 2015-2016 Jeevanandam M (jeeva@myjeeva.com)
// 2016 Andrew Grigorev (https://github.com/ei-grad)
Expand Down Expand Up @@ -43,7 +43,6 @@ type Request struct {
setContentLength bool
isSaveResponse bool
outputFile string
proxyURL *url.URL
multipartFiles []*File
ctx context.Context
}
Expand Down
21 changes: 3 additions & 18 deletions resty_test.go
Expand Up @@ -858,15 +858,15 @@ func TestRawFileUploadByBody(t *testing.T) {
func TestProxySetting(t *testing.T) {
c := dc()

assertEqual(t, true, !c.IsProxySet())
assertEqual(t, false, c.IsProxySet())
assertEqual(t, true, (c.transport.Proxy == nil))

c.SetProxy("http://sampleproxy:8888")
assertEqual(t, true, c.IsProxySet())
assertEqual(t, true, (c.transport.Proxy == nil))
assertEqual(t, false, (c.transport.Proxy == nil))

c.SetProxy("//not.a.user@%66%6f%6f.com:8888")
assertEqual(t, true, (c.proxyURL == nil))
assertEqual(t, false, c.IsProxySet())
assertEqual(t, true, (c.transport.Proxy == nil))

SetProxy("http://sampleproxy:8888")
Expand All @@ -875,20 +875,6 @@ func TestProxySetting(t *testing.T) {
assertEqual(t, true, (DefaultClient.transport.Proxy == nil))
}

func TestPerRequestProxy(t *testing.T) {
ts := createGetServer(t)
defer ts.Close()

c := dc()
c.SetTimeout(1 * time.Second)

resp, err := c.R().
SetProxy("http://sampleproxy:8888").
Get(ts.URL)
assertEqual(t, true, resp != nil)
assertEqual(t, true, err != nil)
}

func TestClientProxyOverride(t *testing.T) {
ts := createGetServer(t)
defer ts.Close()
Expand All @@ -898,7 +884,6 @@ func TestClientProxyOverride(t *testing.T) {
c.SetProxy("http://sampleproxy:8888")

resp, err := c.R().
SetProxy("http://requestproxy:8888").
Get(ts.URL)
assertEqual(t, true, resp != nil)
assertEqual(t, true, err != nil)
Expand Down

0 comments on commit 90b37fd

Please sign in to comment.