Skip to content

Commit

Permalink
Merge pull request #262 from micro/retries
Browse files Browse the repository at this point in the history
Retry requests
  • Loading branch information
Asim Aslam authored May 29, 2018
2 parents f409468 + ba8582a commit 55aca8b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
4 changes: 2 additions & 2 deletions client/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func (r *rpcClient) Call(ctx context.Context, request Request, response interfac
ch := make(chan error, callOpts.Retries)
var gerr error

for i := 0; i < callOpts.Retries; i++ {
for i := 0; i <= callOpts.Retries; i++ {
go func() {
ch <- call(i)
}()
Expand Down Expand Up @@ -411,7 +411,7 @@ func (r *rpcClient) Stream(ctx context.Context, request Request, opts ...CallOpt
ch := make(chan response, callOpts.Retries)
var grr error

for i := 0; i < callOpts.Retries; i++ {
for i := 0; i <= callOpts.Retries; i++ {
go func() {
s, err := call(i)
ch <- response{s, err}
Expand Down
41 changes: 41 additions & 0 deletions client/rpc_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"context"
"errors"
"fmt"
"testing"

Expand Down Expand Up @@ -56,6 +57,46 @@ func TestCallAddress(t *testing.T) {
}

}

func TestCallRetry(t *testing.T) {
service := "test.service"
method := "Test.Method"
address := "10.1.10.1:8080"

var called int

wrap := func(cf CallFunc) CallFunc {
return func(ctx context.Context, addr string, req Request, rsp interface{}, opts CallOptions) error {
called++
if called == 1 {
return errors.New("retry request")
}

// don't do the call
return nil
}
}

r := mock.NewRegistry()
c := NewClient(
Registry(r),
WrapCall(wrap),
)
c.Options().Selector.Init(selector.Registry(r))

req := c.NewRequest(service, method, nil)

// test calling remote address
if err := c.Call(context.Background(), req, nil, WithAddress(address)); err != nil {
t.Fatal("call with address error", err)
}

// num calls
if called < c.Options().CallOptions.Retries+1 {
t.Fatal("request not retried")
}
}

func TestCallWrapper(t *testing.T) {
var called bool
id := "test.1"
Expand Down

0 comments on commit 55aca8b

Please sign in to comment.