-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Promote the use of Timeout() over Temporary()
- Loading branch information
1 parent
2f8a88f
commit 284cd52
Showing
8 changed files
with
155 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
//go:build go1.7 | ||
// +build go1.7 | ||
|
||
package rehttp | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
//go:build !go1.7 | ||
// +build !go1.7 | ||
|
||
package rehttp | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
//go:build go1.13 | ||
// +build go1.13 | ||
|
||
package rehttp | ||
|
||
import ( | ||
"errors" | ||
"net" | ||
) | ||
|
||
func isTimeoutErr(err error) bool { | ||
var neterr net.Error | ||
if errors.As(err, &neterr) && neterr.Timeout() { | ||
return true | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//go:build !go1.13 | ||
// +build !go1.13 | ||
|
||
package rehttp | ||
|
||
type timeouter interface { | ||
Timeout() bool | ||
} | ||
|
||
func isTimeoutErr(err error) bool { | ||
if terr, ok := err.(timeouter); ok { | ||
return terr.Timeout() | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package rehttp | ||
|
||
import ( | ||
"errors" | ||
"net" | ||
"testing" | ||
|
||
"golang.org/x/net/context" | ||
) | ||
|
||
func Test_isTimeoutErr(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
err error | ||
want bool | ||
}{ | ||
{ | ||
name: "context timeouts should be retryable", | ||
err: context.DeadlineExceeded, | ||
want: true, | ||
}, | ||
{ | ||
name: "net op errors are true", | ||
err: &net.OpError{ | ||
Err: timeoutErr{}, | ||
}, | ||
want: true, | ||
}, | ||
{ | ||
name: "non-network related errors should not be retryable", | ||
err: errors.New("fake error"), | ||
want: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := isTimeoutErr(tt.err); got != tt.want { | ||
t.Errorf("isTimeoutErr() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |