Skip to content

Commit 17466b7

Browse files
authored
Merge pull request #588 from xmidt-org/retryingonhttperror
Http error retry funcs
2 parents b88e51f + 77c3773 commit 17466b7

File tree

6 files changed

+181
-1
lines changed

6 files changed

+181
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88

9+
## [v2.0.2]
10+
- Added logic to xhttp for retrying on http errors.[#588](https://github.com/xmidt-org/webpa-common/pull/588)
11+
912
## [v2.0.1]
1013
- Avoid service discovery events for index underflow or retryable errors [#586](https://github.com/xmidt-org/webpa-common/pull/586)
1114

@@ -217,7 +220,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
217220
- The first official release. We will be better about documenting changes
218221
moving forward.
219222

220-
[Unreleased]: https://github.com/xmidt-org/webpa-common/compare/v2.0.1...HEAD
223+
[Unreleased]: https://github.com/xmidt-org/webpa-common/compare/v2.0.2...HEAD
224+
[v2.0.2]: https://github.com/xmidt-org/webpa-common/compare/v2.0.1...v2.0.2
221225
[v2.0.1]: https://github.com/xmidt-org/webpa-common/compare/v2.0.0...v2.0.1
222226
[v2.0.0]: https://github.com/xmidt-org/webpa-common/compare/v1.11.9...v2.0.0
223227
[v1.11.9]: https://github.com/xmidt-org/webpa-common/compare/v1.11.8...v1.11.9

xhttp/error.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/**
2+
* Copyright 2021 Comcast Cable Communications Management, LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
118
package xhttp
219

320
import (

xhttp/error_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/**
2+
* Copyright 2021 Comcast Cable Communications Management, LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
118
package xhttp
219

320
import (

xhttp/mocks_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,9 @@ func (m *mockListener) Close() error {
5656
func (m *mockListener) Addr() net.Addr {
5757
return m.Called().Get(0).(net.Addr)
5858
}
59+
60+
type mockTempError struct{}
61+
62+
func (m mockTempError) Temporary() bool { return true }
63+
64+
func (m mockTempError) Error() string { return "mock temp error" }

xhttp/retry.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
1+
/**
2+
* Copyright 2021 Comcast Cable Communications Management, LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
118
package xhttp
219

320
import (
21+
"context"
22+
"errors"
423
"net/http"
524
"time"
625

@@ -146,3 +165,33 @@ func RetryTransactor(o RetryOptions, next func(*http.Request) (*http.Response, e
146165
return response, err
147166
}
148167
}
168+
169+
func IsTemporary(err error) bool {
170+
type temporary interface {
171+
Temporary() bool
172+
}
173+
var te temporary
174+
if errors.As(err, &te) {
175+
return te.Temporary()
176+
}
177+
return false
178+
}
179+
180+
func ShouldRetry(err error) bool {
181+
if errors.Is(err, context.DeadlineExceeded) {
182+
return false
183+
}
184+
return IsTemporary(err)
185+
}
186+
187+
func RetryCodes(i int) bool {
188+
switch i {
189+
case http.StatusRequestTimeout:
190+
return true
191+
case http.StatusTooManyRequests:
192+
return true
193+
case http.StatusGatewayTimeout:
194+
return true
195+
}
196+
return false
197+
}

xhttp/retry_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
1+
/**
2+
* Copyright 2021 Comcast Cable Communications Management, LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
118
package xhttp
219

320
import (
21+
"context"
422
"errors"
523
"fmt"
624
"io"
@@ -273,3 +291,72 @@ func TestRetryTransactor(t *testing.T) {
273291
t.Run("RewindError", testRetryTransactorRewindError)
274292
t.Run("StatusRetry", testRetryTransactorStatus)
275293
}
294+
295+
func TestRetryCodes(t *testing.T) {
296+
tcs := []struct {
297+
desc string
298+
httpCode int
299+
expectedResult bool
300+
}{
301+
{
302+
desc: "StatusRequestTimeout",
303+
httpCode: 408,
304+
expectedResult: true,
305+
},
306+
{
307+
desc: "StatusTooManyRequests",
308+
httpCode: 429,
309+
expectedResult: true,
310+
},
311+
{
312+
desc: "StatusGatewayTimeout",
313+
httpCode: 504,
314+
expectedResult: true,
315+
},
316+
{
317+
desc: "Random Code Failure",
318+
httpCode: 400,
319+
expectedResult: false,
320+
},
321+
}
322+
323+
for _, tc := range tcs {
324+
t.Run(tc.desc, func(t *testing.T) {
325+
assert := assert.New(t)
326+
res := RetryCodes(tc.httpCode)
327+
assert.Equal(res, tc.expectedResult)
328+
})
329+
}
330+
}
331+
332+
func TestShouldRetry(t *testing.T) {
333+
var mockTempErr mockTempError
334+
tcs := []struct {
335+
desc string
336+
errCase error
337+
expectedResult bool
338+
}{
339+
{
340+
desc: "DeadlineExceeded Case",
341+
errCase: context.DeadlineExceeded,
342+
expectedResult: false,
343+
},
344+
{
345+
desc: "False Temporary Error Case",
346+
errCase: errors.New("not temp"),
347+
expectedResult: false,
348+
},
349+
{
350+
desc: "True Temporary Error Case",
351+
errCase: mockTempErr,
352+
expectedResult: true,
353+
},
354+
}
355+
for _, tc := range tcs {
356+
t.Run(tc.desc, func(t *testing.T) {
357+
assert := assert.New(t)
358+
res := ShouldRetry(tc.errCase)
359+
assert.Equal(res, tc.expectedResult)
360+
})
361+
}
362+
}

0 commit comments

Comments
 (0)