Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use HTTP Client SetTimeout instead of custom timeout #60

Merged
merged 2 commits into from
Mar 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"io/ioutil"
"log"
"mime/multipart"
"net"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -492,24 +491,6 @@ func (c *Client) SetTLSClientConfig(config *tls.Config) *Client {
return c
}

// SetTimeout method sets timeout for request raised from client
// resty.SetTimeout(time.Duration(1 * time.Minute))
//
func (c *Client) SetTimeout(timeout time.Duration) *Client {
c.transport.Dial = func(network, addr string) (net.Conn, error) {
conn, err := net.DialTimeout(network, addr, timeout)
if err != nil {
c.Log.Printf("ERROR [%v]", err)
return nil, err
}
err = conn.SetDeadline(time.Now().Add(timeout))
return conn, err
}
c.httpClient.Transport = c.transport

return c
}

// SetProxy method sets the Proxy URL and Port for resty client.
// resty.SetProxy("http://proxyserver:8888")
//
Expand Down
24 changes: 24 additions & 0 deletions client12.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// +build !go1.3

// Copyright (c) 2015-2017 Jeevanandam M ([email protected]), All rights reserved.
// resty source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.

package resty

import (
"net"
"time"
)

// SetTimeout method sets timeout for request raised from client
// resty.SetTimeout(time.Duration(1 * time.Minute))
//
func (c *Client) SetTimeout(timeout time.Duration) *Client {
c.transport.Dial = func(network, addr string) (net.Conn, error) {
return net.DialTimeout(network, addr, timeout)
}
c.transport.ResponseHeaderTimeout = timeout
c.httpClient.Transport = c.transport
return c
}
17 changes: 17 additions & 0 deletions client13.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// +build go1.3

// Copyright (c) 2015-2017 Jeevanandam M ([email protected]), All rights reserved.
// resty source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.

package resty

import "time"

// SetTimeout method sets timeout for request raised from client
// resty.SetTimeout(time.Duration(1 * time.Minute))
//
func (c *Client) SetTimeout(timeout time.Duration) *Client {
c.httpClient.Timeout = timeout
return c
}
22 changes: 21 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,28 @@ func TestClientTimeout(t *testing.T) {
SetTimeout(time.Duration(time.Second * 3))

_, err := c.R().Get(ts.URL + "/set-timeout-test")
assertEqual(t, true, strings.Contains(strings.ToLower(err.Error()), "timeout"))
}

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

c := dc()
c.SetHTTPMode().
SetTimeout(time.Duration(time.Second * 3))

resp, err := c.R().Get(ts.URL + "/set-timeout-test-with-sequence")
assertError(t, err)

seq1, _ := strconv.ParseInt(resp.String(), 10, 64)

resp, err = c.R().Get(ts.URL + "/set-timeout-test-with-sequence")
assertError(t, err)

seq2, _ := strconv.ParseInt(resp.String(), 10, 64)

assertEqual(t, true, strings.Contains(err.Error(), "i/o timeout"))
assertEqual(t, seq1+1, seq2)
}

func TestClientTimeoutInternalError(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions resty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
)
Expand Down Expand Up @@ -1165,6 +1166,7 @@ func getTestDataPath() string {
// Used for retry testing...
var mc = &sync.Mutex{}
var attempt int
var sequence int64

func createGetServer(t *testing.T) *httptest.Server {
ts := createTestServer(func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -1186,6 +1188,10 @@ func createGetServer(t *testing.T) *httptest.Server {
time.Sleep(time.Second * 6)
}
_, _ = w.Write([]byte("TestClientRetry page"))
} else if r.URL.Path == "/set-timeout-test-with-sequence" {
seq := atomic.AddInt64(&sequence, 1)
time.Sleep(time.Second * 2)
_, _ = fmt.Fprintf(w, "%d", seq)
} else if r.URL.Path == "/set-timeout-test" {
time.Sleep(time.Second * 6)
_, _ = w.Write([]byte("TestClientTimeout page"))
Expand Down