-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add introspection retrier option (#31)
Allows a custom Retrier during introspection. The included CountRetrier stops retries after a number of attempts. Also refactored IntrospectOptions a bit to handle more options with less complexity.
- Loading branch information
1 parent
a736a2e
commit 05d9531
Showing
6 changed files
with
210 additions
and
24 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
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,31 @@ | ||
package graphql | ||
|
||
// Retrier indicates whether or not to retry and attempt another query. | ||
type Retrier interface { | ||
// ShouldRetry returns true if another attempt should run, | ||
// given 'err' from the previous attempt and the total attempt count (starts at 1). | ||
// | ||
// Consider the 'errors' package to unwrap the error. e.g. errors.As(), errors.Is() | ||
ShouldRetry(err error, attempts uint) bool | ||
} | ||
|
||
var _ Retrier = CountRetrier{} | ||
|
||
// CountRetrier is a Retrier that stops after a number of attempts. | ||
type CountRetrier struct { | ||
// maxAttempts is the maximum number of attempts allowed before retries should stop. | ||
// A value of 0 has undefined behavior. | ||
maxAttempts uint | ||
} | ||
|
||
// NewCountRetrier returns a CountRetrier with the given maximum number of retries | ||
// beyond the first attempt. | ||
func NewCountRetrier(maxRetries uint) CountRetrier { | ||
return CountRetrier{ | ||
maxAttempts: 1 + maxRetries, | ||
} | ||
} | ||
|
||
func (c CountRetrier) ShouldRetry(err error, attempts uint) bool { | ||
return attempts < c.maxAttempts | ||
} |
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,20 @@ | ||
package graphql | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCountRetrier(t *testing.T) { | ||
t.Parallel() | ||
retrier := NewCountRetrier(1) | ||
someErr := errors.New("some error") | ||
|
||
assert.Equal(t, CountRetrier{ | ||
maxAttempts: 2, | ||
}, retrier) | ||
assert.True(t, retrier.ShouldRetry(someErr, 1)) | ||
assert.False(t, retrier.ShouldRetry(someErr, 2)) | ||
} |