add gRPC call options (#45)
This contains a breaking change that should not affect end users.
The APICall signature now also accepts a CallSettings struct.
diff --git a/call_option.go b/call_option.go
index 4ba1cdf..536cb8c 100644
--- a/call_option.go
+++ b/call_option.go
@@ -129,8 +129,21 @@
return d
}
+type grpcOpt []grpc.CallOption
+
+func (o grpcOpt) Resolve(s *CallSettings) {
+ s.GRPC = o
+}
+
+func WithGRPCOptions(opt ...grpc.CallOption) CallOption {
+ return grpcOpt(append([]grpc.CallOption(nil), opt...))
+}
+
type CallSettings struct {
// Retry returns a Retryer to be used to control retry logic of a method call.
// If Retry is nil or the returned Retryer is nil, the call will not be retried.
Retry func() Retryer
+
+ // CallOptions to be forwarded to GRPC.
+ GRPC []grpc.CallOption
}
diff --git a/invoke.go b/invoke.go
index d2134e1..86049d8 100644
--- a/invoke.go
+++ b/invoke.go
@@ -36,7 +36,7 @@
)
// A user defined call stub.
-type APICall func(context.Context) error
+type APICall func(context.Context, CallSettings) error
// Invoke calls the given APICall,
// performing retries as specified by opts, if any.
@@ -67,7 +67,7 @@
func invoke(ctx context.Context, call APICall, settings CallSettings, sp sleeper) error {
var retryer Retryer
for {
- err := call(ctx)
+ err := call(ctx, settings)
if err == nil {
return nil
}
diff --git a/invoke_test.go b/invoke_test.go
index f181849..3d12e60 100644
--- a/invoke_test.go
+++ b/invoke_test.go
@@ -58,7 +58,7 @@
func (r boolRetryer) Retry(err error) (time.Duration, bool) { return 0, bool(r) }
func TestInvokeSuccess(t *testing.T) {
- apiCall := func(_ context.Context) error { return nil }
+ apiCall := func(context.Context, CallSettings) error { return nil }
var sp recordSleeper
err := invoke(context.Background(), apiCall, CallSettings{}, sp.sleep)
@@ -72,7 +72,7 @@
func TestInvokeNoRetry(t *testing.T) {
apiErr := errors.New("foo error")
- apiCall := func(_ context.Context) error { return apiErr }
+ apiCall := func(context.Context, CallSettings) error { return apiErr }
var sp recordSleeper
err := invoke(context.Background(), apiCall, CallSettings{}, sp.sleep)
@@ -86,7 +86,7 @@
func TestInvokeNilRetry(t *testing.T) {
apiErr := errors.New("foo error")
- apiCall := func(_ context.Context) error { return apiErr }
+ apiCall := func(context.Context, CallSettings) error { return apiErr }
var settings CallSettings
WithRetry(func() Retryer { return nil }).Resolve(&settings)
var sp recordSleeper
@@ -102,7 +102,7 @@
func TestInvokeNeverRetry(t *testing.T) {
apiErr := errors.New("foo error")
- apiCall := func(_ context.Context) error { return apiErr }
+ apiCall := func(context.Context, CallSettings) error { return apiErr }
var settings CallSettings
WithRetry(func() Retryer { return boolRetryer(false) }).Resolve(&settings)
var sp recordSleeper
@@ -121,7 +121,7 @@
retryNum := 0
apiErr := errors.New("foo error")
- apiCall := func(context.Context) error {
+ apiCall := func(context.Context, CallSettings) error {
retryNum++
if retryNum < target {
return apiErr
@@ -143,7 +143,7 @@
func TestInvokeRetryTimeout(t *testing.T) {
apiErr := errors.New("foo error")
- apiCall := func(context.Context) error { return apiErr }
+ apiCall := func(context.Context, CallSettings) error { return apiErr }
var settings CallSettings
WithRetry(func() Retryer { return boolRetryer(true) }).Resolve(&settings)
var sp recordSleeper