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 1 commit
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
13 changes: 1 addition & 12 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 @@ -496,17 +495,7 @@ func (c *Client) SetTLSClientConfig(config *tls.Config) *Client {
// resty.SetTimeout(time.Duration(1 * time.Minute))
//
func (c *Client) SetTimeout(timeout time.Duration) *Client {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently resty support Go1.2 and above. HTTP Client SetTimeout was introduced in Go1.3. You see travis build is failed for Go1.2

Can you please handle these support with build constraints?

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

c.httpClient.Timeout = timeout
return c
}

Expand Down
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